Compare commits

...

3 commits

Author SHA1 Message Date
21d44796c7
day1 part 2 with typo in chumsky parser fixed 2023-12-01 14:13:19 +01:00
9cf3b3ef76
day1 part 2 (but manual parser) 2023-12-01 14:09:55 +01:00
26134b89cd
day 1 part 1 2023-12-01 13:32:03 +01:00
2 changed files with 1054 additions and 3 deletions

1000
inputs/day1.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -3,12 +3,63 @@
use aoc23::read; use aoc23::read;
use chumsky::prelude::*; use chumsky::prelude::*;
fn parser() -> impl Parser<char, (), Error = Simple<char>> { struct Line {
end() first_digit: u128,
last_digit: u128
}
// thanks rust
fn array<T, const N: usize>(array: &'static [T; N]) -> &'static [T] {
array
}
impl Line {
fn parser() -> impl Parser<char, Self, Error = Simple<char>> {
choice((
// digit combinations (only the relevant ones)
just("oneight").map(|_| array(&[1, 8])),
just("twone").map(|_| array(&[2, 1])),
just("eightwo").map(|_| array(&[8, 2])),
// single digits
just("one").or(just("1")).map(|_| array(&[1])),
just("two").or(just("2")).map(|_| array(&[2])),
just("three").or(just("3")).map(|_| array(&[3])),
just("four").or(just("4")).map(|_| array(&[4])),
just("five").or(just("5")).map(|_| array(&[5])),
just("six").or(just("6")).map(|_| array(&[6])),
just("seven").or(just("7")).map(|_| array(&[7])),
just("eight").or(just("8")).map(|_| array(&[8])),
just("nine").or(just("9")).map(|_| array(&[9])),
// garbage
none_of(['1', '2', '3', '4', '5', '6', '7', '8', '9', '\n'])
.map(|_| array(&[]))
))
.repeated()
.then_ignore(just('\n'))
.map(|digits: Vec<&'static [u128]>| {
let mut digits = digits.into_iter().flat_map(|array| array.iter().copied());
let first_digit = digits.next().expect("No digit in line");
let last_digit = digits.last().unwrap_or(first_digit);
Line {
first_digit,
last_digit
}
})
}
}
fn parser() -> impl Parser<char, Vec<Line>, Error = Simple<char>> {
Line::parser().repeated().then_ignore(end())
} }
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let _ = read("input.txt", parser())?; let lines = read("inputs/day1.txt", parser())?;
let sum: u128 = lines
.iter()
.map(|line| line.first_digit * 10 + line.last_digit)
.sum();
println!("{sum}");
Ok(()) Ok(())
} }