Added Solution for 2020 day 10
This commit is contained in:
parent
efaa7c5ef6
commit
604a19e901
5 changed files with 390 additions and 0 deletions
8
2020/day10_adapter_array/Cargo.toml
Normal file
8
2020/day10_adapter_array/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day10_adapter_array"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
160
2020/day10_adapter_array/challenge.txt
Normal file
160
2020/day10_adapter_array/challenge.txt
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
Patched into the aircraft's data port, you discover weather forecasts of a massive tropical storm. Before you can figure out whether it will impact your vacation plans, however, your device suddenly turns off!
|
||||||
|
|
||||||
|
Its battery is dead.
|
||||||
|
|
||||||
|
You'll need to plug it in. There's only one problem: the charging outlet near your seat produces the wrong number of *jolts*. Always prepared, you make a list of all of the joltage adapters in your bag.
|
||||||
|
|
||||||
|
Each of your joltage adapters is rated for a specific *output joltage* (your puzzle input). Any given adapter can take an input `1`, `2`, or `3` jolts *lower* than its rating and still produce its rated output joltage.
|
||||||
|
|
||||||
|
In addition, your device has a built-in joltage adapter rated for *`3` jolts higher* than the highest-rated adapter in your bag. (If your adapter list were `3`, `9`, and `6`, your device's built-in adapter would be rated for `12` jolts.)
|
||||||
|
|
||||||
|
Treat the charging outlet near your seat as having an effective joltage rating of `0`.
|
||||||
|
|
||||||
|
Since you have some time to kill, you might as well test all of your adapters. Wouldn't want to get to your resort and realize you can't even charge your device!
|
||||||
|
|
||||||
|
If you *use every adapter in your bag* at once, what is the distribution of joltage differences between the charging outlet, the adapters, and your device?
|
||||||
|
|
||||||
|
For example, suppose that in your bag, you have adapters with the following joltage ratings:
|
||||||
|
|
||||||
|
```
|
||||||
|
16
|
||||||
|
10
|
||||||
|
15
|
||||||
|
5
|
||||||
|
1
|
||||||
|
11
|
||||||
|
7
|
||||||
|
19
|
||||||
|
6
|
||||||
|
12
|
||||||
|
4
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
With these adapters, your device's built-in joltage adapter would be rated for `19 + 3 = *22*` jolts, 3 higher than the highest-rated adapter.
|
||||||
|
|
||||||
|
Because adapters can only connect to a source 1-3 jolts lower than its rating, in order to use every adapter, you'd need to choose them like this:
|
||||||
|
|
||||||
|
* The charging outlet has an effective rating of `0` jolts, so the only adapters that could connect to it directly would need to have a joltage rating of `1`, `2`, or `3` jolts. Of these, only one you have is an adapter rated `1` jolt (difference of *`1`*).
|
||||||
|
* From your `1`-jolt rated adapter, the only choice is your `4`-jolt rated adapter (difference of *`3`*).
|
||||||
|
* From the `4`-jolt rated adapter, the adapters rated `5`, `6`, or `7` are valid choices. However, in order to not skip any adapters, you have to pick the adapter rated `5` jolts (difference of *`1`*).
|
||||||
|
* Similarly, the next choices would need to be the adapter rated `6` and then the adapter rated `7` (with difference of *`1`* and *`1`*).
|
||||||
|
* The only adapter that works with the `7`-jolt rated adapter is the one rated `10` jolts (difference of *`3`*).
|
||||||
|
* From `10`, the choices are `11` or `12`; choose `11` (difference of *`1`*) and then `12` (difference of *`1`*).
|
||||||
|
* After `12`, only valid adapter has a rating of `15` (difference of *`3`*), then `16` (difference of *`1`*), then `19` (difference of *`3`*).
|
||||||
|
* Finally, your device's built-in adapter is always 3 higher than the highest adapter, so its rating is `22` jolts (always a difference of *`3`*).
|
||||||
|
|
||||||
|
In this example, when using every adapter, there are *`7`* differences of 1 jolt and *`5`* differences of 3 jolts.
|
||||||
|
|
||||||
|
Here is a larger example:
|
||||||
|
|
||||||
|
```
|
||||||
|
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
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
In this larger example, in a chain that uses all of the adapters, there are *`22`* differences of 1 jolt and *`10`* differences of 3 jolts.
|
||||||
|
|
||||||
|
Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter and count the joltage differences between the charging outlet, the adapters, and your device. *What is the number of 1-jolt differences multiplied by the number of 3-jolt differences?*
|
||||||
|
|
||||||
|
Your puzzle answer was `1856`.
|
||||||
|
|
||||||
|
\--- Part Two ---
|
||||||
|
----------
|
||||||
|
|
||||||
|
To completely determine whether you have enough adapters, you'll need to figure out how many different ways they can be arranged. Every arrangement needs to connect the charging outlet to your device. The previous rules about when adapters can successfully connect still apply.
|
||||||
|
|
||||||
|
The first example above (the one that starts with `16`, `10`, `15`) supports the following arrangements:
|
||||||
|
|
||||||
|
```
|
||||||
|
(0), 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, (22)
|
||||||
|
(0), 1, 4, 5, 6, 7, 10, 12, 15, 16, 19, (22)
|
||||||
|
(0), 1, 4, 5, 7, 10, 11, 12, 15, 16, 19, (22)
|
||||||
|
(0), 1, 4, 5, 7, 10, 12, 15, 16, 19, (22)
|
||||||
|
(0), 1, 4, 6, 7, 10, 11, 12, 15, 16, 19, (22)
|
||||||
|
(0), 1, 4, 6, 7, 10, 12, 15, 16, 19, (22)
|
||||||
|
(0), 1, 4, 7, 10, 11, 12, 15, 16, 19, (22)
|
||||||
|
(0), 1, 4, 7, 10, 12, 15, 16, 19, (22)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
(The charging outlet and your device's built-in adapter are shown in parentheses.) Given the adapters from the first example, the total number of arrangements that connect the charging outlet to your device is *`8`*.
|
||||||
|
|
||||||
|
The second example above (the one that starts with `28`, `33`, `18`) has many arrangements. Here are a few:
|
||||||
|
|
||||||
|
```
|
||||||
|
(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
|
||||||
|
32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 48, 49, (52)
|
||||||
|
|
||||||
|
(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
|
||||||
|
32, 33, 34, 35, 38, 39, 42, 45, 46, 47, 49, (52)
|
||||||
|
|
||||||
|
(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
|
||||||
|
32, 33, 34, 35, 38, 39, 42, 45, 46, 48, 49, (52)
|
||||||
|
|
||||||
|
(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
|
||||||
|
32, 33, 34, 35, 38, 39, 42, 45, 46, 49, (52)
|
||||||
|
|
||||||
|
(0), 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 17, 18, 19, 20, 23, 24, 25, 28, 31,
|
||||||
|
32, 33, 34, 35, 38, 39, 42, 45, 47, 48, 49, (52)
|
||||||
|
|
||||||
|
(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
|
||||||
|
46, 48, 49, (52)
|
||||||
|
|
||||||
|
(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
|
||||||
|
46, 49, (52)
|
||||||
|
|
||||||
|
(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
|
||||||
|
47, 48, 49, (52)
|
||||||
|
|
||||||
|
(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
|
||||||
|
47, 49, (52)
|
||||||
|
|
||||||
|
(0), 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45,
|
||||||
|
48, 49, (52)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
In total, this set of adapters can connect the charging outlet to your device in *`19208`* distinct arrangements.
|
||||||
|
|
||||||
|
You glance back down at your bag and try to remember why you brought so many adapters; there must be *more than a trillion* valid ways to arrange them! Surely, there must be an efficient way to count the arrangements.
|
||||||
|
|
||||||
|
*What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device?*
|
||||||
|
|
||||||
|
Your puzzle answer was `2314037239808`.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||||
|
|
||||||
|
At this point, you should [return to your Advent calendar](/2020) and try another puzzle.
|
||||||
|
|
||||||
|
If you still want to see it, you can [get your puzzle input](10/input).
|
99
2020/day10_adapter_array/src/lib.rs
Normal file
99
2020/day10_adapter_array/src/lib.rs
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
use core::fmt::Display;
|
||||||
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum ParseError {
|
||||||
|
ParseIntError(std::num::ParseIntError),
|
||||||
|
LineMalformed(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ParseIntError> for ParseError {
|
||||||
|
fn from(value: ParseIntError) -> Self {
|
||||||
|
Self::ParseIntError(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for ParseError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::ParseIntError(e) => write!(f, "Unable to parse into integer: {e}"),
|
||||||
|
Self::LineMalformed(v) => write!(f, "Line is malformed: {v}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(input: &str) -> Result<(usize, usize), ParseIntError> {
|
||||||
|
let mut adapters: Vec<_> = input.lines().map(|n| n.parse::<usize>()).collect::<Result<Vec<_>, _>>()?;
|
||||||
|
adapters.push(0);
|
||||||
|
adapters.sort();
|
||||||
|
let first = get_multiplied_differences(&adapters);
|
||||||
|
let groups = split_by_threes(&adapters);
|
||||||
|
let second = groups.iter().map(|group| count_combinations(group)).product();
|
||||||
|
Ok((first, second))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_multiplied_differences(list: &[usize]) -> usize {
|
||||||
|
let mut ones = 0;
|
||||||
|
let mut threes = 1;
|
||||||
|
list.windows(2).for_each(|w| {
|
||||||
|
match w[1]-w[0] {
|
||||||
|
1 => ones += 1,
|
||||||
|
3 => threes += 1,
|
||||||
|
0 | 2 => (),
|
||||||
|
_ => panic!("Invalid chain! Difference between adapters of ratings {} and {} is larger than 3.", w[0], w[1]),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ones * threes
|
||||||
|
}
|
||||||
|
|
||||||
|
fn split_by_threes(list: &[usize]) -> Vec<Vec<usize>> {
|
||||||
|
let mut res = Vec::new();
|
||||||
|
let mut last_partition = 0;
|
||||||
|
|
||||||
|
list.windows(2).enumerate().for_each(|(lower_idx, w)| {
|
||||||
|
if w[1]-w[0] == 3 {
|
||||||
|
res.push(list[last_partition..=lower_idx].to_vec());
|
||||||
|
last_partition = lower_idx+1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
res.push(list[last_partition..].to_vec());
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
fn count_combinations(list: &[usize]) -> usize {
|
||||||
|
match list.len() {
|
||||||
|
t if t < 3 => 1,
|
||||||
|
3 => if list[2]-list[0] <= 3 { 2 } else { 1 },
|
||||||
|
_ => {
|
||||||
|
if list[3]-list[0] <= 3 {
|
||||||
|
count_combinations(&list[1..]) + count_combinations(&list[2..]) + count_combinations(&list[3..])
|
||||||
|
} else if list[2]-list[0] <= 3 {
|
||||||
|
count_combinations(&list[1..]) + count_combinations(&list[2..])
|
||||||
|
} else {
|
||||||
|
count_combinations(&list[1..])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
fn read_file(name: &str) -> String {
|
||||||
|
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sample() {
|
||||||
|
let sample_input = read_file("tests/sample_input");
|
||||||
|
assert_eq!(run(&sample_input), Ok((220, 19208)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_challenge() {
|
||||||
|
let challenge_input = read_file("tests/challenge_input");
|
||||||
|
assert_eq!(run(&challenge_input), Ok((1856, 2314037239808)));
|
||||||
|
}
|
||||||
|
}
|
92
2020/day10_adapter_array/tests/challenge_input
Normal file
92
2020/day10_adapter_array/tests/challenge_input
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
56
|
||||||
|
139
|
||||||
|
42
|
||||||
|
28
|
||||||
|
3
|
||||||
|
87
|
||||||
|
142
|
||||||
|
57
|
||||||
|
147
|
||||||
|
6
|
||||||
|
117
|
||||||
|
95
|
||||||
|
2
|
||||||
|
112
|
||||||
|
107
|
||||||
|
54
|
||||||
|
146
|
||||||
|
104
|
||||||
|
40
|
||||||
|
26
|
||||||
|
136
|
||||||
|
127
|
||||||
|
111
|
||||||
|
47
|
||||||
|
8
|
||||||
|
24
|
||||||
|
13
|
||||||
|
92
|
||||||
|
18
|
||||||
|
130
|
||||||
|
141
|
||||||
|
37
|
||||||
|
81
|
||||||
|
148
|
||||||
|
31
|
||||||
|
62
|
||||||
|
50
|
||||||
|
80
|
||||||
|
91
|
||||||
|
33
|
||||||
|
77
|
||||||
|
1
|
||||||
|
96
|
||||||
|
100
|
||||||
|
9
|
||||||
|
120
|
||||||
|
27
|
||||||
|
97
|
||||||
|
60
|
||||||
|
102
|
||||||
|
25
|
||||||
|
83
|
||||||
|
55
|
||||||
|
118
|
||||||
|
19
|
||||||
|
113
|
||||||
|
49
|
||||||
|
133
|
||||||
|
14
|
||||||
|
119
|
||||||
|
88
|
||||||
|
124
|
||||||
|
110
|
||||||
|
145
|
||||||
|
65
|
||||||
|
21
|
||||||
|
7
|
||||||
|
74
|
||||||
|
72
|
||||||
|
61
|
||||||
|
103
|
||||||
|
20
|
||||||
|
41
|
||||||
|
53
|
||||||
|
32
|
||||||
|
44
|
||||||
|
10
|
||||||
|
34
|
||||||
|
121
|
||||||
|
114
|
||||||
|
67
|
||||||
|
69
|
||||||
|
66
|
||||||
|
82
|
||||||
|
101
|
||||||
|
68
|
||||||
|
84
|
||||||
|
48
|
||||||
|
73
|
||||||
|
17
|
||||||
|
43
|
||||||
|
140
|
31
2020/day10_adapter_array/tests/sample_input
Normal file
31
2020/day10_adapter_array/tests/sample_input
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
|
Loading…
Add table
Add a link
Reference in a new issue