Solve 2016 days 12 and 13.

This commit is contained in:
felixm 2024-04-04 08:09:13 -04:00
parent 46b7d7dfbf
commit e87b2f9069
3 changed files with 82 additions and 1 deletions

43
2016/d12.py Normal file
View File

@ -0,0 +1,43 @@
from lib import *
data = open(0).read().strip()
part_2 = True
REGS = "abcd"
regs = {c: 0 for c in REGS}
if part_2:
regs["c"] = 1
insts = data.splitlines()
i = 0
while i < len(insts):
parts = insts[i].split()
cmd = parts[0]
if cmd == "cpy":
if parts[1] in "abcd":
regs[parts[2]] = regs[parts[1]]
else:
regs[parts[2]] = int(parts[1])
elif cmd == "jnz":
val = 0
if parts[1] in "abcd":
val = regs[parts[1]]
else:
val = int(parts[1])
if val != 0:
i += int(parts[2])
continue
elif cmd == "inc":
regs[parts[1]] += 1
elif cmd == "dec":
regs[parts[1]] -= 1
else:
assert False
i += 1
print(regs["a"])

36
2016/d13.py Normal file
View File

@ -0,0 +1,36 @@
import sys
from lib import *
data = int(open(0).read().strip())
part2 = True
def is_wall(x, y):
v = x*x + 3*x + 2*x*y + y + y*y
v += data
bin_count = bin(v).count("1")
if bin_count % 2 == 0:
return False
return True
seen = set()
poss = [(1, 1)]
for step in range(51):
nposs = []
for pos in poss:
if pos in seen:
continue
seen.add(pos)
if not part2 and pos == (31, 39):
print(step)
sys.exit(0)
for (x, y) in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
nx, ny = pos[0] + x, pos[1] + y
if nx >= 0 and ny >= 0 and not is_wall(nx, ny):
nposs.append((nx, ny))
poss = nposs
if part2:
print(len(seen))

View File

@ -44,4 +44,6 @@ written in Python.
- Day 9: 26:00 okay
- Day 10: 23:07 okay
- Day 11: 75:00 -__-
- Day 12:
- Day 12: 10:05 okay
- Day 13: 9:43 okayish
- Day 14: