-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution_2016_16.rs
101 lines (85 loc) · 2.25 KB
/
solution_2016_16.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use advent_of_code_common::parsing::{Error, convert_error};
use itertools::Itertools;
use num_enum::TryFromPrimitive;
use crate::Digit::{One, Zero};
#[repr(u8)]
#[derive(TryFromPrimitive, Eq, PartialEq, Copy, Clone)]
enum Digit {
Zero = b'0',
One = b'1',
}
fn parse(input: &str) -> Result<Vec<Digit>, Error> {
input
.chars()
.map(|x| convert_error(&x.to_string(), Digit::try_from(x as u8)))
.collect()
}
fn fill_to(data: &[Digit], size: usize) -> Vec<Digit> {
if data.len() > size {
data[.. size].to_vec()
} else {
let other: Vec<Digit> = data
.iter()
.rev()
.map(|d| {
match d {
Zero => One,
One => Zero,
}
})
.collect();
let new = [data.to_vec(), vec![Zero], other].concat();
fill_to(&new, size)
}
}
fn checksum(data: &[Digit]) -> Vec<Digit> {
let result: Vec<Digit> = data
.iter()
.tuples()
.map(|(a, b)| if a == b { One } else { Zero })
.collect();
if result.len() % 2 == 0 {
checksum(&result)
} else {
result
}
}
fn solve(data: &[Digit], size: usize) -> Vec<Digit> {
assert!(data.len() <= size);
let filled = fill_to(data, size);
checksum(&filled)
}
fn process(input: &str, size: usize) -> Result<String, Error> {
let result = parse(input).map(|x| solve(&x, size))?;
let result = result.iter().map(|d| *d as u8 as char).collect();
Ok(result)
}
const DATA: &str = "10010000000110000";
fn main() -> Result<(), Error> {
let result_1 = process(DATA, 272)?;
println!("Part 1: {result_1:?}");
let result_2 = process(DATA, 35_651_584)?;
println!("Part 2: {result_2:?}");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "10000";
#[test]
fn test_solve_1_test() {
assert_eq!(process(TEST, 20), Ok("01100".to_string()));
}
#[test]
fn test_solve_1_real() {
assert_eq!(process(DATA, 272), Ok("10010110010011110".to_string()));
}
#[test]
#[ignore]
fn test_solve_2_real() {
assert_eq!(
process(DATA, 35_651_584),
Ok("01101011101100011".to_string())
);
}
}