13.1, 14.1 python
This commit is contained in:
@@ -0,0 +1,91 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
pattern = re.compile(r'''
|
||||||
|
Button A: X\+(\d+), Y\+(\d+)
|
||||||
|
Button B: X\+(\d+), Y\+(\d+)
|
||||||
|
Prize: X=(\d+), Y=(\d+)
|
||||||
|
'''.strip(), re.MULTILINE)
|
||||||
|
|
||||||
|
|
||||||
|
data = open("input.txt").read().strip().split("\n\n")
|
||||||
|
data = '''
|
||||||
|
Button A: X+94, Y+34
|
||||||
|
Button B: X+22, Y+67
|
||||||
|
Prize: X=8400, Y=5400
|
||||||
|
|
||||||
|
Button A: X+26, Y+66
|
||||||
|
Button B: X+67, Y+21
|
||||||
|
Prize: X=12748, Y=12176
|
||||||
|
|
||||||
|
Button A: X+17, Y+86
|
||||||
|
Button B: X+84, Y+37
|
||||||
|
Prize: X=7870, Y=6450
|
||||||
|
|
||||||
|
Button A: X+69, Y+23
|
||||||
|
Button B: X+27, Y+71
|
||||||
|
Prize: X=18641, Y=10279
|
||||||
|
'''.strip().split("\n\n")
|
||||||
|
|
||||||
|
data = [tuple(map(int, pattern.match(x).groups())) for x in data]
|
||||||
|
|
||||||
|
tok = lambda x: x[0]*3+x[1]
|
||||||
|
def f(data):
|
||||||
|
s = 0
|
||||||
|
for n, (xa, ya, xb, yb, xt, yt) in enumerate(data):
|
||||||
|
s1 = set()
|
||||||
|
for i in range(min(xt//xa+1, 101)):
|
||||||
|
for j in range(min(xt//xb+1, 101)):
|
||||||
|
if xa*i+xb*j==xt:
|
||||||
|
s1.add((i, j))
|
||||||
|
s2 = set()
|
||||||
|
for i in range(min(yt//ya+1, 101)):
|
||||||
|
for j in range(min(yt//yb+1, 101)):
|
||||||
|
if ya*i+yb*j==yt:
|
||||||
|
s2.add((i, j))
|
||||||
|
if s1&s2:
|
||||||
|
print("@0", n, s1&s2)
|
||||||
|
s += tok(min(s1&s2, key=tok))
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def f2(data):
|
||||||
|
s = 0
|
||||||
|
for i, (xa, ya, xb, yb, xt, yt) in enumerate(data):
|
||||||
|
print(f'@1 {i} {xa=}, {ya=}, {xb=}, {yb=}, {xt=}, {yt=}')
|
||||||
|
#xt += 10_000_000_000_000
|
||||||
|
#yt += 10000000000000
|
||||||
|
b = 0
|
||||||
|
a = 0
|
||||||
|
al = 0
|
||||||
|
ar = max(xt//xa+1, yt//ya+1)
|
||||||
|
while ar-al>1:
|
||||||
|
am = (ar+al)//2
|
||||||
|
bl = 0
|
||||||
|
br = max(xt//xb+1, yt//yb+1)
|
||||||
|
f = False
|
||||||
|
while br-bl>1:
|
||||||
|
bm = (bl+br)//2
|
||||||
|
if xa*am+xb*bm>xt and ya*am+yb*bm>xt:
|
||||||
|
br = bm
|
||||||
|
elif xa*am+xb*bm<xt and ya*am+yb*bm<xt:
|
||||||
|
bl = bm
|
||||||
|
elif xa*am+xb*bm==xt and ya*am+yb*bm==xt:
|
||||||
|
f = True
|
||||||
|
b = bm
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
print(al, ar)
|
||||||
|
if f:
|
||||||
|
a = am
|
||||||
|
ar = am
|
||||||
|
else:
|
||||||
|
if a>3*br:
|
||||||
|
al = am
|
||||||
|
else:
|
||||||
|
ar = am
|
||||||
|
print('@end', a, b, a*3+b)
|
||||||
|
s += a*3+b
|
||||||
|
return s
|
||||||
|
print(f(data))
|
||||||
|
print(f2(data))
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
from math import prod
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
data = open("input.txt").read().strip().split("\n")
|
||||||
|
w = 101
|
||||||
|
h = 103
|
||||||
|
|
||||||
|
def robot(r):
|
||||||
|
'p=0,4 v=3,-3'
|
||||||
|
p, v = r.split()
|
||||||
|
return tuple(map(int, p[2:].split(',')+v[2:].split(',')))
|
||||||
|
robots = [robot(r) for r in data]
|
||||||
|
|
||||||
|
|
||||||
|
def bound(a, m):
|
||||||
|
return a%m
|
||||||
|
bound_x = lambda x: bound(x, w)
|
||||||
|
bound_y = lambda x: bound(x, h)
|
||||||
|
|
||||||
|
def step(robots):
|
||||||
|
return [(bound_x(x+vx), bound_y(y+vy), vx, vy) for x, y, vx, vy in robots]
|
||||||
|
|
||||||
|
def quadrants(robots):
|
||||||
|
res = [[] for _ in range(4)]
|
||||||
|
for x, y, _, _ in robots:
|
||||||
|
if x==w//2 or y==h//2:
|
||||||
|
pass
|
||||||
|
elif x<w//2 and y<h//2:
|
||||||
|
res[0].append((x,y))
|
||||||
|
elif x>w//2 and y<h//2:
|
||||||
|
res[1].append((x,y))
|
||||||
|
elif x<w//2 and y>h//2:
|
||||||
|
res[2].append((x,y))
|
||||||
|
elif x>w//2 and y>h//2:
|
||||||
|
res[3].append((x,y))
|
||||||
|
return res
|
||||||
|
|
||||||
|
def part1(robots):
|
||||||
|
for _ in range(100):
|
||||||
|
robots = step(robots)
|
||||||
|
return prod(map(len, quadrants(robots)))
|
||||||
|
|
||||||
|
def print_robots(r):
|
||||||
|
t = [[' ']*w for _ in range(h)]
|
||||||
|
for x, y, _, _ in robots:
|
||||||
|
t[y][x] = '*'
|
||||||
|
print('\n'.join(''.join(x for x in y) for y in t))
|
||||||
|
|
||||||
|
def difference(q1, q2):
|
||||||
|
q2 = [(w-1-x, y) for x, y in q2]
|
||||||
|
return len(set.difference(set(q1), set(q2)))
|
||||||
|
|
||||||
|
def part2(robots):
|
||||||
|
i = 0
|
||||||
|
print_robots(robots)
|
||||||
|
while not input("next?"):
|
||||||
|
same = False
|
||||||
|
while not same:
|
||||||
|
robots = step(robots)
|
||||||
|
i += 1
|
||||||
|
r = quadrants(robots)
|
||||||
|
same = difference(r[0], r[1])<30
|
||||||
|
print(difference(r[0], r[1]))
|
||||||
|
print_robots(robots)
|
||||||
|
return i
|
||||||
|
p2 = part2(robots)
|
||||||
|
print(part1(robots))
|
||||||
|
print(p2)
|
||||||
Reference in New Issue
Block a user