2020 Day 10
This commit is contained in:
parent
05c9c80f52
commit
a71b6283d8
151
2020/10/README.md
Normal file
151
2020/10/README.md
Normal file
@ -0,0 +1,151 @@
|
||||
# 2020 Day 10: Adapter Array
|
||||
Copyright (c) Eric Wastl
|
||||
#### [Direct Link](https://adventofcode.com/2020/day/10)
|
||||
|
||||
## Part 1
|
||||
|
||||
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?**
|
||||
|
||||
## Part 2
|
||||
|
||||
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?**
|
33
2020/10/code.py
Normal file
33
2020/10/code.py
Normal file
@ -0,0 +1,33 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Copyright (c) 2020 Akumatic
|
||||
#
|
||||
#https://adventofcode.com/2020/day/10
|
||||
|
||||
def readFile() -> list:
|
||||
with open(f"{__file__.rstrip('code.py')}input.txt", "r") as f:
|
||||
return [int(line) for line in f.read().strip().split("\n")]
|
||||
|
||||
def part1(input: list) -> int:
|
||||
count = [0, 0, 1]
|
||||
cur = 0
|
||||
for num in input:
|
||||
count[num - cur - 1] += 1
|
||||
cur = num
|
||||
return count[0] * count[2]
|
||||
|
||||
def count_ways(input, cur, cache):
|
||||
if cur in cache:
|
||||
return cache[cur]
|
||||
cache[cur] = sum([count_ways(input, i, cache) for i in range(cur + 1, cur + 4) if i in input])
|
||||
return cache[cur]
|
||||
|
||||
def part2(input: list) -> int:
|
||||
cache = {input[-1]: 1}
|
||||
count_ways(input, 0, cache)
|
||||
return cache[0]
|
||||
|
||||
if __name__ == "__main__":
|
||||
input = readFile()
|
||||
input.sort()
|
||||
print(f"Part 1: {part1(input)}")
|
||||
print(f"Part 2: {part2(input)}")
|
104
2020/10/input.txt
Normal file
104
2020/10/input.txt
Normal file
@ -0,0 +1,104 @@
|
||||
152
|
||||
18
|
||||
146
|
||||
22
|
||||
28
|
||||
133
|
||||
114
|
||||
67
|
||||
19
|
||||
37
|
||||
66
|
||||
14
|
||||
90
|
||||
163
|
||||
26
|
||||
149
|
||||
71
|
||||
106
|
||||
46
|
||||
143
|
||||
145
|
||||
12
|
||||
151
|
||||
105
|
||||
58
|
||||
130
|
||||
93
|
||||
49
|
||||
74
|
||||
83
|
||||
129
|
||||
122
|
||||
63
|
||||
134
|
||||
86
|
||||
136
|
||||
166
|
||||
169
|
||||
159
|
||||
3
|
||||
178
|
||||
88
|
||||
103
|
||||
97
|
||||
110
|
||||
53
|
||||
125
|
||||
128
|
||||
9
|
||||
15
|
||||
78
|
||||
1
|
||||
50
|
||||
87
|
||||
56
|
||||
89
|
||||
60
|
||||
139
|
||||
113
|
||||
43
|
||||
36
|
||||
118
|
||||
170
|
||||
96
|
||||
135
|
||||
23
|
||||
144
|
||||
153
|
||||
150
|
||||
142
|
||||
95
|
||||
180
|
||||
35
|
||||
179
|
||||
80
|
||||
13
|
||||
115
|
||||
2
|
||||
171
|
||||
32
|
||||
70
|
||||
6
|
||||
72
|
||||
119
|
||||
29
|
||||
79
|
||||
27
|
||||
47
|
||||
107
|
||||
73
|
||||
162
|
||||
172
|
||||
57
|
||||
40
|
||||
48
|
||||
100
|
||||
64
|
||||
59
|
||||
175
|
||||
104
|
||||
156
|
||||
94
|
||||
77
|
||||
65
|
2
2020/10/solution.txt
Normal file
2
2020/10/solution.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Part 1: 10884537
|
||||
Part 2: 1261309
|
22
2020/10/test_code.py
Normal file
22
2020/10/test_code.py
Normal file
@ -0,0 +1,22 @@
|
||||
# SPDX-License-Identifier: MIT
|
||||
# Copyright (c) 2020 Akumatic
|
||||
|
||||
from code import part1, part2
|
||||
|
||||
def test():
|
||||
input = [16,10,15,5,1,11,7,19,6,12,4]
|
||||
tmp = input[:]
|
||||
tmp.sort()
|
||||
assert part1(tmp) == 35
|
||||
assert part2(tmp) == 8
|
||||
print("Passed tests for", input)
|
||||
|
||||
input = [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]
|
||||
tmp = input[:]
|
||||
tmp.sort()
|
||||
assert part1(tmp) == 220
|
||||
assert part2(tmp) == 19208
|
||||
print("Passed tests for", input)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test()
|
@ -19,7 +19,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
|
||||
| 03 | :white_check_mark: | :white_check_mark: || 04 | :white_check_mark: | :white_check_mark: |
|
||||
| 05 | :white_check_mark: | :white_check_mark: || 06 | :white_check_mark: | :white_check_mark: |
|
||||
| 07 | :white_check_mark: | :white_check_mark: || 08 | :white_check_mark: | :white_check_mark: |
|
||||
| 09 | :white_check_mark: | :white_check_mark: || 10 | | |
|
||||
| 09 | :white_check_mark: | :white_check_mark: || 10 | :white_check_mark: | :white_check_mark: |
|
||||
| 11 | | || 12 | | |
|
||||
| 13 | | || 14 | | |
|
||||
| 15 | | || 16 | | |
|
||||
|
Loading…
x
Reference in New Issue
Block a user