day24
This commit is contained in:
parent
a44bd51f68
commit
fd8aa6af91
65
day24.py
Normal file
65
day24.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
from tools.aoc import AOCDay
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
def get_max_bridge_strength(bridge_parts: list, connect_to: int) -> int:
|
||||||
|
max_strength = 0
|
||||||
|
|
||||||
|
for part in bridge_parts:
|
||||||
|
if connect_to not in part:
|
||||||
|
continue
|
||||||
|
|
||||||
|
sub_parts = bridge_parts.copy()
|
||||||
|
sub_parts.remove(part)
|
||||||
|
this_strength = sum(part) + get_max_bridge_strength(sub_parts, part[int(not part.index(connect_to))])
|
||||||
|
max_strength = max(max_strength, this_strength)
|
||||||
|
|
||||||
|
return max_strength
|
||||||
|
|
||||||
|
|
||||||
|
def get_possible_bridges(bridge_parts: list, connect_to: int) -> list:
|
||||||
|
bridges = []
|
||||||
|
for part in bridge_parts:
|
||||||
|
if connect_to not in part:
|
||||||
|
continue
|
||||||
|
|
||||||
|
sub_parts = bridge_parts.copy()
|
||||||
|
sub_parts.remove(part)
|
||||||
|
bridges.append([part])
|
||||||
|
sub_bridges = get_possible_bridges(sub_parts, part[int(not part.index(connect_to))])
|
||||||
|
for sub_bridge in sub_bridges:
|
||||||
|
bridges.append([part] + sub_bridge)
|
||||||
|
|
||||||
|
return bridges
|
||||||
|
|
||||||
|
|
||||||
|
class Day(AOCDay):
|
||||||
|
inputs = [
|
||||||
|
[
|
||||||
|
(31, "input24_test"),
|
||||||
|
(1940, "input24")
|
||||||
|
],
|
||||||
|
[
|
||||||
|
(19, "input24_test"),
|
||||||
|
(1928, "input24")
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
def get_bridge_parts(self) -> list:
|
||||||
|
return [
|
||||||
|
list(map(int, line.split("/")))
|
||||||
|
for line in self.getInput()
|
||||||
|
]
|
||||||
|
|
||||||
|
def part1(self) -> Any:
|
||||||
|
return get_max_bridge_strength(self.get_bridge_parts(), 0)
|
||||||
|
|
||||||
|
def part2(self) -> Any:
|
||||||
|
bridges = get_possible_bridges(self.get_bridge_parts(), 0)
|
||||||
|
max_length = max(map(len, bridges))
|
||||||
|
return max(sum(sum(x) for x in b) for b in bridges if len(b) == max_length)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
day = Day(2017, 24)
|
||||||
|
day.run(verbose=True)
|
||||||
57
inputs/input24
Normal file
57
inputs/input24
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
42/37
|
||||||
|
28/28
|
||||||
|
29/25
|
||||||
|
45/8
|
||||||
|
35/23
|
||||||
|
49/20
|
||||||
|
44/4
|
||||||
|
15/33
|
||||||
|
14/19
|
||||||
|
31/44
|
||||||
|
39/14
|
||||||
|
25/17
|
||||||
|
34/34
|
||||||
|
38/42
|
||||||
|
8/42
|
||||||
|
15/28
|
||||||
|
0/7
|
||||||
|
49/12
|
||||||
|
18/36
|
||||||
|
45/45
|
||||||
|
28/7
|
||||||
|
30/43
|
||||||
|
23/41
|
||||||
|
0/35
|
||||||
|
18/9
|
||||||
|
3/31
|
||||||
|
20/31
|
||||||
|
10/40
|
||||||
|
0/22
|
||||||
|
1/23
|
||||||
|
20/47
|
||||||
|
38/36
|
||||||
|
15/8
|
||||||
|
34/32
|
||||||
|
30/30
|
||||||
|
30/44
|
||||||
|
19/28
|
||||||
|
46/15
|
||||||
|
34/50
|
||||||
|
40/20
|
||||||
|
27/39
|
||||||
|
3/14
|
||||||
|
43/45
|
||||||
|
50/42
|
||||||
|
1/33
|
||||||
|
6/39
|
||||||
|
46/44
|
||||||
|
22/35
|
||||||
|
15/20
|
||||||
|
43/31
|
||||||
|
23/23
|
||||||
|
19/27
|
||||||
|
47/15
|
||||||
|
43/43
|
||||||
|
25/36
|
||||||
|
26/38
|
||||||
|
1/10
|
||||||
8
inputs/input24_test
Normal file
8
inputs/input24_test
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
0/2
|
||||||
|
2/2
|
||||||
|
2/3
|
||||||
|
3/4
|
||||||
|
3/5
|
||||||
|
0/1
|
||||||
|
10/1
|
||||||
|
9/10
|
||||||
Loading…
Reference in New Issue
Block a user