This commit is contained in:
Stefan Bühler 2020-12-12 11:21:28 +01:00
parent be899a9b97
commit ecc548a987
2 changed files with 143 additions and 0 deletions

104
data/day10/input Normal file
View File

@ -0,0 +1,104 @@
79
142
139
33
56
133
138
61
125
88
158
123
65
69
105
6
81
31
60
70
159
114
71
15
13
72
118
14
9
93
162
140
165
1
80
148
32
53
102
5
68
101
111
85
45
25
16
59
131
23
91
92
115
103
166
22
145
161
108
155
135
55
86
34
37
78
28
75
7
104
121
24
153
167
95
87
94
134
154
84
151
124
62
49
38
39
54
109
128
19
2
98
122
132
141
168
8
160
50
42
46
110
12
152

39
src/bin/day10.rs Normal file
View File

@ -0,0 +1,39 @@
const INPUT: &str = include_str!("../../data/day10/input");
fn calc(input: &[u32], cache: &mut [Option<u64>], ndx: usize) -> u64 {
if let Some(n) = cache[ndx] {
n
} else {
let cur = input[ndx];
let mut n = 0;
for next in ndx+1..input.len() {
if input[next] - cur > 3 { break; }
n += calc(input, cache, next);
}
cache[ndx] = Some(n);
n
}
}
fn main() {
let mut input: Vec<u32> = INPUT.lines().map(|s| s.parse().unwrap()).collect();
input.sort();
let mut prev = 0;
let mut count = [0, 0, 0];
{
for &j in &input {
count[(j - prev - 1) as usize] += 1;
prev = j;
}
}
count[2] += 1; // device always has diff of 3.
println!("#(joltage diff 1) * #(joltage diff 3): {}", count[2] * count[0]);
// insert outlet as first element
input.insert(0, 0);
let mut cache: Vec<Option<u64>> = input.iter().map(|_| None).collect();
cache[input.len()-1] = Some(1); // always need last adapter
println!("Combinations: {}", calc(&input, &mut cache, 0));
}