Solve day 24 2017.
This commit is contained in:
parent
869a22a310
commit
f62f5fcff2
35
2017/d24.py
Normal file
35
2017/d24.py
Normal file
@ -0,0 +1,35 @@
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def part_1(data):
|
||||
ports = [list(map(int, line.split("/"))) for line in data.splitlines()]
|
||||
in_to_out = defaultdict(list)
|
||||
for index, (i, o) in enumerate(ports):
|
||||
in_to_out[i].append((o, index))
|
||||
in_to_out[o].append((i, index))
|
||||
|
||||
max_sum = 0
|
||||
max_len = (0, 0)
|
||||
bs = [(0, [], 0, 0)]
|
||||
|
||||
while bs:
|
||||
current_port, used, current_sum, current_len = bs.pop()
|
||||
max_sum = max(max_sum, current_sum)
|
||||
max_len = max(max_len, (current_len, current_sum))
|
||||
for out_port, index in in_to_out[current_port]:
|
||||
if index in used:
|
||||
continue
|
||||
new_sum = current_sum + current_port + out_port
|
||||
bs.append((out_port, used + [index], new_sum, current_len + 1))
|
||||
print(max_sum)
|
||||
print(max_len[1])
|
||||
|
||||
|
||||
def main():
|
||||
with open("i24.txt") as f:
|
||||
data = f.read()
|
||||
part_1(data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue
Block a user