From 5eba5d2b339075f51b7255de72d32c8a740614e3 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 1 Dec 2023 00:42:45 -0500 Subject: day 1 --- src/bin/2023/day1.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/bin/2023/main.rs | 26 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/bin/2023/day1.rs create mode 100644 src/bin/2023/main.rs (limited to 'src/bin') 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> { + Ok(parse::lines(fh)) +} + +pub fn part1(lines: impl Iterator) -> Result { + 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) -> Result { + 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(()) +} -- cgit v1.2.3-54-g00ecf