commit a5c30adfca625d690dae868b73f7d1dd3bd776b8
parent 9dc669266945d536458f4c546405e6c55e759874
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Sun, 25 Dec 2022 08:30:50 -0500
Add 2022 day 25
Diffstat:
3 files changed, 213 insertions(+), 0 deletions(-)
diff --git a/2022/day25/Cargo.toml b/2022/day25/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "day25"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/2022/day25/input.txt b/2022/day25/input.txt
@@ -0,0 +1,125 @@
+1100-
+11-1=12012=
+111==2=-022=01
+20---
+1=01=20
+2=2=2=-21-=
+1021-02=10==-
+1-22=12001-0=222011
+1=1-1
+2-2=
+211=211=22-=0=10
+111-111=212--01
+10-2-121
+10=-210000
+12=
+1==2-22--==01-20-0
+1-1==120
+201=120100=1
+1==2111=1=
+1--0-=-01
+2
+2=2
+1=-=0==010-=--021
+1=-2-=1-2=00122-10
+210--21--
+112
+1=0-22-10-2=1==0
+12-12
+1==01-=-
+1-1
+1=1
+11=20102-001=0=-20
+11=11==10
+22210=-=
+12001=-0
+1=-00122200=
+20==-00=2=201=-=
+1-21101021-====-
+2-2=2=0=1122
+10=2
+1-02-10
+21220--=-01--1-1
+1==02-02
+221-=0
+110
+2=122-
+21-0=1=-
+20
+2---2-2-01-1=1110
+2-11-211
+12-110022-
+10101-20-
+211-2211-10-2002
+2--22==1=1122=-0000
+1=0
+10-
+20===21=00222==1
+21-110-11=1
+12--1=02-121
+1101=00--2==0
+11=0-1
+10101-1=21-1121100
+1=1010=-222-
+22=2200---2=2-02-=2
+20=1021=2--2---=
+10=20112
+1-0-10-
+1002002=-==0
+220-110
+1=--=21
+10--0
+122==2-1-
+1-2=100-0-=101
+2=1
+21=0=111=11=000=-2
+12121=0
+1-210-0=
+2-1=111=20-1-2-
+1012-2-02=
+1121=2=1==
+1=101-
+1-21110
+1=2011-0-
+1==0
+1-==2==12011-
+11-2=121==
+20-121002=021==111
+22220-2-02=22=
+221101
+22-=20=02-0-1=-11
+1-=01=010-1-1101-0
+1-1-=0==0=010
+1-212211-=02-11=-
+1=01=
+2000=10=-122=00
+1=1=21-=-
+1-=011=--2-2=12
+2000000=2212-00
+22120-11-1-0-=-2
+112=1---21-0111
+12112220=110
+121=0011-=1-0
+222=-=02121
+2-000010221=
+1=0221=1==-=2010
+1102====
+210=1-02-=1102
+2=-=-201102012
+2-=21-=0=1
+11022-00-2=1210
+1120-0-1012001
+2-
+10==0--0-020110
+1-=02122-0=0=
+1=10=2002=120=1-1=
+1-=11
+1-11201102-
+2-==11102-101
+10
+2-=2=
+2==0-
+111=--12=2
+1=201
+11200-1==2=001=--2
+10---10--=0-2202=1
diff --git a/2022/day25/src/main.rs b/2022/day25/src/main.rs
@@ -0,0 +1,80 @@
+fn main() {
+ let input = std::fs::read_to_string("input.txt")
+ .unwrap()
+ .trim()
+ .split('\n')
+ .map(|s| s.to_string())
+ .collect::<Vec<String>>();
+ // 2==0=0===02--210---1
+ println!("Part 1: {}", part_1(&input));
+}
+
+fn snafu_to_decimal(s: &str) -> i64 {
+ let mut res = 0;
+ let mut pow = 1;
+ for c in s.chars().rev() {
+ res += pow
+ * match c {
+ '2' => 2,
+ '1' => 1,
+ '0' => 0,
+ '-' => -1,
+ '=' => -2,
+ _ => unreachable!(),
+ };
+ pow *= 5;
+ }
+ res
+}
+
+fn decimal_to_snafu(x: i64) -> String {
+ // assume positive number
+ let mut pow = 1;
+ while pow * 5 < x {
+ pow *= 5;
+ }
+ let mut res = x;
+ let mut digits_5 = Vec::new();
+ while pow > 0 {
+ let d = res / pow;
+ digits_5.push(d);
+ res -= d * pow;
+ pow /= 5;
+ }
+
+ let mut digits_snafu = Vec::new();
+ let mut carry = 0;
+ for mut d in digits_5.into_iter().rev() {
+ d += carry;
+ match d {
+ 0 | 1 | 2 => {
+ carry = 0;
+ }
+ 3 | 4 | 5 => {
+ d = d - 5;
+ carry = 1;
+ }
+ _ => unreachable!(),
+ }
+ digits_snafu.insert(0, d);
+ }
+ if carry != 0 {
+ digits_snafu.insert(0, carry);
+ }
+ digits_snafu
+ .into_iter()
+ .map(|d| match d {
+ 2 => '2',
+ 1 => '1',
+ 0 => '0',
+ -1 => '-',
+ -2 => '=',
+ _ => unreachable!(),
+ })
+ .collect()
+}
+
+fn part_1(input: &Vec<String>) -> String {
+ let sum = input.iter().map(|s| snafu_to_decimal(s)).sum::<i64>();
+ decimal_to_snafu(sum)
+}