Skip to content

Commit 227d254

Browse files
author
tuukkasarvi
committedDec 11, 2020
Day 10
1 parent 3193ebb commit 227d254

File tree

2 files changed

+176
-0
lines changed

2 files changed

+176
-0
lines changed
 

‎data/input_10.txt

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
138
2+
3
3+
108
4+
64
5+
92
6+
112
7+
44
8+
53
9+
27
10+
20
11+
23
12+
77
13+
119
14+
62
15+
121
16+
11
17+
2
18+
37
19+
148
20+
34
21+
83
22+
24
23+
10
24+
79
25+
96
26+
98
27+
127
28+
7
29+
115
30+
19
31+
16
32+
78
33+
133
34+
61
35+
82
36+
91
37+
145
38+
39
39+
33
40+
13
41+
97
42+
55
43+
141
44+
1
45+
134
46+
40
47+
71
48+
54
49+
103
50+
101
51+
26
52+
47
53+
90
54+
72
55+
126
56+
124
57+
110
58+
131
59+
58
60+
12
61+
142
62+
105
63+
63
64+
75
65+
50
66+
95
67+
69
68+
25
69+
68
70+
144
71+
86
72+
132
73+
89
74+
128
75+
135
76+
65
77+
125
78+
76
79+
116
80+
32
81+
18
82+
6
83+
38
84+
109
85+
111
86+
30
87+
70
88+
143
89+
104
90+
102
91+
120
92+
31
93+
41
94+
17

‎day_10.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
Each of your joltage adapters is rated for a specific output joltage (your puzzle input). Any given adapter
3+
can take an input 1, 2, or 3 jolts lower than its rating and still produce its rated output joltage.
4+
5+
In addition, your device has a built-in joltage adapter rated for 3 jolts higher than the highest-rated adapter
6+
in your bag. (If your adapter list were 3, 9, and 6, your device's built-in adapter would be rated for 12 jolts.)
7+
8+
Treat the charging outlet near your seat as having an effective joltage rating of 0.
9+
10+
Since you have some time to kill, you might as well test all of your adapters. Wouldn't want to get to your resort
11+
and realize you can't even charge your device!
12+
13+
If you use every adapter in your bag at once, what is the distribution of joltage differences between the charging
14+
outlet, the adapters, and your device?
15+
16+
1.
17+
Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter and count
18+
the joltage differences between the charging outlet, the adapters, and your device. What is the number of 1-jolt
19+
differences multiplied by the number of 3-jolt differences?
20+
21+
2.
22+
What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device?
23+
24+
"""
25+
import numpy as np
26+
27+
28+
filepath = "data/input_10.txt"
29+
30+
31+
def part1():
32+
with open(filepath) as fp:
33+
lines = [line.strip("\n") for line in fp]
34+
joltages = np.array([int(item) for item in lines])
35+
joltages.sort()
36+
joltages = np.concatenate(([0], joltages, [joltages.max() + 3]))
37+
diffs = joltages[1:] - joltages[:(len(joltages)-1)]
38+
unique_elements, counts_elements = np.unique(diffs, return_counts=True)
39+
print(unique_elements)
40+
answer = counts_elements[0] * counts_elements[1]
41+
print("Answer %d." % answer)
42+
43+
44+
def get_number_of_paths(ones_group):
45+
""" For a set of ones [1]_k (k ones) the number of paths f([1]_k) =
46+
f([1]_(k-1)) + f([1]_(k-2)) + f([1]_(k-3)).
47+
"""
48+
assert set(ones_group) == {1} or len(ones_group) == 0
49+
if len(ones_group) == 0:
50+
return 1
51+
elif len(ones_group) == 1:
52+
return 1
53+
elif len(ones_group) == 2:
54+
return get_number_of_paths(ones_group[1:]) + get_number_of_paths(ones_group[2:])
55+
else: # len >= 3
56+
return get_number_of_paths(ones_group[1:]) + get_number_of_paths(ones_group[2:]) + \
57+
get_number_of_paths(ones_group[3:])
58+
59+
60+
def part2():
61+
with open(filepath) as fp:
62+
lines = [line.strip("\n") for line in fp]
63+
joltages = np.array([int(item) for item in lines])
64+
joltages.sort()
65+
joltages = np.concatenate(([0], joltages, [joltages.max() + 3]))
66+
diffs = joltages[1:] - joltages[:(len(joltages)-1)]
67+
# in diffs there are only 1s and 3s
68+
# paths can be formed by combining different 1s. Each subset of 1s between 3s is an independent set of paths
69+
# total number of paths found by multiplying number of paths for each subset of ones: n1 * n2 * n3 ... nk
70+
diffs_string = "".join([str(i) for i in diffs])
71+
ones_groups = diffs_string.split("3")
72+
ones_groups = [list(item) for item in ones_groups]
73+
ones_groups = [[int(i) for i in item] for item in ones_groups]
74+
numbers_of_paths = [get_number_of_paths(og) for og in ones_groups]
75+
total_number_paths = np.prod(np.array(numbers_of_paths))
76+
print("Answer %d" % total_number_paths)
77+
78+
79+
if __name__ == '__main__':
80+
part1()
81+
print("*****")
82+
part2()

0 commit comments

Comments
 (0)
Please sign in to comment.