-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_25.rs
69 lines (57 loc) · 1.34 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
use common::{solution, Answer};
solution!("Full of Hot Air", 25);
fn part_a(input: &str) -> Answer {
snafu::encode(input.lines().map(snafu::decode).sum::<i64>()).into()
}
fn part_b(_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;
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!(super::part_a(CASE), "2=-1=0".into());
}
}