Solve 2016 days 12 and 13.
This commit is contained in:
43
2016/d12.py
Normal file
43
2016/d12.py
Normal 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
36
2016/d13.py
Normal 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))
|
||||||
Reference in New Issue
Block a user