mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Implementation day 10
This commit is contained in:
100
2020/inputs/10.txt
Normal file
100
2020/inputs/10.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
30
|
||||
73
|
||||
84
|
||||
136
|
||||
132
|
||||
117
|
||||
65
|
||||
161
|
||||
49
|
||||
68
|
||||
139
|
||||
46
|
||||
21
|
||||
127
|
||||
109
|
||||
153
|
||||
163
|
||||
160
|
||||
18
|
||||
22
|
||||
131
|
||||
146
|
||||
62
|
||||
113
|
||||
172
|
||||
150
|
||||
171
|
||||
98
|
||||
93
|
||||
130
|
||||
170
|
||||
59
|
||||
1
|
||||
110
|
||||
2
|
||||
55
|
||||
37
|
||||
44
|
||||
148
|
||||
102
|
||||
40
|
||||
28
|
||||
35
|
||||
43
|
||||
56
|
||||
169
|
||||
33
|
||||
5
|
||||
141
|
||||
83
|
||||
15
|
||||
105
|
||||
142
|
||||
36
|
||||
116
|
||||
11
|
||||
45
|
||||
82
|
||||
10
|
||||
17
|
||||
159
|
||||
140
|
||||
12
|
||||
108
|
||||
29
|
||||
72
|
||||
121
|
||||
52
|
||||
91
|
||||
166
|
||||
88
|
||||
97
|
||||
118
|
||||
99
|
||||
124
|
||||
149
|
||||
16
|
||||
9
|
||||
143
|
||||
104
|
||||
57
|
||||
79
|
||||
123
|
||||
58
|
||||
96
|
||||
24
|
||||
162
|
||||
23
|
||||
92
|
||||
69
|
||||
147
|
||||
156
|
||||
25
|
||||
133
|
||||
34
|
||||
8
|
||||
85
|
||||
76
|
||||
103
|
||||
122
|
||||
31
2020/samples/10.2.txt
Normal file
31
2020/samples/10.2.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
28
|
||||
33
|
||||
18
|
||||
42
|
||||
31
|
||||
14
|
||||
46
|
||||
20
|
||||
48
|
||||
47
|
||||
24
|
||||
23
|
||||
49
|
||||
45
|
||||
19
|
||||
38
|
||||
39
|
||||
11
|
||||
1
|
||||
32
|
||||
25
|
||||
35
|
||||
8
|
||||
17
|
||||
7
|
||||
9
|
||||
4
|
||||
2
|
||||
34
|
||||
10
|
||||
3
|
||||
11
2020/samples/10.txt
Normal file
11
2020/samples/10.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
16
|
||||
10
|
||||
15
|
||||
5
|
||||
1
|
||||
11
|
||||
7
|
||||
19
|
||||
6
|
||||
12
|
||||
4
|
||||
71
2020/src/day10.rs
Normal file
71
2020/src/day10.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use std::io::Read;
|
||||
|
||||
use crate::common::from_lines;
|
||||
use crate::Solution;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Day10;
|
||||
|
||||
impl Solution for Day10 {
|
||||
fn part1(&mut self, input: &mut dyn Read) -> String {
|
||||
let mut adapters: Vec<u32> = from_lines(input);
|
||||
// Outlet
|
||||
adapters.push(0);
|
||||
adapters.sort();
|
||||
let device = *adapters.last().unwrap() + 3;
|
||||
adapters.push(device);
|
||||
|
||||
let mut differences = [0u32; 4];
|
||||
|
||||
for window in adapters.windows(2) {
|
||||
differences[(window[1] - window[0]) as usize] += 1;
|
||||
}
|
||||
|
||||
(differences[1] * differences[3]).to_string()
|
||||
}
|
||||
|
||||
fn part2(&mut self, input: &mut dyn Read) -> String {
|
||||
let mut adapters: Vec<u32> = from_lines(input);
|
||||
adapters.push(0);
|
||||
adapters.sort();
|
||||
|
||||
let mut methods = vec![0u64; adapters.len()];
|
||||
methods[0] = 1;
|
||||
|
||||
for (i, a) in adapters.iter().copied().enumerate() {
|
||||
let c = methods[i];
|
||||
|
||||
for (j, b) in adapters[i..].iter().enumerate().skip(1) {
|
||||
if b - a <= 3 {
|
||||
methods[i + j] += c;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
methods.last().unwrap().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test_implementation;
|
||||
|
||||
use super::*;
|
||||
|
||||
const SAMPLE: &[u8] = include_bytes!("../samples/10.txt");
|
||||
const SAMPLE2: &[u8] = include_bytes!("../samples/10.2.txt");
|
||||
|
||||
#[test]
|
||||
fn sample_part1() {
|
||||
test_implementation!(Day10, 1, SAMPLE, 35);
|
||||
test_implementation!(Day10, 1, SAMPLE2, 220);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_part2() {
|
||||
test_implementation!(Day10, 2, SAMPLE, 8);
|
||||
test_implementation!(Day10, 2, SAMPLE2, 19208);
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ mod day06;
|
||||
mod day07;
|
||||
mod day08;
|
||||
mod day09;
|
||||
mod day10;
|
||||
|
||||
pub trait Solution {
|
||||
fn part1(&mut self, input: &mut dyn Read) -> String;
|
||||
@@ -30,6 +31,7 @@ pub fn get_implementation(day: usize) -> Box<dyn Solution> {
|
||||
7 => Box::new(day07::Day07::default()),
|
||||
8 => Box::new(day08::Day08::default()),
|
||||
9 => Box::new(day09::Day09::default()),
|
||||
10 => Box::new(day10::Day10::default()),
|
||||
_ => panic!("Unsupported day {}", day),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user