-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
100 lines (93 loc) · 3.3 KB
/
solution.py
File metadata and controls
100 lines (93 loc) · 3.3 KB
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
95
96
97
98
99
100
def get_value(token: str, values) -> int:
"""Returns the value of the token."""
if token.isdigit():
return int(token)
return values.get(token)
def process_instructions(tokens: list[str], instructions):
"""Processes a list of tokens into its instructions."""
match tokens:
case [a, "AND", b, "->", target]:
instructions[target] = a + " AND " + b
case [a, "OR", b, "->", target]:
instructions[target] = a + " OR " + b
case [a, "RSHIFT", b, "->", target]:
instructions[target] = a + " RSHIFT " + b
case [a, "LSHIFT", b, "->", target]:
instructions[target] = a + " LSHIFT " + b
case ["NOT", a, "->", target]:
instructions[target] = "NOT " + a
case [a, "->", target]:
instructions[target] = a
def provide_signal(expr, wire, values):
"""Provides a signal to the wire if the components providing a signal to the wire has a signal."""
match expr:
case [a, "AND", b]:
a = get_value(a, values)
b = get_value(b, values)
if a != None and b != None:
values[wire] = a & b
case [a, "OR", b]:
a = get_value(a, values)
b = get_value(b, values)
if a != None and b != None:
values[wire] = a | b
case [a, "RSHIFT", b]:
a = get_value(a, values)
b = get_value(b, values)
if a != None and b != None:
values[wire] = a >> b
case [a, "LSHIFT", b]:
a = get_value(a, values)
b = get_value(b, values)
if a != None and b != None:
values[wire] = a << b
case ["NOT", a]:
a = get_value(a, values)
if a != None:
values[wire] = ~a
case [a]:
a = get_value(a, values)
if a is not None:
values[wire] = a
def solve_part1_slow(input: str) -> int:
pass
def solve_part1_fast(input: str) -> int:
"""Computes the signal of wire a."""
instructions = {}
values = {}
lines = input.split("\n")
for line in lines:
tokens = line.split()
process_instructions(tokens, instructions)
while 'a' not in values:
for wire, expr in instructions.items():
if wire in values:
continue
else:
provide_signal(expr.split(), wire, values)
return values['a']
def solve_part2_slow(input: str) -> int:
pass
def solve_part2_fast(input: str) -> int:
"""Computes the signal of wire a, after resetting all wires and providing wire b with the start value of wire a."""
instructions = {}
values = {}
lines = input.split("\n")
for line in lines:
tokens = line.split()
process_instructions(tokens, instructions)
while 'a' not in values:
for wire, expr in instructions.items():
if wire in values:
continue
else:
provide_signal(expr.split(), wire, values)
value_a = values['a']
values = {'b': value_a}
while 'a' not in values:
for wire, expr in instructions.items():
if wire in values:
continue
else:
provide_signal(expr.split(), wire, values)
return values['a']