diff options
author | Jesse Luehrs <doy@tozt.net> | 2023-12-01 00:42:45 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2023-12-01 00:42:45 -0500 |
commit | 5eba5d2b339075f51b7255de72d32c8a740614e3 (patch) | |
tree | e8e8dba235b0a6c4a2bf80aee4ee7ced108a8475 /src/bin | |
parent | 18202694cfc4c715de7dab36639373f9deb1abe3 (diff) | |
download | advent-of-code-5eba5d2b339075f51b7255de72d32c8a740614e3.tar.gz advent-of-code-5eba5d2b339075f51b7255de72d32c8a740614e3.zip |
day 1
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/2023/day1.rs | 73 | ||||
-rw-r--r-- | src/bin/2023/main.rs | 26 |
2 files changed, 99 insertions, 0 deletions
diff --git a/src/bin/2023/day1.rs b/src/bin/2023/day1.rs new file mode 100644 index 0000000..af98533 --- /dev/null +++ b/src/bin/2023/day1.rs @@ -0,0 +1,73 @@ +#![allow(dead_code)] +#![allow(unused_variables)] + +use advent_of_code::prelude::*; + +const NUMBERS: [&str; 10] = [ + "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", + "nine", +]; + +pub fn parse(fh: File) -> Result<impl Iterator<Item = String>> { + Ok(parse::lines(fh)) +} + +pub fn part1(lines: impl Iterator<Item = String>) -> Result<i64> { + let mut total = 0; + for line in lines { + let first = line.chars().find(char::is_ascii_digit).unwrap(); + let last = line.chars().rev().find(char::is_ascii_digit).unwrap(); + let val: i64 = format!("{}{}", first, last).parse().unwrap(); + total += val; + } + Ok(total) +} + +pub fn part2(lines: impl Iterator<Item = String>) -> Result<i64> { + let mut total = 0; + for line in lines { + let mut first = 0; + let mut last = 0; + 'c: for idx in 0..line.len() { + let c = line.chars().nth(idx).unwrap(); + if char::is_numeric(c) { + first = i64::from(u32::from(c) - u32::from('0')); + break 'c; + } + for (i, n) in NUMBERS.iter().enumerate() { + if line[idx..].starts_with(n) { + first = i64::try_from(i).unwrap(); + break 'c; + } + } + } + 'c: for idx in (0..line.len()).rev() { + let c = line.chars().nth(idx).unwrap(); + if char::is_numeric(c) { + last = i64::from(u32::from(c) - u32::from('0')); + break 'c; + } + for (i, n) in NUMBERS.iter().enumerate() { + if line[..=idx].ends_with(n) { + last = i64::try_from(i).unwrap(); + break 'c; + } + } + } + let val: i64 = format!("{}{}", first, last).parse().unwrap(); + total += val; + } + Ok(total) +} + +#[test] +fn test() { + assert_eq!( + part1(parse(parse::data(2023, 1).unwrap()).unwrap()).unwrap(), + 54927 + ); + assert_eq!( + part2(parse(parse::data(2023, 1).unwrap()).unwrap()).unwrap(), + 54581 + ); +} diff --git a/src/bin/2023/main.rs b/src/bin/2023/main.rs new file mode 100644 index 0000000..01e0de3 --- /dev/null +++ b/src/bin/2023/main.rs @@ -0,0 +1,26 @@ +#![allow(clippy::cognitive_complexity)] +#![allow(clippy::missing_const_for_fn)] +#![allow(clippy::similar_names)] +#![allow(clippy::struct_excessive_bools)] +#![allow(clippy::too_many_arguments)] +#![allow(clippy::too_many_lines)] +#![allow(clippy::type_complexity)] +#![allow(clippy::collapsible_else_if)] +#![allow(clippy::collapsible_if)] +#![allow(clippy::comparison_chain)] + +use advent_of_code::prelude::*; + +mod day1; +// NEXT MOD + +#[paw::main] +fn main(opt: Opt) -> Result<()> { + #[allow(clippy::match_single_binding)] + match opt.day { + 1 => advent_of_code::day!(2023, opt.day, opt.puzzle, day1), + // NEXT PART + _ => panic!("unknown day {}", opt.day), + } + Ok(()) +} |