#
Day 10 (Advent of Code 2020)
From the series
Advent of Code 2020

Day, 10! Day, 10!

Okay, Day 10.

Again, the problem statement is very confusing - but what it all boils down to is this. We have a list of numbers:

16 10 15 5 1 11 7 19 6 12 4

To which we need to add `0`

and whatever the maximum was, plus three:

16 10 15 5 1 11 7 19 6 12 4 0 22

From there on, if we take them in order, we'll have gaps of 1 and gaps of 3:

0 1 // 1 4 // 3 5 // 1 6 // 1 7 // 1 10 // 3 11 // 1 12 // 1 15 // 3 16 // 1 19 // 3 22 // 3

And we need to multiply the amount of 1-gaps with the amount of 3-gaps we find.

We can use some of the techniques we've very recently seen: use some types
to represent the results, use `windows`

on a slice to deal with contiguous
sets of items, and a couple new tricks:

#[derive(Default,Clone,Copy,Debug)]structResults{ones:usize,threes:usize,}fnmain(){letmutnumbers:Vec<_>= std::iter::once(0).chain(include_str!("input.txt").lines().map(|x| x.parse::<usize>().unwrap()),).collect();// clippy told me to use `sort_unstable`numbers.sort_unstable();ifletSome(&max)= numbers.iter().max(){// numbers is still sorted after thisnumbers.push(max +3);}letresults = numbers.windows(2).fold(Results::default(),|acc,s|{iflet[x,y]= s{matchy - x{1=>Results{ones:acc.ones+1,..acc},3=>Results{threes:acc.threes+1,..acc},gap =>panic!("invalid input (found {} gap)",gap),}}else{unreachable!()}});dbg!(results,results.ones*results.threes);}

Note that if we used Rust nightly, we could use `array_windows`

instead, which
would give us an `[usize; 2]`

and then we wouldn't need an `if let`

because it
would be an irrefutable pattern!

$ cargo run --quiet [src/main.rs:40] results = Results { ones: 7, threes: 5, } [src/main.rs:40] results.ones * results.threes = 35

Let's try it with the real input:

$ cargo run --quiet [src/main.rs:40] results = Results { ones: 75, threes: 37, } [src/main.rs:40] results.ones * results.threes = 2775

And we're done with the first part!

### Part 2

The next problem is: what are all the possible ways in which we can connect our adapters? All the valid chains?

Say we have:

1 2 3 5 6

We can have `[1, 2, 3, 5, 6]`

, `[1, 2, 3, 6]`

, `[1, 2, 5, 6]`

, `[1, 3, 5, 6]`

,
or `[1, 3, 6]`

.

Just like in Day 7, we pretty much have a DAG here: