コード例 #1
0
ファイル: sln.py プロジェクト: ZSwaff/advent
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from itertools import count

from comp import Comp

# part 1
c = 0
for x in range(50):
    for y in range(50):
        cmp = Comp(fname='inp.txt')
        c += cmp.add_inputs([x, y]).last_outputs[0]
print(c)


# part 2
def ib(x, y):
    return bool(Comp(fname='inp.txt').add_inputs([x, y]).last_outputs[0])


x, y = 1000, 1000
for x in count(x):
    if ib(x, y):
        break
old = x, y
while True:
    for y in count(y, -1):
        if not ib(x + 99, y - 100):
            break
    for x in count(x, -1):
コード例 #2
0

# part 1
cmp = Comp(fin='inp.txt')
out = cmp.outputs
print(sum(out[i * 3 + 2] == 2 for i in range(len(out) // 3)))


# part 2
def sign(a):
    if a == 0:
        return 0
    return 1 if a > 0 else -1


with open('inp.txt') as fin:
    ops = [int(e) for e in fin.read().strip().split(',')]
ops[0] = 2


cmp = Comp(ops=ops).add_inputs([0, 0, 0])
while True:
    out = cmp.outputs
    sb = {(out[i * 3], out[i * 3 + 1]): out[i * 3 + 2] for i in range(len(out) // 3)}
    if not any(v == 2 for v in sb.values()):
        break
    paddle = [k for k, v in sb.items() if v == 3][0]
    ball = [k for k, v in sb.items() if v == 4][0]
    cmp.add_inputs([sign(ball[0] - paddle[0])])
print(sb[(-1, 0)])
コード例 #3
0
ファイル: sln.py プロジェクト: ZSwaff/advent
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from collections import defaultdict

from comp import Comp

DRS = {0: (0, 1), 1: (1, 0), 2: (0, -1), 3: (-1, 0)}

# part 1
locs = defaultdict(int)
cmp = Comp(fin='inp.txt')
cloc, cdr = (0, 0), 0
while True:
    locs[cloc], turn = cmp.add_inputs([locs[cloc]]).last_outputs
    if cmp.is_finished:
        break
    cdr = (cdr + turn * 2 - 1) % 4
    cloc = cloc[0] + DRS[cdr][0], cloc[1] + DRS[cdr][1]
print(len(locs))

# part 2
locs = defaultdict(int)
cmp = Comp(fin='inp.txt')
cloc, cdr = (0, 0), 0
locs[cloc] = 1
while True:
    locs[cloc], turn = cmp.add_inputs([locs[cloc]]).last_outputs
    if cmp.is_finished:
        break
    cdr = (cdr + turn * 2 - 1) % 4