commit ff2b7698d75978b2e96e6b0f59c536ecf8de6cb5
parent d78541c47c0b2b2a977a8a23114541bbbf7a0556
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Thu, 10 Jul 2025 08:33:07 +0200
Merry Christmas 2022
Diffstat:
4 files changed, 239 insertions(+), 0 deletions(-)
diff --git a/2022/25/a.rs b/2022/25/a.rs
@@ -0,0 +1,13 @@
+mod common;
+use common::*;
+
+fn main() {
+ let mut line = String::new();
+ let mut sum = Snafu::zero();
+ while std::io::stdin().read_line(&mut line).unwrap() > 0 {
+ let s = Snafu::from_str(&line[..line.len()-1]);
+ sum += &s;
+ line.clear();
+ }
+ println!("{sum}");
+}
diff --git a/2022/25/common.rs b/2022/25/common.rs
@@ -0,0 +1,72 @@
+use std::fmt;
+use std::ops;
+use std::cmp;
+
+pub struct Snafu {
+ d: Vec<i8>
+}
+
+impl Snafu {
+ const DIGITS: [char; 5] = ['=', '-', '0', '1', '2'];
+
+ fn digit_to_i8(c: char) -> i8 {
+ for i in 0..5 {
+ if c == Snafu::DIGITS[i] { return i as i8 - 2; }
+ }
+ panic!("Invalid Snafu digit '{}'", c);
+ }
+
+ fn i8_to_digit(i: i8) -> char {
+ if i < -2 || i > 2 {
+ panic!("Invalid Snafu digit value {}", i);
+ }
+ Snafu::DIGITS[(i + 2) as usize]
+ }
+
+ pub fn ndigits(&self) -> usize {
+ self.d.len()
+ }
+
+ pub fn zero() -> Snafu {
+ Snafu { d: vec![0] }
+ }
+
+ pub fn from_str(s: &str) -> Snafu {
+ let mut d = vec![];
+ for c in s.chars() { d.push(Self::digit_to_i8(c)); }
+ d.reverse();
+ Snafu { d }
+ }
+}
+
+impl fmt::Display for Snafu {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ // Strip leading zeros
+ let mut n = self.ndigits()-1;
+ while self.d[n] == 0 { n -= 1; }
+
+ for i in (0..=n).rev() {
+ write!(f, "{}", Self::i8_to_digit(self.d[i]))?;
+ }
+ Ok(())
+ }
+}
+
+impl ops::AddAssign<&Snafu> for Snafu {
+ fn add_assign(&mut self, other: &Snafu) {
+ for i in 0..cmp::max(self.ndigits(), other.ndigits()) {
+ if i < other.ndigits() {
+ self.d[i] += other.d[i];
+ }
+
+ // Carry over, both ways
+ if i == self.ndigits()-1 { self.d.push(0); }
+ while self.d[i] > 2 { self.d[i+1] += 1; self.d[i] -= 5; }
+ while self.d[i] < -2 { self.d[i+1] -= 1; self.d[i] += 5; }
+ }
+
+ for i in 0..self.ndigits() {
+ if self.d[i] == 0 { self.d.pop(); } else { break; }
+ }
+ }
+}
diff --git a/2022/25/input b/2022/25/input
@@ -0,0 +1,141 @@
+121=10120=1022=2=0
+10==--=-=2
+2=11
+120221-1-
+2002011
+1-00-11=0
+1==2=-200101===
+2122=-10
+1=1=2=-1
+11--=-10-0-0
+2=-210=
+2-==1221011=021==-
+2
+1-2==212
+1-2=0=12--0111010=0
+1220-20=1=1
+1211=1
+10-22=-212--00
+1=---021
+1-010-21-2-
+10-==00122-=0--
+10=-
+1=20=-=---1=1
+1-0-0
+2===11=12200=2
+1===1
+1002
+10=-==-02
+12---2-==-020122
+11=-=2
+1-==2-=--2
+1-1112
+210-212-=20220-1
+1=-0111=0==
+100=20==-
+200==22-2=
+2-
+21-=1012=2=01=
+1=-=0=0-01=0
+1-2
+1121==0=
+2--21
+212111=00=-1-=1=
+10-021-
+2=
+2=0=12-
+1-0--
+2-0=21012==112=-1=
+1222==01=100121=0-=
+1011=01
+12102-
+10012=0-11021
+1-=01-
+12=010=12-0=02=11
+1-22=2--1-
+100=010-1
+210
+1-
+22--011--1212101
+11201
+1=2=-122=---1
+1===2122=-01=0=2
+1011--0--=20
+202-2-1=2
+2=000=10=-0
+110-=00-1211
+2==0=--00-2===
+110
+2=-02-1220012-
+1=21----=-=
+1=1
+12=--2=012-21
+1=0020-1--=0-=
+1==
+11-01101221=-02
+1=0201-2==0--01
+1=120120-=1
+1-121=====2-0=-
+201=1=1101-=02==
+1-00--0-
+10-202
+22-
+1==1=-2==-0-110
+1==21=1=20-1210=12
+20112-1=0--0=
+1=-=2021--0===21
+1-==-2
+1==120-0-
+1=0111=21-00-0=2
+1-110-=11-1021
+22
+1=1212=20=
+212=1=-12---122
+2=2
+2220-1=2==022=--0=
+2-02222
+1--0=0-22=-1
+120==2100
+10==201121==2-1221-0
+12210--==2--01022
+211120-21202
+22021011
+21---0=2-10=22
+1-22=-0
+12201=-2121-=-1
+11122-=202-
+1-2---=
+1-22=01=10-10
+1-0=0=21211011-0-
+2---
+1==12100
+112=1121
+120--1
+1-11=-2=-0
+1--=02-02
+2000-22==0221=2=
+1=1-10-1-==100=1
+202-011011
+20=10=-0002=00=
+2=1-2
+220
+11==-0=120--0-10=
+1=112-22202=2
+10=-0
+1=1=2=0
+2222--11---011-1
+1=-1=2=1-2122--10-1
+11021
+101
+11=2101222==
+100112-2011
+1=1=
+2-021--
+12211-1122=2-
+2--2=-1=-
+21012=2---2=2120=1
+1-010-==--2
+12=-22000-
+2-===1-1
+22=-=122=--1=
+2-2=-0-021
diff --git a/2022/25/test b/2022/25/test
@@ -0,0 +1,13 @@
+1=-0-2
+12111
+2=0=
+21
+2=01
+111
+20012
+112
+1=-1=
+1-12
+12
+1=
+122