You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

40 lines
1.1 KiB

#!/usr/bin/env python3
import sys, re
def parse(l):
return re.compile(r"target area: x=(?P<x1>\d+)..(?P<x2>\d+), y=(?P<y1>-?\d+)..(?P<y2>-?\d+)").search(l)
def step(cx,cy,x,y):
cx += x
cy += y
x = x-1 if x > 0 else (x+1 if x < 0 else 0)
y -= 1
return cx,cy,x,y
def hits_target(x,y,t):
cx, cy, rt, my = 0,0, False, 0
while cx <= max(t[0]) and cy >= min(t[1]):
cx,cy,x,y = step(cx,cy,x,y)
my = max(my,cy)
if (min(t[0]) <= cx <= max(t[0])) and (min(t[1]) <= cy <= max(t[1])): rt = True
return my, rt
if __name__ == '__main__':
x1,x2,y1,y2 = map(int,parse(open(sys.argv[1]).read()).groups())
target = ((x1,x2),(y1,y2))
dic = {}
for x in range(target[0][1]+1):
for y in range(min(target[1]),target[0][1]+max(map(abs,target[1]))):
my,rt = hits_target(x,y,target)
if rt: dic[(x,y)] = my
# challenge 1
res1 = str(max(dic.values()))
print("challenge 1:" +"\n" + res1 + "\n")
# challenge 2
res2 = str(len(dic))
print("challenge 2:" +"\n" + res2 + "\n")