|
| 1 | +use std::{ |
| 2 | + fs::File, |
| 3 | + io::{BufReader, BufRead}, |
| 4 | + ops::Add, fmt::Display, |
| 5 | +}; |
| 6 | + |
| 7 | +const SNAFU_BASE: isize = 5; |
| 8 | + |
| 9 | +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Default, Clone, Copy)] |
| 10 | +enum SnafuDigit { |
| 11 | + DoubleMinus = -2, |
| 12 | + Minus = -1, |
| 13 | + #[default] |
| 14 | + Zero = 0, |
| 15 | + One = 1, |
| 16 | + Two = 2, |
| 17 | +} |
| 18 | + |
| 19 | +impl Add<Self> for SnafuDigit { |
| 20 | + type Output = Snafu; |
| 21 | + fn add(self, rhs: Self) -> Self::Output { |
| 22 | + match (self, rhs) { |
| 23 | + (Self::DoubleMinus, Self::DoubleMinus) => Snafu::new(b"-1"), |
| 24 | + (Self::DoubleMinus, Self::Minus) => Snafu::new(b"-2"), |
| 25 | + (Self::DoubleMinus, Self::Zero) => Snafu::new(b"="), |
| 26 | + (Self::DoubleMinus, Self::One) => Snafu::new(b"-"), |
| 27 | + (Self::DoubleMinus, Self::Two) => Snafu::new(b"0"), |
| 28 | + // |
| 29 | + (Self::Minus, Self::Minus) => Snafu::new(b"="), |
| 30 | + (Self::Minus, Self::Zero) => Snafu::new(b"-"), |
| 31 | + (Self::Minus, Self::One) => Snafu::new(b"0"), |
| 32 | + (Self::Minus, Self::Two) => Snafu::new(b"1"), |
| 33 | + // |
| 34 | + (Self::Zero, Self::Zero) => Snafu::new(b"0"), |
| 35 | + (Self::Zero, Self::One) => Snafu::new(b"1"), |
| 36 | + (Self::Zero, Self::Two) => Snafu::new(b"2"), |
| 37 | + // |
| 38 | + (Self::One, Self::One) => Snafu::new(b"2"), |
| 39 | + (Self::One, Self::Two) => Snafu::new(b"1="), |
| 40 | + // |
| 41 | + (Self::Two, Self::Two) => Snafu::new(b"1-"), |
| 42 | + // |
| 43 | + (x, y) => Self::add(y,x), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +impl From<&SnafuDigit> for isize { |
| 50 | + fn from(value: &SnafuDigit) -> Self { |
| 51 | + *value as isize |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl From<SnafuDigit> for isize { |
| 56 | + fn from(value: SnafuDigit) -> Self { |
| 57 | + value.into() |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl SnafuDigit { |
| 62 | + fn to_snafu(self) -> Snafu { |
| 63 | + Snafu(vec![self]) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl From<&u8> for SnafuDigit { |
| 68 | + fn from(digit: &u8) -> Self { |
| 69 | + match digit { |
| 70 | + b'2' => Self::Two, |
| 71 | + b'1' => Self::One, |
| 72 | + b'0' => Self::Zero, |
| 73 | + b'-' => Self::Minus, |
| 74 | + b'=' => Self::DoubleMinus, |
| 75 | + _ => panic!("unknown digit"), |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl Display for SnafuDigit { |
| 81 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 82 | + write!(f, "{}", match self { |
| 83 | + Self::DoubleMinus => '=', |
| 84 | + Self::Minus => '-', |
| 85 | + Self::Zero => '0', |
| 86 | + Self::One => '1', |
| 87 | + Self::Two => '2', |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// saved from LSD to MSD |
| 93 | +#[derive(Debug, PartialEq, Eq, Clone)] |
| 94 | +struct Snafu(Vec<SnafuDigit>); |
| 95 | + |
| 96 | +impl Snafu { |
| 97 | + fn new(arr: &[u8]) -> Self { |
| 98 | + Self(arr.iter().rev().map(|digit| digit.into()).collect()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl Display for Snafu { |
| 103 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 104 | + for d in self.0.iter().rev() { |
| 105 | + write!(f, "{}", d)?; |
| 106 | + } |
| 107 | + Ok(()) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl Default for Snafu { |
| 112 | + fn default() -> Self { |
| 113 | + Snafu(vec![SnafuDigit::default()]) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl Add<Self> for Snafu { |
| 118 | + type Output = Snafu; |
| 119 | + fn add(self, rhs: Self) -> Self::Output { |
| 120 | + // longest number first |
| 121 | + let (x, y) = if self.0.len() > rhs.0.len() {(&self.0,&rhs.0)} else {(&rhs.0,&self.0)}; |
| 122 | + let mut carry = Snafu(vec![]); |
| 123 | + let mut res = Snafu::new(b""); |
| 124 | + for i in 0.. { |
| 125 | + if i >= x.len() && carry.0.is_empty() { |
| 126 | + break; |
| 127 | + } |
| 128 | + |
| 129 | + carry = match (x.get(i), y.get(i), carry) { |
| 130 | + (Some(l), Some(r), carry) if carry.0.is_empty() => { |
| 131 | + *l + *r |
| 132 | + } |
| 133 | + (Some(l), Some(r), carry) => { |
| 134 | + *l + *r + carry |
| 135 | + } |
| 136 | + (Some(_), None, carry) if carry.0.is_empty() => { |
| 137 | + res.0.extend_from_slice(&x[i..]); |
| 138 | + return res |
| 139 | + } |
| 140 | + (Some(l), None, carry) => { |
| 141 | + l.to_snafu() + carry |
| 142 | + } |
| 143 | + (None, Some(_), carry) if carry.0.is_empty() => { |
| 144 | + res.0.extend_from_slice(&y[i..]); |
| 145 | + return res |
| 146 | + } |
| 147 | + (None, Some(r), carry) => { |
| 148 | + r.to_snafu() + carry |
| 149 | + } |
| 150 | + (None, None, carry) => { |
| 151 | + res.0.extend(carry.0); |
| 152 | + return res; |
| 153 | + } |
| 154 | + }; |
| 155 | + let digit = carry.0.remove(0); |
| 156 | + res.0.push(digit); |
| 157 | + } |
| 158 | + res |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl FromIterator<u8> for Snafu { |
| 163 | + fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self { |
| 164 | + let mut digits: Vec<SnafuDigit> = iter.into_iter().map(|b| (&b).into()).collect(); |
| 165 | + digits.reverse(); |
| 166 | + Self(digits) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +impl From<&Snafu> for isize { |
| 171 | + fn from(value: &Snafu) -> Self { |
| 172 | + let mut res = 0; |
| 173 | + let mut weight: isize = 1; |
| 174 | + for digit in value.0.iter() { |
| 175 | + let v = weight * (*digit) as isize; |
| 176 | + res += v; |
| 177 | + weight *= SNAFU_BASE; |
| 178 | + } |
| 179 | + res |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +impl From<Snafu> for isize { |
| 184 | + fn from(value: Snafu) -> Self { |
| 185 | + (&value).into() |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +#[allow(unused)] |
| 190 | +fn test_conversion(dec: isize, snafu: Snafu) { |
| 191 | + let res: isize = snafu.into(); |
| 192 | + assert_eq!(res, dec); |
| 193 | +} |
| 194 | + |
| 195 | +fn main() { |
| 196 | + let f = File::open("input/day25.txt").unwrap(); |
| 197 | + let read = BufReader::new(f); |
| 198 | + let lines = read.lines(); |
| 199 | + |
| 200 | + let mut acc = Snafu::default(); |
| 201 | + for line in lines { |
| 202 | + let line = line.unwrap(); |
| 203 | + let snafu: Snafu = line.bytes().collect(); |
| 204 | + |
| 205 | + acc = acc + snafu; |
| 206 | + } |
| 207 | + println!("{}", acc); |
| 208 | +} |
0 commit comments