aboutsummaryrefslogtreecommitdiff
path: root/2024/17/puzzle-2.py
blob: e23e34f6e2d3e9cce18a2f71ae70a4f33098b78c (plain) (blame)
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
#!/usr/bin/python3

def main() -> None:
	with open("input", "r") as f:
		prog = [
			int(x) for x in (
				next(l for l in f.readlines() if l.startswith("Program"))
				.split(':' )[1]
				.split(',')
			)
		][::-1]
	print(search(prog))


def step(a: int) -> int:
	b = (a & 7) ^ 2
	return (a>>b ^ b ^ 7) & 7


def search(prog: list[int], a: int = 0) -> int | None:
	if len(prog) == 0:
		return a
	for i in range(0b111 + 1):
		n = a<<3 | i
		if (
			step(n) == prog[0]
			and (b := search(prog[1:], n)) is not None
		):
			return b


if __name__ == "__main__":
	main()