diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-12-25 00:37:06 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-12-25 00:37:06 -0500 |
commit | 67515a357519a3c2fac699ba2e8c56e671b651c5 (patch) | |
tree | 5e7810ca18f6bf990caa76855fd3322089b23ec8 /src/bin | |
parent | 4cae21c7b20f0e4480fc8f0d113d89e2bd2b8de5 (diff) | |
download | advent-of-code-67515a357519a3c2fac699ba2e8c56e671b651c5.tar.gz advent-of-code-67515a357519a3c2fac699ba2e8c56e671b651c5.zip |
day 25
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/2022/day25.rs | 63 | ||||
-rw-r--r-- | src/bin/2022/main.rs | 2 |
2 files changed, 65 insertions, 0 deletions
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<impl Iterator<Item = String>> { + Ok(parse::raw_lines(fh)) +} + +pub fn part1(lines: impl Iterator<Item = String>) -> Result<String> { + Ok(to_snafu(lines.map(from_snafu).sum())) +} + +pub fn part2(_: impl Iterator<Item = String>) -> Result<i64> { + 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), } |