2015/day07: Complete!
ci/woodpecker/push/woodpecker Pipeline is pending Details

main
earnest ma 2023-05-14 12:53:56 -04:00
parent 4a8fa46228
commit b58397645e
Signed by: earnest ma
GPG Key ID: A343F43342EB6E2A
3 changed files with 92 additions and 0 deletions

79
2015/day07.py Normal file
View File

@ -0,0 +1,79 @@
#!/usr/bin/env python3
from typing import TextIO
import itertools
circuit: dict[str, int] = {}
class Continue(Exception):
pass
def part1(instructions: TextIO) -> int:
ins: list[str] = []
# process all instructions into a list
for l in instructions:
ins.append(l.strip())
# repeat until the list is empty
while len(ins) != 0:
for connection in ins:
cpart = connection.split()
# print(cpart, sep="")
cpart.remove("->")
# Exclude the destination
try:
# possibles = list(itertools.filterfalse(str.isupper, cpart))[:-1]
for p in range(len(cpart) - 1):
if not cpart[p].isnumeric() and not cpart[p].isupper():
if cpart[p] in circuit:
# Get the value?
# This error can be ignored, lol
cpart[p] = circuit[cpart[p]]
else:
# We can't do anything for this
raise Continue
except Continue:
continue
# assignment
if len(cpart) == 2:
circuit[cpart[1]] = int(cpart[0])
elif "NOT" in cpart:
circuit[cpart[-1]] = (
~int(cpart[1]) & 0xFFFF
) # see notes for why you can't just use ~
elif "AND" in cpart:
circuit[cpart[-1]] = int(cpart[0]) & int(cpart[2])
elif "OR" in cpart:
circuit[cpart[-1]] = int(cpart[0]) | int(cpart[2])
elif "LSHIFT" in cpart:
circuit[cpart[-1]] = int(cpart[0]) << int(cpart[2])
elif "RSHIFT" in cpart:
circuit[cpart[-1]] = int(cpart[0]) >> int(cpart[2])
ins.remove(connection)
# print(circuit)
return circuit["a"]
def part2(f: TextIO):
circuit["b"] = circuit["a"]
for k in list(circuit.keys()):
if k != "b":
del circuit[k]
return part1(f)
if __name__ == "__main__":
with open("day07.txt", "r") as f:
print(part1(f))
with open("day07.txt", "r") as f:
print(part2(f))

BIN
2015/day07.txt Normal file

Binary file not shown.

13
2015/day07_test.py Normal file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
from day07 import *
def test_part1() -> None:
with open("day07.txt", "r") as f:
assert part1(f) == 956
def test_part2() -> None:
with open("day07.txt", "r") as f:
assert part2(f) == 40149