From 4636c264b8547acfec3bf7cf0579537f1997cadc Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sun, 25 Dec 2022 21:26:14 +0000 Subject: day25pt1 --- input/day25.txt | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ input/day25_ex.txt | 13 +++++++ src/day25.rs | 73 ++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 ++-- 4 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 input/day25.txt create mode 100644 input/day25_ex.txt create mode 100644 src/day25.rs diff --git a/input/day25.txt b/input/day25.txt new file mode 100644 index 0000000..4118043 --- /dev/null +++ b/input/day25.txt @@ -0,0 +1,100 @@ +1=120=22222-=10-= +1--22-=- +21 +1=0222=-1=-0--220=02 +122102 +22--=20==2===201 +100-1-01=0-1 +20200-2==220120- +2220-2200=000= +1==002=221-2 +2--221-22= +1022=121=1-22=-01 +2=1=1-1=02-=02=2= +1-21112=02 +2=1-22021 +1---=-221=-22122 +10020 +1=--101 +1=12=02-1--12 +1=0-=1002020-2= +120=1==201102==2== +2==-=0120112=2-=- +1---1 +12=2--02-00==-22 +12==1-=0-2=0--11-- +1=10=0-1221 +1=201-0- +22 +10-0=21-2-1-2-=211 +1=1--12==0=--1 +2=-1022=1=-10- +12-02-2- +1=-1=00--- +1=100120=101 +112=01 +20-22=00-=1=1=10 +200--=0-221101= +10-2=01=-20 +10=11=1 +20-1- +2=012-002-02-1221 +1==1=1-012100 +10=221202-0-0- +1-0121- +2==1100-= +102=022=202 +2=2- +21-2=12-22-1 +1-2100 +1-200101==0 +2==0200-12012-=1=02 +1=1010-2=22=2 +1-2=-12-2-2=112=120 +1=-=- +2-= +100=0-1-1=000 +11=1-=2--00=0=2-=1 +1221120122=11110- +111=10-002= +20=-=200 +2==21 +1=--2=-=2-102211 +20=1=-0=1= +12 +202=-0-=1220-0 +1=0112=1-=-1 +21-=-===0=-122000 +1= +112010-11221=-00 +112-2==102=2-1=1 +1==-0= +1=01-11-211 +21=-1-20--2=202122 +1=01 +20221 +100 +10=22-1=-1121-2 +2-0=- +101012-200=010= +1011=0011 +1-0202=0-11 +101==10 +10= +10111-1=-0 +1112=0101=- +11=0=-1111-000 +12=11 +1102222=20 +2=0=--==022- +11222 +2100= +1=-0 +12-11-2=-222- +1-10-012=21-0 +1-2= +112 +111=20 +1221=-2 +1===1==0010=0- +1-=1-==-=20 \ No newline at end of file diff --git a/input/day25_ex.txt b/input/day25_ex.txt new file mode 100644 index 0000000..237ef0c --- /dev/null +++ b/input/day25_ex.txt @@ -0,0 +1,13 @@ +1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 \ No newline at end of file diff --git a/src/day25.rs b/src/day25.rs new file mode 100644 index 0000000..2d26028 --- /dev/null +++ b/src/day25.rs @@ -0,0 +1,73 @@ +fn from_snafu(s: &str) -> i64 { + // This is the easy bit... + let mut i: i64 = 0; + let mut pow: i64 = 1; + for ch in s.chars().rev() { + match ch { + '2' => i += 2 * pow, + '1' => i += 1 * pow, + '0' => {}, // additive identity! + '-' => i += -1 * pow, + '=' => i += -2 * pow, + _ => panic!("unexpected input {}", ch) + } + pow *= 5; + } + i +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_from_snafu() { + let cases = vec![ + ("1=-0-2", "1747"), + ( "12111", "906"), + ( "2=0=", "198"), + ( "21", "11"), // works + ( "2=01", "201"), // 201,1 > 40,0 > 8,= > 1,1 + ( "111", "31"), // works + ( "20012", "1257"), //works + ( "112", "32"), // works + ( "1=-1=", "353"), // broke + ( "1-12", "107"), + ( "12", "7"), + ( "1=", "3"), + ( "122", "37"), + ]; + for (snafu, decimal) in cases { + let x = from_snafu(snafu); + assert_eq!(x.to_string().as_str(), decimal); + assert_eq!(to_snafu(x), snafu); + } + } +} + +fn to_snafu(mut i: i64) -> String { + // how to do this :thinking: add two, mod 5,take away 2? // nope + let mut res = Vec::::new(); + while i != 0 { + let d = ((i + 2) % 5) - 2; + let ch = match d { + 2 => '2', + 1 => '1', + 0 => '0', + -1 => '-', + -2 => '=', + _ => panic!("unexpected digit {}", d) + }; + res.push(ch); + i = (i + 2) / 5; + } + res.reverse(); + res.into_iter().collect::() + +} + +pub fn run(input: String) { + let s: i64 = input.lines() + .map(from_snafu) + .sum(); + println!("Day 25: {}", to_snafu(s)); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e865636..886b86c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,8 @@ use std::fs; // mod day15; // mod day16; //mod day17; -mod day18; +//mod day18; +mod day25; fn main() { // day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!")); @@ -40,5 +41,6 @@ fn main() { // day15::run(fs::read_to_string("input/day15.txt").expect("Failed to read input file!")); // day16::run(fs::read_to_string("input/day16.txt").expect("Failed to read input file!")); //day17::run(fs::read_to_string("input/day17.txt").expect("Failed to read input file!")); - day18::run(fs::read_to_string("input/day18.txt").expect("Failed to read input file!")); + //day18::run(fs::read_to_string("input/day18.txt").expect("Failed to read input file!")); + day25::run(fs::read_to_string("input/day25.txt").expect("Failed to read input file!")); } -- cgit v1.2.3-ZIG