generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25.rs
67 lines (53 loc) · 1.51 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
62
63
64
65
66
67
advent_of_code::solution!(25);
fn parse_data(input: &str) -> Vec<&[u8]> {
input.lines().map(|x| x.as_bytes()).collect()
}
fn encode(n: i64) -> String {
let mut result = vec![];
while decode(&result) < n {
result.push(b'2');
}
for i in 0..result.len() {
for option in [b'=', b'-', b'0', b'1', b'2'] {
result[i] = option;
if decode(&result) >= n {
break;
}
}
}
String::from(std::str::from_utf8(&result).unwrap())
}
fn decode(s: &[u8]) -> i64 {
s.iter().fold(0, |acc, v| match v {
b'2' => 5 * acc + 2,
b'1' => 5 * acc + 1,
b'0' => 5 * acc,
b'-' => 5 * acc - 1,
b'=' => 5 * acc - 2,
_ => unreachable!(),
})
}
pub fn part_one(input: &str) -> Option<String> {
let data = parse_data(input);
let digital_sum = data.into_iter().map(decode).sum();
let result = encode(digital_sum);
Some(result)
}
pub fn part_two(_: &str) -> Option<String> {
// "Thank you Eric for another wonderful year of AoC!"
Some(String::from("⭐️⭐️"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(String::from("2=-1=0")));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(String::from("⭐️⭐️")));
}
}