From ea51fd11feb1df2a4508d086e6f6471e30d2c066 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sun, 25 Dec 2022 13:54:52 +0100 Subject: [PATCH] 2022 - Day 25 [Merry Christmas!] --- 2022/input/25/input | 116 ++++++++++++++++++++++++++++++++++++++++++ 2022/input/25/test-1 | 13 +++++ 2022/src/bin/day25.rs | 97 +++++++++++++++++++++++++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 2022/input/25/input create mode 100644 2022/input/25/test-1 create mode 100644 2022/src/bin/day25.rs diff --git a/2022/input/25/input b/2022/input/25/input new file mode 100644 index 0000000..bc619f6 --- /dev/null +++ b/2022/input/25/input @@ -0,0 +1,116 @@ +1=-=2=-1000=0 +11=001122 +120- +1-0 +202-0==-2- +2-1010=2-2=01 +112=-2 +2-0= +1-=2-=-1-0--0=1=0000 +1=0=0--021-0 +11000101=210 +21-111=2112=2==- +111010-2 +2=10=-101-=--0 +2-=-2000012=0022= +1-=11-01--2100 +1=2211-2-=== +2=--2-==-1 +1=12---02=2 +1==0=--11=01-1- +121-21021 +12=1001=2=-=22 +1=2-212=2-0211 +20----11= +10=100=0=0 +111 +1=--011==2--212121= +2=-10=210200-2-=0 +1=-122=001-1 +2-0=0121=-0=0 +2-=20--1-=21-22=1= +20011 +12=1--=-0--10-0=1-= +2002=100=-00- +202 +1=2==21- +1=221--01=00-10= +1--0-1= +2-==11=222--- +21-10-=22 +1=--22-0- +2-0- +1-0=-2 +20-02102 +1=0=20----21=12=- +20=111-12=21101= +111=1=-2=0=--21 +11- +10-1=01=- +1222-10 +1=2-1 +20 +101==12-1-0 +1-=22=--=22=1=22 +1200-010-10=021 +1=2-10=-02--=- +12-0=000=0 +1==10=00===1-- +2-1=-=1===1=-0=--- +1=11=120 +2===002010=-=0 +1==00212=01-002=221 +10020221===- +1--01=-=0-20-101-=- +112=101 +10- +1220=1 +2= +1=1120=--0 +11-21200= +2==011-10-0==0=0= +12-=102 +100-022 +1=1 +201-0--=- +2-02 +21=11=00020 +12=20- +1101=00-1 +1=0=-11--=-010-==1 +1-2-0=00=-=220=11- +2-2 +12-2 +12=10=2-= +20222110-10002 +1002=0 +12=11-=012 +1== +1=00-01 +110101222-020--111 +2-0 +1-12=1 +222-10-12=00 +11=1=1 +10=2-00-121-2-=2 +1---22-1-0=00-1220 +2-=2210=-==2=010= +12201-111-220 +2=122=022=22=12-1 +211===2=020 +121-2-0-=0==2=1 +1-0221200-21100- +10 +1-11 +2===2=-2- +1-0-2021-21== +1==-00-=2-21===1200 +22 +1222=20=120=1- +11=120001012==01-1 +2=-1--00 +10==121--221 +1=2100102=20012 +2==1- +1- +10-2--0 diff --git a/2022/input/25/test-1 b/2022/input/25/test-1 new file mode 100644 index 0000000..027aeec --- /dev/null +++ b/2022/input/25/test-1 @@ -0,0 +1,13 @@ +1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 diff --git a/2022/src/bin/day25.rs b/2022/src/bin/day25.rs new file mode 100644 index 0000000..2ce067c --- /dev/null +++ b/2022/src/bin/day25.rs @@ -0,0 +1,97 @@ +#![feature(test)] +use anyhow::Result; +use aoc::Solver; + +// -- Runners -- +fn main() -> Result<()> { + Day::solve() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn part1_test1() -> Result<()> { + Day::test(Day::part1, "test-1", "2=-1=0".to_owned()) + } + #[test] + fn part1_solution() -> Result<()> { + Day::test(Day::part1, "input", "2-20=01--0=0=0=2-120".to_owned()) + } + + // Benchmarks + extern crate test; + #[bench] + #[ignore] + fn part1_bench(b: &mut test::Bencher) { + Day::benchmark(Day::part1, b) + } + #[bench] + #[ignore] + fn part2_bench(b: &mut test::Bencher) { + Day::benchmark(Day::part2, b) + } +} + +// -- Solution -- +pub struct Day; +impl aoc::Solver for Day { + type Output1 = String; + type Output2 = String; + + fn day() -> u8 { + 25 + } + + fn part1(input: &str) -> Self::Output1 { + let mut sum: isize = input + .trim() + .lines() + .map(|line| { + line.chars().rev().enumerate().fold(0, |acc, (idx, c)| { + let num = match c { + '2' => 2, + '1' => 1, + '0' => 0, + '-' => -1, + '=' => -2, + _ => panic!("Invalid number!"), + }; + + acc + num * 5_isize.pow(idx as u32) + }) + }).sum(); + + let mut result = String::new(); + while sum > 0 { + let mut carry = 0; + let rem = sum % 5; + match rem { + 0 => result += "0", + 1 => result += "1", + 2 => result += "2", + 3 => { + result += "="; + carry += 1; + }, + 4 => { + result += "-"; + carry += 1; + }, + _ => unreachable!("Number is mod 5 so should always be 0..=4"), + } + + sum = sum/5 + carry; + } + // Reverse the string, since we construct it the wrong way around + result = result.chars().rev().collect(); + + result + } + + fn part2(_input: &str) -> Self::Output2 { + // There is no part 2! + "Merry Christmas!".to_owned() + } +}