2020 Day 09

This commit is contained in:
Akumatic 2020-12-09 13:51:57 +01:00
parent ab66e38e47
commit 05c9c80f52
6 changed files with 1150 additions and 1 deletions

90
2020/09/README.md Normal file
View File

@ -0,0 +1,90 @@
# 2020 Day 9: Encoding Error
Copyright (c) Eric Wastl
#### [Direct Link](https://adventofcode.com/2020/day/9)
## Part 1
With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you.
Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input).
The data appears to be encrypted with the eXchange-Masking Addition System (XMAS) which, conveniently for you, is an old cypher with an important weakness.
XMAS starts by transmitting a **preamble** of 25 numbers. After that, each number you receive should be the sum of any two of the 25 immediately previous numbers. The two numbers will have different values, and there might be more than one such pair.
For example, suppose your preamble consists of the numbers `1` through `25` in a random order. To be valid, the next number must be the sum of two of those numbers:
- `26` would be a **valid** next number, as it could be `1` plus `25` (or many other pairs, like `2` and `24`).
- `49` would be a **valid** next number, as it is the sum of `24` and `25`.
- `100` would **not** be valid; no two of the previous `25` numbers sum to `100`.
- `50` would also **not** be valid; although `25` appears in the previous `25` numbers, the two numbers in the pair must be different.
Suppose the 26th number is `45`, and the first number (no longer an option, as it is more than 25 numbers ago) was `20`. Now, for the next number to be valid, there needs to be some pair of numbers among `1`-`19`, `21`-`25`, or `45` that add up to it:
- `26` would still be a **valid** next number, as `1` and `25` are still within the previous `25` numbers.
- `65` would **not** be valid, as no two of the available numbers sum to it.
- `64` and `66` would both be **valid**, as they are the result of `19`+`45` and `21`+`45` respectively.
Here is a larger example which only considers the previous **5** numbers (and has a preamble of length 5):
```
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
```
In this example, after the 5-number preamble, almost every number is the sum of two of the previous 5 numbers; the only number that does not follow this rule is **`127`**.
The first step of attacking the weakness in the XMAS data is to find the first number in the list (after the preamble) which is **not** the sum of two of the 25 numbers before it. **What is the first number that does not have this property?**
## Part 2
The final step in breaking the XMAS encryption relies on the invalid number you just found: you must **find a contiguous set of at least two numbers** in your list which sum to the invalid number from step 1.
Again consider the above example:
```
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
```
In this list, adding up all of the numbers from `15` through `40` produces the invalid number from step 1, `127`. (Of course, the contiguous set of numbers in your actual list might be much longer.)
To find the **encryption weakness**, add together the **smallest** and **largest number** in this contiguous range; in this example, these are `15` and `47`, producing **62**.
**What is the encryption weakness in your XMAS-encrypted list of numbers?**

44
2020/09/code.py Normal file
View File

@ -0,0 +1,44 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2020 Akumatic
#
#https://adventofcode.com/2020/day/9
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 getSums(input, position, size):
return [input[i] + input[j]
for i in range(position, position + size)
for j in range(i + 1, position + size)
if input[i] != input[j]
]
def part1(input: list, len_preamble: int = 25) -> int:
size = len(input)
cur_pos = 0
sums = getSums(input, cur_pos, len_preamble)
loop_size = size - len_preamble - 1
while cur_pos < loop_size:
if input[cur_pos + len_preamble] not in sums:
return input[cur_pos + len_preamble]
cur_pos += 1
sums = getSums(input, cur_pos, len_preamble)
def part2(input: list, num: int) -> int:
size = len(input)
for i in range(size):
sum = input[i]
next = i+1
while sum < num and next < size:
sum += input[next]
next += 1
if sum == num:
tmp = input[i:next-1]
return min(tmp) + max(tmp)
if __name__ == "__main__":
input = readFile()
number = part1(input)
print(f"Part 1: {number}")
print(f"Part 2: {part2(input, number)}")

1000
2020/09/input.txt Normal file

File diff suppressed because it is too large Load Diff

2
2020/09/solution.txt Normal file
View File

@ -0,0 +1,2 @@
Part 1: 10884537
Part 2: 1261309

13
2020/09/test_code.py Normal file
View File

@ -0,0 +1,13 @@
# SPDX-License-Identifier: MIT
# Copyright (c) 2020 Akumatic
from code import part1, part2
def test():
input = [35, 20, 15, 25, 47, 40, 62, 55, 65, 95, 102,
117, 150, 182, 127, 219, 299, 277, 309, 579]
assert part1(input, 5) == 127
assert part2(input, 127) == 62
if __name__ == "__main__":
test()

View File

@ -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 | | || 10 | | |
| 09 | :white_check_mark: | :white_check_mark: || 10 | | |
| 11 | | || 12 | | |
| 13 | | || 14 | | |
| 15 | | || 16 | | |