Day 10 - brute force
This commit is contained in:
parent
0355f55a25
commit
491dc3c5fa
4 changed files with 266 additions and 0 deletions
104
2020/10/data.txt
Normal file
104
2020/10/data.txt
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
2
|
||||||
|
49
|
||||||
|
78
|
||||||
|
116
|
||||||
|
143
|
||||||
|
42
|
||||||
|
142
|
||||||
|
87
|
||||||
|
132
|
||||||
|
86
|
||||||
|
67
|
||||||
|
44
|
||||||
|
136
|
||||||
|
82
|
||||||
|
125
|
||||||
|
1
|
||||||
|
108
|
||||||
|
123
|
||||||
|
46
|
||||||
|
37
|
||||||
|
137
|
||||||
|
148
|
||||||
|
106
|
||||||
|
121
|
||||||
|
10
|
||||||
|
64
|
||||||
|
165
|
||||||
|
17
|
||||||
|
102
|
||||||
|
156
|
||||||
|
22
|
||||||
|
117
|
||||||
|
31
|
||||||
|
38
|
||||||
|
24
|
||||||
|
69
|
||||||
|
131
|
||||||
|
144
|
||||||
|
162
|
||||||
|
63
|
||||||
|
171
|
||||||
|
153
|
||||||
|
90
|
||||||
|
9
|
||||||
|
107
|
||||||
|
79
|
||||||
|
7
|
||||||
|
55
|
||||||
|
138
|
||||||
|
34
|
||||||
|
52
|
||||||
|
77
|
||||||
|
152
|
||||||
|
3
|
||||||
|
158
|
||||||
|
100
|
||||||
|
45
|
||||||
|
129
|
||||||
|
130
|
||||||
|
135
|
||||||
|
23
|
||||||
|
93
|
||||||
|
96
|
||||||
|
103
|
||||||
|
124
|
||||||
|
95
|
||||||
|
8
|
||||||
|
62
|
||||||
|
39
|
||||||
|
118
|
||||||
|
164
|
||||||
|
172
|
||||||
|
75
|
||||||
|
122
|
||||||
|
20
|
||||||
|
145
|
||||||
|
14
|
||||||
|
112
|
||||||
|
61
|
||||||
|
43
|
||||||
|
141
|
||||||
|
30
|
||||||
|
85
|
||||||
|
101
|
||||||
|
151
|
||||||
|
29
|
||||||
|
113
|
||||||
|
94
|
||||||
|
68
|
||||||
|
58
|
||||||
|
76
|
||||||
|
97
|
||||||
|
28
|
||||||
|
111
|
||||||
|
128
|
||||||
|
21
|
||||||
|
11
|
||||||
|
163
|
||||||
|
161
|
||||||
|
4
|
||||||
|
168
|
||||||
|
157
|
||||||
|
27
|
||||||
|
72
|
120
2020/10/main.py
Normal file
120
2020/10/main.py
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
import copy
|
||||||
|
|
||||||
|
|
||||||
|
def load(filename):
|
||||||
|
with open(filename, "r", encoding="utf-8") as f:
|
||||||
|
return [int(x) for x in f]
|
||||||
|
|
||||||
|
|
||||||
|
def calc_builtin(adapters):
|
||||||
|
return max(adapters) + 3
|
||||||
|
|
||||||
|
|
||||||
|
def calc_diffs(adapters):
|
||||||
|
wall = 0
|
||||||
|
builtin = calc_builtin(adapters)
|
||||||
|
adapters = sorted(adapters)
|
||||||
|
adapters.insert(0, 0)
|
||||||
|
adapters.append(builtin)
|
||||||
|
adapters += [wall, builtin]
|
||||||
|
|
||||||
|
d1 = 0
|
||||||
|
d2 = 0
|
||||||
|
d3 = 0
|
||||||
|
sa = sorted(adapters)
|
||||||
|
for x in range(0, len(sa) - 1):
|
||||||
|
a = sa[x]
|
||||||
|
b = sa[x+1]
|
||||||
|
d = b - a
|
||||||
|
if d == 1:
|
||||||
|
d1 += 1
|
||||||
|
elif d == 2:
|
||||||
|
d2 += 1
|
||||||
|
elif d == 3:
|
||||||
|
d3 += 1
|
||||||
|
|
||||||
|
return d1, d2, d3
|
||||||
|
|
||||||
|
|
||||||
|
def is_compatible(a, b):
|
||||||
|
return abs(a - b) <= 3
|
||||||
|
|
||||||
|
|
||||||
|
def calc_arrangements(adapters, wall, builtin):
|
||||||
|
return _calc(sorted(adapters), [wall], builtin)
|
||||||
|
|
||||||
|
def _calc(adapters, arranged, builtin):
|
||||||
|
a = arranged[-1]
|
||||||
|
b = adapters.pop(0)
|
||||||
|
while is_compatible(a, b):
|
||||||
|
if not adapters:
|
||||||
|
if is_compatible(b, builtin):
|
||||||
|
yield arranged + [b, builtin]
|
||||||
|
return
|
||||||
|
|
||||||
|
cadapters = copy.deepcopy(adapters)
|
||||||
|
carranged = copy.deepcopy(arranged)
|
||||||
|
carranged.append(b)
|
||||||
|
yield from _calc(cadapters, carranged, builtin)
|
||||||
|
|
||||||
|
b = adapters.pop(0)
|
||||||
|
|
||||||
|
|
||||||
|
assert calc_builtin([3, 6, 9]) == 12
|
||||||
|
|
||||||
|
adapters = load("test1.txt")
|
||||||
|
d1, _, d3 = calc_diffs(adapters)
|
||||||
|
assert d1 == 7
|
||||||
|
assert d3 == 5
|
||||||
|
|
||||||
|
adapters = load("test2.txt")
|
||||||
|
d1, d2, d3 = calc_diffs(adapters)
|
||||||
|
assert d1 == 22
|
||||||
|
assert d3 == 10
|
||||||
|
print(d1, d2, d3)
|
||||||
|
|
||||||
|
adapters = load("data.txt")
|
||||||
|
d1, _, d3 = calc_diffs(adapters)
|
||||||
|
print(f"1: Answer is {d1 * d3}")
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
assert d1*d3 == 2450, "Task 1"
|
||||||
|
|
||||||
|
adapters = load("test1.txt")
|
||||||
|
bi = calc_builtin(adapters)
|
||||||
|
arrangements = list(calc_arrangements(adapters, 0, bi))
|
||||||
|
assert [0, 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
assert [0, 1, 4, 5, 6, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
assert [0, 1, 4, 5, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
assert [0, 1, 4, 5, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
assert [0, 1, 4, 6, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
assert [0, 1, 4, 6, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
assert [0, 1, 4, 7, 10, 11, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
assert [0, 1, 4, 7, 10, 12, 15, 16, 19, 22] in arrangements
|
||||||
|
|
||||||
|
adapters = load("test2.txt")
|
||||||
|
bi = calc_builtin(adapters)
|
||||||
|
arrangements = list(calc_arrangements(adapters, 0, bi))
|
||||||
|
assert [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] in arrangements
|
||||||
|
assert [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] in arrangements
|
||||||
|
assert [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] in arrangements
|
||||||
|
assert [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] in arrangements
|
||||||
|
assert [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] in arrangements
|
||||||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 46, 48, 49, 52] in arrangements
|
||||||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 46, 49, 52] in arrangements
|
||||||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 47, 48, 49, 52] in arrangements
|
||||||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 47, 49, 52] in arrangements
|
||||||
|
assert [0, 3, 4, 7, 10, 11, 14, 17, 20, 23, 25, 28, 31, 34, 35, 38, 39, 42, 45, 48, 49, 52] in arrangements
|
||||||
|
assert len(arrangements) == 19208
|
||||||
|
|
||||||
|
#adapters = load("data.txt")
|
||||||
|
#bi = calc_builtin(adapters)
|
||||||
|
#
|
||||||
|
#arrangements = calc_arrangements(adapters, 0, bi)
|
||||||
|
#c = 0
|
||||||
|
#for x in arrangements:
|
||||||
|
# print(x)
|
||||||
|
# c += 1
|
||||||
|
#
|
||||||
|
#print(f"2:Found {c} ways to connect.")
|
11
2020/10/test1.txt
Normal file
11
2020/10/test1.txt
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
16
|
||||||
|
10
|
||||||
|
15
|
||||||
|
5
|
||||||
|
1
|
||||||
|
11
|
||||||
|
7
|
||||||
|
19
|
||||||
|
6
|
||||||
|
12
|
||||||
|
4
|
31
2020/10/test2.txt
Normal file
31
2020/10/test2.txt
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
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
|
Loading…
Reference in a new issue