2020: day10: part 1

This commit is contained in:
Antoine Martin 2020-12-10 14:09:47 +01:00
parent 35899f02f9
commit 2726134d7d
7 changed files with 209 additions and 0 deletions

View file

@ -9,6 +9,7 @@ use aoc2020::day06;
use aoc2020::day07; use aoc2020::day07;
use aoc2020::day08; use aoc2020::day08;
use aoc2020::day09; use aoc2020::day09;
use aoc2020::day10;
fn aoc2020_all(c: &mut Criterion) { fn aoc2020_all(c: &mut Criterion) {
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
@ -20,6 +21,7 @@ fn aoc2020_all(c: &mut Criterion) {
c.bench_function("day07", |b| b.iter(|| day07::run().unwrap())); c.bench_function("day07", |b| b.iter(|| day07::run().unwrap()));
c.bench_function("day08", |b| b.iter(|| day08::run().unwrap())); c.bench_function("day08", |b| b.iter(|| day08::run().unwrap()));
c.bench_function("day09", |b| b.iter(|| day09::run().unwrap())); c.bench_function("day09", |b| b.iter(|| day09::run().unwrap()));
c.bench_function("day10", |b| b.iter(|| day10::run().unwrap()));
} }
criterion_group! { criterion_group! {

97
aoc2020/input/day10.txt Normal file
View file

@ -0,0 +1,97 @@
8
131
91
35
47
116
105
121
56
62
94
72
13
82
156
102
12
59
31
138
46
120
7
127
126
111
2
123
22
69
18
157
75
149
88
81
23
98
132
1
63
142
37
133
61
112
122
128
155
145
139
66
42
134
24
60
9
28
17
29
101
148
96
68
25
19
6
67
113
55
40
135
97
79
48
159
14
43
86
36
41
85
87
119
30
108
80
152
158
151
32
78
150
95
3
52
49

View file

@ -0,0 +1,11 @@
16
10
15
5
1
11
7
19
6
12
4

View 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

65
aoc2020/src/day10.rs Normal file
View file

@ -0,0 +1,65 @@
use std::fmt::Write;
use aoc::err;
const INPUT: &str = include_str!("../input/day10.txt");
pub fn run() -> aoc::Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> aoc::Result<usize> {
let mut jolts = input
.lines()
.map(|line| {
line.parse()
.map_err(|e| err!("couldn't parse joltage: {}", e))
})
.collect::<aoc::Result<Vec<u64>>>()?;
if jolts.is_empty() {
return Err(err!("input was empty!"));
}
// charging outlet can be added
jolts.insert(0, 0);
jolts.sort_unstable();
// device is rated for max adapter + 3 jolts
let max_adapter = jolts[jolts.len() - 1];
let device_rating = max_adapter + 3;
jolts.push(device_rating);
let mut differences: [usize; 4] = [0; 4];
for (prev, next) in jolts.iter().zip(jolts.iter().skip(1)) {
let difference = (next - prev) as usize;
differences[difference] += 1;
}
Ok(differences[1] * differences[3])
}
#[cfg(test)]
mod tests {
use super::*;
const PROVIDED1: &str = include_str!("../input/day10_provided1.txt");
const PROVIDED2: &str = include_str!("../input/day10_provided2.txt");
#[test]
fn part1_provided() {
assert_eq!(part1(PROVIDED1).unwrap(), 7 * 5);
assert_eq!(part1(PROVIDED2).unwrap(), 22 * 10);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 2112);
}
}

View file

@ -7,3 +7,4 @@ pub mod day06;
pub mod day07; pub mod day07;
pub mod day08; pub mod day08;
pub mod day09; pub mod day09;
pub mod day10;

View file

@ -10,6 +10,7 @@ use aoc2020::day06;
use aoc2020::day07; use aoc2020::day07;
use aoc2020::day08; use aoc2020::day08;
use aoc2020::day09; use aoc2020::day09;
use aoc2020::day10;
fn main() -> Result<()> { fn main() -> Result<()> {
let days: &[DayFunc] = &[ let days: &[DayFunc] = &[
@ -22,6 +23,7 @@ fn main() -> Result<()> {
day07::run, day07::run,
day08::run, day08::run,
day09::run, day09::run,
day10::run,
]; ];
aoc::run(days) aoc::run(days)