Skip to main content

aoc/year2020/
day10.rs

1//! # Adapter Array
2//!
3//! Part one uses the [`windows`] function to iterate over all pairs counting the differences.
4//!
5//! Part two is a classic [dynamic programming](https://en.wikipedia.org/wiki/Dynamic_programming)
6//! problem. The charging outlet can be arranged in exactly one way. Each subsequent adapter can be
7//! arranged in the sum of the number of ways that the adapters at -1, -2 and -3 jolts can be
8//! arranged.
9//!
10//! [`windows`]: slice::windows
11use crate::util::parse::*;
12
13pub fn parse(input: &str) -> Vec<usize> {
14    let mut adapters: Vec<_> = input.iter_unsigned().collect();
15    adapters.sort_unstable();
16    adapters
17}
18
19pub fn part1(input: &[usize]) -> usize {
20    let mut total = [0, 0, 0, 1];
21    total[input[0]] += 1;
22
23    for &[a, b] in input.array_windows() {
24        total[b - a] += 1;
25    }
26
27    total[1] * total[3]
28}
29
30pub fn part2(input: &[usize]) -> usize {
31    let last = *input.last().unwrap();
32    let mut sum = vec![0; last + 1];
33    sum[0] = 1;
34
35    for &i in input {
36        sum[i] = match i {
37            1 => sum[i - 1],
38            2 => sum[i - 1] + sum[i - 2],
39            _ => sum[i - 1] + sum[i - 2] + sum[i - 3],
40        };
41    }
42
43    sum[last]
44}