-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday10.py
94 lines (73 loc) · 2.6 KB
/
day10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from typing import List
def parse_input(filename: str) -> List[int]:
return [int(d) for d in open(filename).read().strip().split(',')]
def knot_hash(numbers: List[int], lengths: List[int]) -> List[int]:
num_elements = len(numbers)
current = 0
skip = 0
for length in lengths:
sub_list = numbers[current: current + length]
sl_length = len(sub_list)
if sl_length < length:
sub_list += numbers[:length - sl_length]
sub_list.reverse()
if sl_length < length:
before = sub_list[-(length - sl_length):]
after = sub_list[:sl_length]
mid = numbers[length - sl_length: -sl_length]
else:
before = numbers[:current]
after = numbers[current + length:]
mid = sub_list
numbers = before + mid + after
current = (current + length + skip) % num_elements
skip += 1
return numbers
def part1(num_elements: int, lengths: List[int]) -> int:
numbers = [i for i in range(num_elements)]
numbers = knot_hash(numbers, lengths)
return numbers[0] * numbers[1]
def part2(input_str: str) -> str:
lengths = []
for ch in input_str:
lengths.append(ord(ch))
lengths += [17, 31, 73, 47, 23]
num_elements = 256
numbers = [i for i in range(num_elements)]
current = 0
skip = 0
for i in range(64):
for length in lengths:
sub_list = numbers[current: current + length]
sl_length = len(sub_list)
if sl_length < length:
sub_list += numbers[:length - sl_length]
sub_list.reverse()
if sl_length < length:
before = sub_list[-(length - sl_length):]
after = sub_list[:sl_length]
mid = numbers[length - sl_length: -sl_length]
else:
before = numbers[:current]
after = numbers[current + length:]
mid = sub_list
numbers = before + mid + after
current = (current + length + skip) % num_elements
skip += 1
dense_hash = []
for i in range(16):
hash_val = numbers.pop(0)
for j in range(15):
hash_val ^= numbers.pop(0)
dense_hash.append(hash_val)
hex_output = ''
for digit in dense_hash:
hex_output += str(hex(digit))[2:]
return hex_output
def main():
lengths = parse_input('input/day10.txt')
print(f'Part 1: {part1(256, lengths)}')
input_str = open('input/day10.txt').read().strip()
print(f'Part 2: {part2(input_str)}')
if __name__ == "__main__":
main()