Skip to content

Commit eeb69c1

Browse files
committed
Switch to the new framework: src/y2023 (part1-3)
1 parent 1a173e2 commit eeb69c1

16 files changed

+297
-266
lines changed

src/y2023/day01.rs

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! <https://adventofcode.com/2023/day/1>
2-
use crate::framework::{aoc, AdventOfCode, ParseError};
2+
use crate::framework::{AdventOfCode, ParseError, aoc};
33

44
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
55
pub struct Puzzle {
@@ -40,39 +40,40 @@ impl Default for Puzzle {
4040

4141
#[aoc(2023, 1)]
4242
impl AdventOfCode for Puzzle {
43-
const DELIMITER: &'static str = "\n";
44-
fn parse_block(&mut self, block: &str) -> Result<(), ParseError> {
45-
let b = block.bytes().collect::<Vec<_>>();
46-
let len = b.len();
47-
for dir in [0, 1] {
48-
let scale = 10 - dir * 9;
49-
let mut not_found = true;
50-
for ii in 0..len {
51-
let i = if dir == 1 { len - ii - 1 } else { ii };
52-
let mut value = self.table[b[i] as usize];
53-
if value == 0 {
54-
continue;
55-
}
56-
if value < 10 {
57-
value *= scale;
58-
if not_found {
59-
self.sum2 += value;
43+
fn parse(&mut self, input: &str) -> Result<(), ParseError> {
44+
for l in input.lines() {
45+
let b = l.bytes().collect::<Vec<_>>();
46+
let len = b.len();
47+
for dir in [0, 1] {
48+
let scale = 10 - dir * 9;
49+
let mut not_found = true;
50+
for ii in 0..len {
51+
let i = if dir == 1 { len - ii - 1 } else { ii };
52+
let mut value = self.table[b[i] as usize];
53+
if value == 0 {
54+
continue;
6055
}
61-
self.sum1 += value;
62-
break;
63-
}
64-
if not_found {
65-
for (j, r) in self.subst.iter().enumerate() {
66-
if b[i..].starts_with(r) {
67-
self.sum2 += scale * (j + 1);
68-
not_found = false;
69-
break;
56+
if value < 10 {
57+
value *= scale;
58+
if not_found {
59+
self.sum2 += value;
60+
}
61+
self.sum1 += value;
62+
break;
63+
}
64+
if not_found {
65+
for (j, r) in self.subst.iter().enumerate() {
66+
if b[i..].starts_with(r) {
67+
self.sum2 += scale * (j + 1);
68+
not_found = false;
69+
break;
70+
}
7071
}
7172
}
7273
}
7374
}
7475
}
75-
Ok(())
76+
Self::parsed()
7677
}
7778
fn part1(&mut self) -> Self::Output1 {
7879
self.sum1

src/y2023/day02.rs

+34-34
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! <https://adventofcode.com/2023/day/2>
22
use {
3-
crate::framework::{aoc, AdventOfCode, ParseError},
3+
crate::framework::{AdventOfCode, ParseError, aoc},
44
winnow::{
5+
ModalResult, Parser,
56
ascii::{alpha1, digit1, space1},
67
combinator::{delimited, separated, terminated},
7-
ModalResult, Parser,
88
},
99
};
1010

@@ -17,40 +17,40 @@ pub struct Puzzle {
1717

1818
#[aoc(2023, 2)]
1919
impl AdventOfCode for Puzzle {
20-
const DELIMITER: &'static str = "\n";
21-
fn parse_block(&mut self, block: &str) -> Result<(), ParseError> {
22-
self.index += 1;
23-
fn parse_color(block: &mut &str) -> ModalResult<(String, usize)> {
24-
let value = terminated(digit1, space1).parse_next(block)?;
25-
let color = alpha1(block)?;
26-
Ok((color.to_string(), value.parse::<usize>().unwrap()))
27-
}
28-
fn parse_block(block: &mut &str) -> ModalResult<(usize, usize, usize)> {
29-
let v: Vec<(String, usize)> = separated(1.., parse_color, ", ").parse_next(block)?;
30-
let v3 = v.iter().fold((0, 0, 0), |acc, c_v| match c_v.0.as_str() {
31-
"red" => (c_v.1, acc.1, acc.2),
32-
"green" => (acc.0, c_v.1, acc.2),
33-
"blue" => (acc.0, acc.1, c_v.1),
34-
_ => panic!("can't"),
20+
fn parse(&mut self, input: &str) -> Result<(), ParseError> {
21+
for mut l in input.lines() {
22+
self.index += 1;
23+
fn parse_color(block: &mut &str) -> ModalResult<(String, usize)> {
24+
let value = terminated(digit1, space1).parse_next(block)?;
25+
let color = alpha1(block)?;
26+
Ok((color.to_string(), value.parse::<usize>().unwrap()))
27+
}
28+
fn parse_block(block: &mut &str) -> ModalResult<(usize, usize, usize)> {
29+
let v: Vec<(String, usize)> =
30+
separated(1.., parse_color, ", ").parse_next(block)?;
31+
let v3 = v.iter().fold((0, 0, 0), |acc, c_v| match c_v.0.as_str() {
32+
"red" => (c_v.1, acc.1, acc.2),
33+
"green" => (acc.0, c_v.1, acc.2),
34+
"blue" => (acc.0, acc.1, c_v.1),
35+
_ => panic!("can't"),
36+
});
37+
Ok(v3)
38+
}
39+
fn parse_line(block: &mut &str) -> ModalResult<Vec<(usize, usize, usize)>> {
40+
let _num = delimited("Game ", digit1, ": ").parse_next(block)?;
41+
let v = separated(1.., parse_block, "; ").parse_next(block)?;
42+
Ok(v)
43+
}
44+
let x = parse_line(&mut l)?;
45+
let maxs = x.iter().fold((0, 0, 0), |acc, val| {
46+
(acc.0.max(val.0), acc.1.max(val.1), acc.2.max(val.2))
3547
});
36-
Ok(v3)
37-
}
38-
fn parse_line(block: &mut &str) -> ModalResult<Vec<(usize, usize, usize)>> {
39-
let _num = delimited("Game ", digit1, ": ").parse_next(block)?;
40-
let v = separated(1.., parse_block, "; ").parse_next(block)?;
41-
Ok(v)
42-
}
43-
let s = block.to_string();
44-
let p = &mut s.as_str();
45-
let x = parse_line(p)?;
46-
let maxs = x.iter().fold((0, 0, 0), |acc, val| {
47-
(acc.0.max(val.0), acc.1.max(val.1), acc.2.max(val.2))
48-
});
49-
if maxs.0 <= 12 && maxs.1 <= 13 && maxs.2 <= 14 {
50-
self.result1 += self.index;
48+
if maxs.0 <= 12 && maxs.1 <= 13 && maxs.2 <= 14 {
49+
self.result1 += self.index;
50+
}
51+
self.result2 += maxs.0 * maxs.1 * maxs.2;
5152
}
52-
self.result2 += maxs.0 * maxs.1 * maxs.2;
53-
Ok(())
53+
Self::parsed()
5454
}
5555
fn part1(&mut self) -> Self::Output1 {
5656
self.result1

src/y2023/day03.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! <https://adventofcode.com/2023/day/3>
22
use {
3-
crate::framework::{aoc, AdventOfCode, ParseError},
3+
crate::framework::{AdventOfCode, ParseError, aoc},
44
std::collections::HashMap,
55
};
66

@@ -13,36 +13,37 @@ pub struct Puzzle {
1313

1414
#[aoc(2023, 3)]
1515
impl AdventOfCode for Puzzle {
16-
const DELIMITER: &'static str = "\n";
17-
fn parse_block(&mut self, block: &str) -> Result<(), ParseError> {
18-
let cs = block.chars().collect::<Vec<_>>();
19-
let mut acc: Option<usize> = None;
20-
let mut pos_start = 0;
21-
for (j, c) in cs.iter().enumerate() {
22-
if c.is_ascii_digit() {
23-
let n = (*c as u8 - b'0') as usize;
24-
if let Some(a) = acc {
25-
acc = Some(a * 10 + n);
16+
fn parse(&mut self, input: &str) -> Result<(), ParseError> {
17+
for l in input.lines() {
18+
let cs = l.chars().collect::<Vec<_>>();
19+
let mut acc: Option<usize> = None;
20+
let mut pos_start = 0;
21+
for (j, c) in cs.iter().enumerate() {
22+
if c.is_ascii_digit() {
23+
let n = (*c as u8 - b'0') as usize;
24+
if let Some(a) = acc {
25+
acc = Some(a * 10 + n);
26+
} else {
27+
acc = Some(n);
28+
pos_start = j;
29+
}
2630
} else {
27-
acc = Some(n);
28-
pos_start = j;
29-
}
30-
} else {
31-
if *c != '.' {
32-
self.symbol.push((self.line, j, *c));
33-
}
34-
if let Some(n) = acc {
35-
// dbg!((self.line, pos_start, j), n);
36-
self.number.insert((self.line, pos_start, j - 1), n);
37-
acc = None;
31+
if *c != '.' {
32+
self.symbol.push((self.line, j, *c));
33+
}
34+
if let Some(n) = acc {
35+
// dbg!((self.line, pos_start, j), n);
36+
self.number.insert((self.line, pos_start, j - 1), n);
37+
acc = None;
38+
}
3839
}
3940
}
41+
if let Some(n) = acc {
42+
self.number.insert((self.line, pos_start, cs.len()), n);
43+
}
44+
self.line += 1;
4045
}
41-
if let Some(n) = acc {
42-
self.number.insert((self.line, pos_start, cs.len()), n);
43-
}
44-
self.line += 1;
45-
Ok(())
46+
Self::parsed()
4647
}
4748
fn part1(&mut self) -> Self::Output1 {
4849
self.number

src/y2023/day04.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! <https://adventofcode.com/2023/day/4>
22
use crate::{
3-
framework::{aoc, AdventOfCode, ParseError},
3+
framework::{AdventOfCode, ParseError, aoc},
44
parser,
55
};
66

@@ -12,15 +12,16 @@ pub struct Puzzle {
1212

1313
#[aoc(2023, 4)]
1414
impl AdventOfCode for Puzzle {
15-
const DELIMITER: &'static str = "\n";
16-
fn parse_block(&mut self, block: &str) -> Result<(), ParseError> {
17-
let mut vecs: [Vec<usize>; 2] = [Vec::new(), Vec::new()];
18-
for (i, segment) in block.split(':').nth(1).unwrap().split(" | ").enumerate() {
19-
vecs[i] = parser::to_usizes(segment, &[' ']).unwrap();
15+
fn parse(&mut self, input: &str) -> Result<(), ParseError> {
16+
for l in input.lines() {
17+
let mut vecs: [Vec<usize>; 2] = [Vec::new(), Vec::new()];
18+
for (i, segment) in l.split(':').nth(1).unwrap().split(" | ").enumerate() {
19+
vecs[i] = parser::to_usizes(segment, &[' ']).unwrap();
20+
}
21+
self.card.push(vecs);
22+
self.amount.push(1);
2023
}
21-
self.card.push(vecs);
22-
self.amount.push(1);
23-
Ok(())
24+
Self::parsed()
2425
}
2526
fn part1(&mut self) -> Self::Output1 {
2627
self.card

src/y2023/day05.rs

+36-29
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
//! <https://adventofcode.com/2023/day/5>
22
use {
3-
crate::{
4-
framework::{aoc, AdventOfCode, ParseError},
5-
parser::parse_usize,
6-
},
3+
crate::framework::{AdventOfCode, ParseError, aoc},
74
itertools::Itertools,
8-
winnow::{
9-
ascii::{newline, space1, till_line_ending},
10-
combinator::{preceded, separated},
11-
ModalResult, Parser,
12-
},
135
};
146

157
// A half-open range implementation
@@ -21,30 +13,45 @@ pub struct Puzzle {
2113
line: Vec<Vec<(usize, usize, usize)>>,
2214
}
2315

24-
fn parse_line(str: &mut &str) -> ModalResult<Vec<usize>> {
25-
separated(1.., parse_usize, space1).parse_next(str)
16+
mod parser {
17+
use {
18+
crate::parser::parse_usize,
19+
winnow::{
20+
ModalResult, Parser,
21+
ascii::{alpha1, newline, space1},
22+
combinator::{separated, seq},
23+
},
24+
};
25+
26+
fn parse_seeds(s: &mut &str) -> ModalResult<Vec<usize>> {
27+
seq!(_: "seeds: ", separated(1.., parse_usize, space1))
28+
.map(|(v,)| v)
29+
.parse_next(s)
30+
}
31+
32+
fn parse_block(s: &mut &str) -> ModalResult<Vec<(usize, usize, usize)>> {
33+
seq!(
34+
_: (alpha1, "-to-", alpha1, " map:\n"),
35+
separated(1.., separated(3, parse_usize, " ").map(|v: Vec<usize>| (v[0], v[1], v[1] + v[2])), newline)
36+
.map(|v: Vec<(usize, usize, usize)>| v)
37+
)
38+
.map(|(v,)| v)
39+
.parse_next(s)
40+
}
41+
42+
pub fn parse(s: &mut &str) -> ModalResult<(Vec<usize>, Vec<Vec<(usize, usize, usize)>>)> {
43+
seq!(parse_seeds, _: (newline, newline), separated(1.., parse_block, (newline, newline)))
44+
.parse_next(s)
45+
}
2646
}
2747

2848
#[aoc(2023, 5)]
2949
impl AdventOfCode for Puzzle {
30-
const DELIMITER: &'static str = "\n\n";
31-
fn parse_block(&mut self, block: &str) -> Result<(), ParseError> {
32-
fn parse_block(str: &mut &str) -> ModalResult<Vec<(usize, usize, usize)>> {
33-
let _ = preceded(till_line_ending, newline).parse_next(str)?;
34-
let v: Vec<Vec<usize>> = separated(1.., parse_line, newline).parse_next(str)?;
35-
Ok(v.iter()
36-
.map(|l| (l[0], l[1], (l[1] + l[2])))
37-
.collect::<Vec<_>>())
38-
}
39-
if block.starts_with("seeds:") {
40-
let vals = &mut block.split(": ").nth(1).unwrap().trim();
41-
self.seeds = parse_line(vals).expect("error");
42-
return Ok(());
43-
}
44-
let p = block.to_string();
45-
let v = parse_block(&mut p.as_str())?;
46-
self.line.push(v);
47-
Ok(())
50+
fn parse(&mut self, mut input: &str) -> Result<(), ParseError> {
51+
let (seeds, rules) = parser::parse(&mut input)?;
52+
self.seeds = seeds;
53+
self.line = rules;
54+
Self::parsed()
4855
}
4956
#[allow(clippy::unnecessary_lazy_evaluations)]
5057
fn part1(&mut self) -> Self::Output1 {

src/y2023/day06.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! <https://adventofcode.com/2023/day/6>
22
use crate::{
3-
framework::{aoc, AdventOfCode, ParseError},
3+
framework::{AdventOfCode, ParseError, aoc},
44
parser,
55
};
66

@@ -13,11 +13,15 @@ pub struct Puzzle {
1313

1414
#[aoc(2023, 6)]
1515
impl AdventOfCode for Puzzle {
16-
const DELIMITER: &'static str = "\n";
17-
fn parse_block(&mut self, block: &str) -> Result<(), ParseError> {
18-
let s = block.split(':').nth(1).unwrap().trim();
19-
self.line.push(parser::to_usizes(s, &[' ']).unwrap());
20-
Ok(())
16+
fn parse(&mut self, input: &str) -> Result<(), ParseError> {
17+
self.line = input
18+
.lines()
19+
.map(|l| {
20+
let s = l.split(':').nth(1).unwrap().trim();
21+
parser::to_usizes(s, &[' ']).unwrap()
22+
})
23+
.collect();
24+
Self::parsed()
2125
}
2226
fn end_of_data(&mut self) {
2327
for (i, t) in self.line[0].iter().enumerate() {

0 commit comments

Comments
 (0)