-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_25.rs
78 lines (64 loc) · 1.51 KB
/
day_25.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use common::{Answer, Solution};
pub struct Day25;
impl Solution for Day25 {
fn name(&self) -> &'static str {
"Full of Hot Air"
}
fn part_a(&self, input: &str) -> Answer {
snafu::encode(input.lines().map(snafu::decode).sum::<i64>()).into()
}
fn part_b(&self, _input: &str) -> Answer {
// No part b for day 25!
Answer::Unimplemented
}
}
mod snafu {
pub fn decode(s: &str) -> i64 {
let mut value = 0;
for (i, c) in s.chars().rev().enumerate() {
value += match c {
'0'..='2' => c as i64 - '0' as i64,
'-' => -1,
'=' => -2,
_ => panic!("Invalid character"),
} * 5_i64.pow(i as u32);
}
value
}
pub fn encode(real: i64) -> String {
let mut out = String::new();
let mut num = real;
while num > 0 {
let index = (num % 5) as usize;
out.push("012=-".as_bytes()[index] as char);
num -= [0, 1, 2, -2, -1][index];
num /= 5;
}
out.chars().rev().collect::<String>()
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
use super::Day25;
use common::Solution;
const CASE: &str = indoc! {r"
1=-0-2
12111
2=0=
21
2=01
111
20012
112
1=-1=
1-12
12
1=
122
"};
#[test]
fn part_a() {
assert_eq!(Day25.part_a(CASE), "2=-1=0".into());
}
}