diff --git a/src/day15.rs b/src/day15.rs new file mode 100644 index 0000000..df7d580 --- /dev/null +++ b/src/day15.rs @@ -0,0 +1,70 @@ +use std::ascii; + +use crate::*; + +pub struct Day15; +impl Day for Day15 { + const DAY_NUM: u8 = 15; + type Input = String; + type Output = usize; + + fn get_test_data() -> Self::Input { + "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7".into() + } + fn get_multiple_test_result() -> Box> { + Box::new([52, 30, 253, 97, 47, 14, 180, 9, 197, 48, 214, 231].into_iter()) + } + fn get_multiple_test_data() -> Box> { + Box::new( + [ + "HASH".into(), + "rn=1".into(), + "cm-".into(), + "qp=3".into(), + "cm=2".into(), + "qp-".into(), + "pc=4".into(), + "ot=9".into(), + "ab=5".into(), + "pc-".into(), + "pc=6".into(), + "ot=7".into(), + ] + .into_iter(), + ) + } + + fn get_test_result() -> Self::Output { + 1320 + } + + fn run(data: Self::Input) -> Self::Output { + run_part1(&data) + } +} +fn run_part1(data: &str) -> usize { + assert!(!data.contains('\n')); + + data.split(',') + .into_iter() + .filter_map(str::as_ascii) + .map(algorythm) + .sum() +} +fn algorythm(data: &[std::ascii::Char]) -> usize { + assert!(!data.contains(&ascii::Char::Comma)); + let mut result = 0; + + for x in data + .into_iter() + // .inspect(|x| print!("{x}:")) + .map(get_ascii_value) + { + result = ((result + x) * 17) % 256; + // println!("{result}"); + } + result +} +fn get_ascii_value(c: &std::ascii::Char) -> usize { + c.to_u8() as usize +} diff --git a/src/lib.rs b/src/lib.rs index beedca5..dc7455e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![feature(test)] #![feature(iter_map_windows)] +#![feature(ascii_char, ascii_char_variants)] pub fn read_input(day: u8) -> T where @@ -35,6 +36,9 @@ pub use day10::*; mod day14; pub use day14::*; +mod day15; +pub use day15::*; + mod day19; pub use day19::*; diff --git a/src/main.rs b/src/main.rs index 05d9b6f..ed75df1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,8 @@ fn main() { // Day10::part1(); // Day10::part2(); // Day10::run_all(); - Day14::run_all(); + // Day14::run_all(); + Day15::part1(); // Day14::part1(); // Day14::part2(); //Day19::run_all();