Skip to content

Commit 891bae8

Browse files
committed
Added solutions for day 9
1 parent a2665d6 commit 891bae8

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

advent2020/day9.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
from . import day1
25+
from . import util
26+
27+
28+
def find_invalid_number(numbers, preamble_length):
29+
for idx in range(preamble_length, len(numbers)):
30+
num = numbers[idx]
31+
if day1.find_sum_pair(numbers[idx - preamble_length:idx], num) is None:
32+
return num
33+
return None
34+
35+
36+
def find_contiguous_sum(numbers, target):
37+
for length in range(2, len(numbers)):
38+
for idx in range(len(numbers) - length + 1):
39+
contiguous_range = numbers[idx:idx+length]
40+
if sum(numbers[idx:idx+length]) == target:
41+
return contiguous_range
42+
return None
43+
44+
45+
def get_part1_answer(numbers):
46+
return find_invalid_number(numbers, 25)
47+
48+
49+
def get_part2_answer(numbers, invalid_num):
50+
contiguous_range = find_contiguous_sum(numbers, invalid_num)
51+
return min(contiguous_range) + max(contiguous_range)
52+
53+
54+
def run():
55+
with open(util.get_input_file_path("day9.txt")) as f:
56+
numbers = [int(line) for line in f if len(line.strip()) > 0]
57+
invalid_num = get_part1_answer(numbers)
58+
print(f"The answer to part 1 is {invalid_num}")
59+
print(f"The answer to part 2 is {get_part2_answer(numbers, invalid_num)}")

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from advent2020 import day6
3232
from advent2020 import day7
3333
from advent2020 import day8
34+
from advent2020 import day9
3435

3536

3637
day_runners = [
@@ -41,7 +42,8 @@
4142
lambda: day5.run(),
4243
lambda: day6.run(),
4344
lambda: day7.run(),
44-
lambda: day8.run()
45+
lambda: day8.run(),
46+
lambda: day9.run()
4547
]
4648

4749

test/test_day9.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2020 Andrew Krepps
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
24+
import unittest
25+
26+
from advent2020.day9 import find_contiguous_sum
27+
from advent2020.day9 import find_invalid_number
28+
29+
30+
data = """
31+
35
32+
20
33+
15
34+
25
35+
47
36+
40
37+
62
38+
55
39+
65
40+
95
41+
102
42+
117
43+
150
44+
182
45+
127
46+
219
47+
299
48+
277
49+
309
50+
576
51+
"""
52+
53+
54+
class Day9Test(unittest.TestCase):
55+
def test_day9_part1(self):
56+
numbers = [int(line) for line in data.split("\n") if len(line.strip()) > 0]
57+
self.assertEqual(find_invalid_number(numbers, 5), 127)
58+
self.assertListEqual(find_contiguous_sum(numbers, 127), [15, 25, 47, 40])

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy