From 67515a357519a3c2fac699ba2e8c56e671b651c5 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 25 Dec 2022 00:37:06 -0500 Subject: day 25 --- src/bin/2022/day25.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/bin/2022/main.rs | 2 ++ 2 files changed, 65 insertions(+) create mode 100644 src/bin/2022/day25.rs (limited to 'src/bin') diff --git a/src/bin/2022/day25.rs b/src/bin/2022/day25.rs new file mode 100644 index 0000000..3149365 --- /dev/null +++ b/src/bin/2022/day25.rs @@ -0,0 +1,63 @@ +#![allow(dead_code)] +#![allow(unused_variables)] + +use advent_of_code::prelude::*; + +fn from_snafu(line: String) -> i64 { + let mut total = 0; + for c in line.chars() { + let digit = match c { + '2' => 2, + '1' => 1, + '0' => 0, + '-' => -1, + '=' => -2, + _ => panic!("unknown char {}", c), + }; + total *= 5; + total += digit; + } + total +} + +fn to_snafu(mut n: i64) -> String { + let mut s = String::new(); + while n > 0 { + let rem = n % 5; + let c = match rem { + 0 => '0', + 1 => '1', + 2 => '2', + 3 => '=', + 4 => '-', + _ => unreachable!(), + }; + s.insert(0, c); + n = (n + 2) / 5; + } + s +} + +pub fn parse(fh: File) -> Result> { + Ok(parse::raw_lines(fh)) +} + +pub fn part1(lines: impl Iterator) -> Result { + Ok(to_snafu(lines.map(from_snafu).sum())) +} + +pub fn part2(_: impl Iterator) -> Result { + todo!() +} + +#[test] +fn test() { + assert_eq!( + part1(parse(parse::data(2022, 25).unwrap()).unwrap()).unwrap(), + "2-20=01--0=0=0=2-120" + ); + // assert_eq!( + // part2(parse(parse::data(2022, 25).unwrap()).unwrap()).unwrap(), + // 0 + // ); +} diff --git a/src/bin/2022/main.rs b/src/bin/2022/main.rs index 10b06fe..46cc3bb 100644 --- a/src/bin/2022/main.rs +++ b/src/bin/2022/main.rs @@ -34,6 +34,7 @@ mod day20; mod day21; mod day23; mod day24; +mod day25; // NEXT MOD #[paw::main] @@ -63,6 +64,7 @@ fn main(opt: Opt) -> Result<()> { 21 => advent_of_code::day!(2022, opt.day, opt.puzzle, day21), 23 => advent_of_code::day!(2022, opt.day, opt.puzzle, day23), 24 => advent_of_code::day!(2022, opt.day, opt.puzzle, day24), + 25 => advent_of_code::day!(2022, opt.day, opt.puzzle, day25), // NEXT PART _ => panic!("unknown day {}", opt.day), } -- cgit v1.2.3-54-g00ecf