advent-of-code/2020/src/bin/day10.rs

95 lines
1.5 KiB
Rust
Raw Normal View History

2020-12-10 10:56:13 +01:00
#![feature(test)]
extern crate test;
2020-12-10 14:44:42 +01:00
use aoc2020::common::*;
2020-12-10 10:56:13 +01:00
use itertools::Itertools;
type Parsed = Vec<usize>;
2020-12-10 11:05:02 +01:00
const STEP_SIZE: usize = 3;
2020-12-10 10:56:13 +01:00
fn read_input() -> String {
2020-12-10 14:44:42 +01:00
read_file(10)
2020-12-10 10:56:13 +01:00
}
fn parse_input(raw: &str) -> Parsed {
let mut xs: Vec<usize> = raw.lines().map(|l| l.parse().unwrap()).collect();
2020-12-10 11:05:02 +01:00
xs.push(0);
2020-12-10 10:56:13 +01:00
// faster than using sorted() directly on the iterator
xs.sort_unstable();
xs
}
fn part1(input: &Parsed) -> (usize, usize) {
2020-12-10 11:05:02 +01:00
input.iter().tuple_windows().fold((0, 1), |(one, three), (a, b)| match b - a {
1 => (one + 1, three),
2 => (one, three),
3 => (one, three + 1),
_ => unreachable!(),
})
2020-12-10 10:56:13 +01:00
}
fn part2(input: &Parsed) -> usize {
2020-12-10 11:05:02 +01:00
let mut iter = input.iter().rev();
let max = *iter.next().unwrap();
let mut paths = vec![0; max + STEP_SIZE];
2020-12-10 10:56:13 +01:00
paths[max] = 1;
2020-12-10 11:05:02 +01:00
for &i in iter {
paths[i] = (1..=STEP_SIZE).map(|j| paths[i + j]).sum();
2020-12-10 10:56:13 +01:00
}
2020-12-10 11:05:02 +01:00
paths[0]
2020-12-10 10:56:13 +01:00
}
fn main() {
let input = parse_input(&read_input());
let (ones, threes) = part1(&input);
println!("Part 1: {}", ones * threes);
println!("Part 2: {}", part2(&input));
}
#[cfg(test)]
mod tests {
use super::*;
2020-12-10 14:01:25 +01:00
use aoc2020::*;
use paste::paste;
2020-12-10 10:56:13 +01:00
use test::black_box;
const TEST_INPUT: &str = "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";
2020-12-10 16:41:54 +01:00
test!(part1() == (22, 10));
test!(part2() == 19208);
bench!(part1() == (69, 24));
bench!(part2() == 56693912375296);
2020-12-10 14:01:25 +01:00
bench_input!(len == 93);
2020-12-10 10:56:13 +01:00
}