day1 part 2 with typo in chumsky parser fixed
This commit is contained in:
parent
9cf3b3ef76
commit
21d44796c7
1 changed files with 1 additions and 40 deletions
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
use aoc23::read;
|
use aoc23::read;
|
||||||
use chumsky::prelude::*;
|
use chumsky::prelude::*;
|
||||||
use std::{io::{BufReader, BufRead}, fs::File};
|
|
||||||
use chumsky::chain::Chain;
|
|
||||||
|
|
||||||
struct Line {
|
struct Line {
|
||||||
first_digit: u128,
|
first_digit: u128,
|
||||||
|
@ -20,9 +18,8 @@ impl Line {
|
||||||
choice((
|
choice((
|
||||||
// digit combinations (only the relevant ones)
|
// digit combinations (only the relevant ones)
|
||||||
just("oneight").map(|_| array(&[1, 8])),
|
just("oneight").map(|_| array(&[1, 8])),
|
||||||
just("twoone").map(|_| array(&[2, 1])),
|
just("twone").map(|_| array(&[2, 1])),
|
||||||
just("eightwo").map(|_| array(&[8, 2])),
|
just("eightwo").map(|_| array(&[8, 2])),
|
||||||
|
|
||||||
// single digits
|
// single digits
|
||||||
just("one").or(just("1")).map(|_| array(&[1])),
|
just("one").or(just("1")).map(|_| array(&[1])),
|
||||||
just("two").or(just("2")).map(|_| array(&[2])),
|
just("two").or(just("2")).map(|_| array(&[2])),
|
||||||
|
@ -33,7 +30,6 @@ impl Line {
|
||||||
just("seven").or(just("7")).map(|_| array(&[7])),
|
just("seven").or(just("7")).map(|_| array(&[7])),
|
||||||
just("eight").or(just("8")).map(|_| array(&[8])),
|
just("eight").or(just("8")).map(|_| array(&[8])),
|
||||||
just("nine").or(just("9")).map(|_| array(&[9])),
|
just("nine").or(just("9")).map(|_| array(&[9])),
|
||||||
|
|
||||||
// garbage
|
// garbage
|
||||||
none_of(['1', '2', '3', '4', '5', '6', '7', '8', '9', '\n'])
|
none_of(['1', '2', '3', '4', '5', '6', '7', '8', '9', '\n'])
|
||||||
.map(|_| array(&[]))
|
.map(|_| array(&[]))
|
||||||
|
@ -62,43 +58,8 @@ fn main() -> anyhow::Result<()> {
|
||||||
let sum: u128 = lines
|
let sum: u128 = lines
|
||||||
.iter()
|
.iter()
|
||||||
.map(|line| line.first_digit * 10 + line.last_digit)
|
.map(|line| line.first_digit * 10 + line.last_digit)
|
||||||
// .map(|number| { eprintln!("{number}"); number })
|
|
||||||
.sum();
|
.sum();
|
||||||
println!("{sum}");
|
println!("{sum}");
|
||||||
|
|
||||||
let mut sum: u128 = 0;
|
|
||||||
for line in BufReader::new(File::open("inputs/day1.txt")?).lines() {
|
|
||||||
let line = line?;
|
|
||||||
let line = line.trim();
|
|
||||||
if line.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut digits = Vec::new();
|
|
||||||
for i in 0 .. line.len() {
|
|
||||||
let s = &line[i..];
|
|
||||||
|
|
||||||
macro_rules! check_digit {
|
|
||||||
($digit:literal, $string:literal) => {
|
|
||||||
if s.starts_with(stringify!($digit)) || line[i..].starts_with($string) {
|
|
||||||
digits.push($digit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
check_digit!(1, "one");
|
|
||||||
check_digit!(2, "two");
|
|
||||||
check_digit!(3, "three");
|
|
||||||
check_digit!(4, "four");
|
|
||||||
check_digit!(5, "five");
|
|
||||||
check_digit!(6, "six");
|
|
||||||
check_digit!(7, "seven");
|
|
||||||
check_digit!(8, "eight");
|
|
||||||
check_digit!(9, "nine");
|
|
||||||
}
|
|
||||||
sum += digits.first().unwrap() * 10 + digits.last().unwrap();
|
|
||||||
}
|
|
||||||
println!("{sum}");
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue