-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.rs
61 lines (52 loc) · 1.21 KB
/
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
/*
* Day 25: Full of Hot Air
* See [https://adventofcode.com/2022/day/25]
*/
fn parse_line(line: &str) -> isize {
let mut sum = 0;
for l in line.bytes() {
let lp = match l {
b'0' => 0,
b'1' => 1,
b'2' => 2,
b'-' => -1,
b'=' => -2,
x => panic!("Invalid character {}", x as char)
};
sum *= 5;
sum += lp;
}
sum
}
const NUM_CH: [char; 5] = ['0', '1', '2', '=', '-'];
fn fmt_num(num: isize) -> String {
let mut wn = num;
let mut chv = Vec::new();
while wn > 0 {
let dp = wn % 5;
chv.push(NUM_CH[dp as usize]);
if dp > 2 {
wn += 5 - dp;
}
wn /= 5;
}
chv.into_iter().rev().collect()
}
pub fn part_1(input: &str) -> Option<String> {
let num = input.lines().map(parse_line).sum();
Some(fmt_num(num))
}
pub fn part_2(_: &str) -> Option<u32> {
// Part 2 not available for this day
Some(0)
}
aoc2022::solve!(part_1, part_2);
#[cfg(test)]
mod tests {
use aoc2022::assert_ex;
use super::*;
#[test]
fn test_part_1() { assert_ex!(part_1, "2=-1=0"); }
//#[test]
//fn test_part_2() { assert_ex!(part_2, 0); }
}