Add 2022 extras and rm input/samples
This commit is contained in:
parent
7809a82ce7
commit
1d2098a708
30 changed files with 402 additions and 6471 deletions
|
|
@ -4,85 +4,71 @@ input = open("input", 'r')
|
|||
data = []
|
||||
total = 0
|
||||
|
||||
minx = 1000
|
||||
miny = 0
|
||||
maxx = 500
|
||||
maxy = 0
|
||||
ROW = 2_000_000
|
||||
# ROW = 10
|
||||
|
||||
taxicab = lambda x,y: abs(x[0] - y[0]) + abs(x[1] - y[1])
|
||||
tuning = lambda x: 4_000_000*x[0] + x[1]
|
||||
|
||||
minx = 10_000_000
|
||||
maxx = 0
|
||||
|
||||
beaconsRow = set()
|
||||
|
||||
for line in input:
|
||||
nums = line.strip().split(" -> ")
|
||||
tempdata = []
|
||||
for string in nums:
|
||||
x, y = string.split(",")
|
||||
minx = min(minx, int(x))
|
||||
miny = min(miny, int(y))
|
||||
maxx = max(maxx, int(x))
|
||||
maxy = max(maxy, int(y))
|
||||
tempdata.append((int(x), int(y)))
|
||||
data.append(tempdata)
|
||||
line = line.strip()
|
||||
sensor, beacon = line.split(": ")
|
||||
sx, sy = sensor.split(", ")
|
||||
bx, by = beacon.split(", ")
|
||||
sensor = (int(sx.split("=")[1]), int(sy.split("=")[1]))
|
||||
beacon = (int(bx.split("=")[1]), int(by.split("=")[1]))
|
||||
minx = min(sensor[0], beacon[0], minx)
|
||||
maxx = max(sensor[0], beacon[0], maxx)
|
||||
data.append((sensor, beacon))
|
||||
|
||||
minx -= maxy
|
||||
distance = []
|
||||
for sensor, beacon in data:
|
||||
d = taxicab(beacon, sensor)
|
||||
if beacon[1] == ROW and beacon not in beaconsRow:
|
||||
beaconsRow.add(beacon)
|
||||
total -= 1
|
||||
distance.append((sensor, d))
|
||||
|
||||
grid = [["." for _ in range(maxx-minx+1+maxy)] for _ in range(maxy-miny+1)]
|
||||
|
||||
for line in data:
|
||||
for one, two in zip(line, line[1:]):
|
||||
if one[0] == two[0]:
|
||||
i = min(one[1], two[1])
|
||||
a = max(one[1], two[1])
|
||||
for y in range(i, a+1):
|
||||
grid[y-miny][one[0]-minx] = "#"
|
||||
else:
|
||||
i = min(one[0], two[0])
|
||||
a = max(one[0], two[0])
|
||||
for x in range(i, a+1):
|
||||
grid[one[1]-miny][x-minx] = "#"
|
||||
|
||||
# Send sand
|
||||
falling = False
|
||||
while not falling:
|
||||
settled = False
|
||||
sandpos = (500-minx, 0-miny)
|
||||
while not settled:
|
||||
if sandpos[1]+1 >= len(grid):
|
||||
falling = True
|
||||
for x in range(minx*2, (maxx*2+1)):
|
||||
for sensor, dist in distance:
|
||||
psd = taxicab(sensor, (x, ROW))
|
||||
if dist >= psd:
|
||||
# print(x)
|
||||
total +=1
|
||||
break
|
||||
if grid[sandpos[1]+1][sandpos[0]] == ".":
|
||||
sandpos = (sandpos[0], sandpos[1]+1)
|
||||
elif grid[sandpos[1]+1][sandpos[0]-1] == ".":
|
||||
sandpos = (sandpos[0]-1, sandpos[1]+1)
|
||||
elif grid[sandpos[1]+1][sandpos[0]+1] == ".":
|
||||
sandpos = (sandpos[0]+1, sandpos[1]+1)
|
||||
else:
|
||||
grid[sandpos[1]][sandpos[0]] = "o"
|
||||
settled = True
|
||||
total += 1
|
||||
# print(sandpos)
|
||||
|
||||
print(total)
|
||||
|
||||
grid.append(["." for _ in range(maxx-minx+1+maxy)])
|
||||
grid.append(["#" for _ in range(maxx-minx+1+maxy)])
|
||||
found = False
|
||||
for sensor, dist in distance:
|
||||
for dx in range(dist+2):
|
||||
dy = (dist+1) - dx
|
||||
for dirx, diry in [(-1,-1),(-1,1),(1,-1),(1,1)]:
|
||||
x = sensor[0] + (dx*dirx)
|
||||
y = sensor[1] + (dy*diry)
|
||||
if not (0 <= x <= 4_000_000 and 0 <= y <= 4_000_000):
|
||||
continue
|
||||
|
||||
final = False
|
||||
while not final:
|
||||
settled = False
|
||||
sandpos = (500-minx, 0-miny)
|
||||
while not settled:
|
||||
if grid[sandpos[1]+1][sandpos[0]] == ".":
|
||||
sandpos = (sandpos[0], sandpos[1]+1)
|
||||
elif grid[sandpos[1]+1][sandpos[0]-1] == ".":
|
||||
sandpos = (sandpos[0]-1, sandpos[1]+1)
|
||||
elif grid[sandpos[1]+1][sandpos[0]+1] == ".":
|
||||
sandpos = (sandpos[0]+1, sandpos[1]+1)
|
||||
else:
|
||||
if sandpos[1] == 0-miny and sandpos[0] == 500-minx:
|
||||
final = True
|
||||
grid[sandpos[1]][sandpos[0]] = "o"
|
||||
settled = True
|
||||
total += 1
|
||||
# print(sandpos)
|
||||
for row in grid:
|
||||
for column in row:
|
||||
print(column, end="")
|
||||
print()
|
||||
print(total)
|
||||
found = True
|
||||
for sensor, dist in distance:
|
||||
psd = taxicab((x,y),sensor)
|
||||
if psd <= dist:
|
||||
found = False
|
||||
break
|
||||
if found:
|
||||
print(tuning((x,y)))
|
||||
|
||||
|
||||
|
||||
# for x in range(0, 4_000_000):
|
||||
# for y in range(0, 4_000_000):
|
||||
# for sensor, dist in distance:
|
||||
# psd = taxicab(sensor, (x, ROW))
|
||||
# if dist >= psd:
|
||||
# break
|
||||
# print((x,y), tuning((x,y)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue