Skip to content

Commit 2d29721

Browse files
committed
Added solutions for day 10
1 parent c399ccd commit 2d29721

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

advent2020/day10.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 util
25+
26+
27+
def get_adapter_differences(adapters):
28+
diffs = []
29+
current = 0
30+
for adapter in sorted(adapters):
31+
diffs.append(adapter - current)
32+
current = adapter
33+
diffs.append(3)
34+
return diffs
35+
36+
37+
def count_arrangements(adapters):
38+
sorted_adapters = sorted(adapters)
39+
counts = []
40+
for idx, adapter in enumerate(sorted_adapters):
41+
count = 0
42+
# see if adapter is compatible with outlet (0 joltage)
43+
if adapter <= 3:
44+
count += 1
45+
# count how many previous adapters are compatible and accumulate possible connection paths
46+
look_back = 1
47+
while idx - look_back >= 0 and (adapter - sorted_adapters[idx - look_back]) <= 3:
48+
count += counts[idx - look_back]
49+
look_back += 1
50+
counts.append(count)
51+
return counts
52+
53+
54+
def get_part1_answer(adapters):
55+
diffs = get_adapter_differences(adapters)
56+
num_ones = 0
57+
num_threes = 0
58+
for diff in diffs:
59+
if diff == 1:
60+
num_ones += 1
61+
elif diff == 3:
62+
num_threes += 1
63+
return num_ones*num_threes
64+
65+
66+
def get_part2_answer(adapters):
67+
return count_arrangements(adapters)[-1]
68+
69+
70+
def run():
71+
with open(util.get_input_file_path("day10.txt")) as f:
72+
adapters = [int(line) for line in f if len(line.strip()) > 0]
73+
print(f"The answer to part 1 is {get_part1_answer(adapters)}")
74+
print(f"The answer to part 2 is {get_part2_answer(adapters)}")

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from advent2020 import day7
3333
from advent2020 import day8
3434
from advent2020 import day9
35+
from advent2020 import day10
3536

3637

3738
day_runners = [
@@ -43,7 +44,8 @@
4344
lambda: day6.run(),
4445
lambda: day7.run(),
4546
lambda: day8.run(),
46-
lambda: day9.run()
47+
lambda: day9.run(),
48+
lambda: day10.run()
4749
]
4850

4951

test/test_day10.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.day10 import get_part1_answer
27+
from advent2020.day10 import get_part2_answer
28+
29+
30+
adapter_data1 = """
31+
16
32+
10
33+
15
34+
5
35+
1
36+
11
37+
7
38+
19
39+
6
40+
12
41+
4
42+
"""
43+
44+
adapter_data2 = """
45+
28
46+
33
47+
18
48+
42
49+
31
50+
14
51+
46
52+
20
53+
48
54+
47
55+
24
56+
23
57+
49
58+
45
59+
19
60+
38
61+
39
62+
11
63+
1
64+
32
65+
25
66+
35
67+
8
68+
17
69+
7
70+
9
71+
4
72+
2
73+
34
74+
10
75+
3
76+
"""
77+
78+
79+
class Day10Test(unittest.TestCase):
80+
def test_day10(self):
81+
self.run_day10_test(adapter_data1, 35, 8)
82+
self.run_day10_test(adapter_data2, 220, 19208)
83+
84+
def run_day10_test(self, data, expected_part1, expected_part2):
85+
adapters = [int(line) for line in data.split("\n") if len(line.strip()) > 0]
86+
self.assertEqual(get_part1_answer(adapters), expected_part1)
87+
self.assertEqual(get_part2_answer(adapters), expected_part2)

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