Solved 2024/14
This commit is contained in:
+177
@@ -0,0 +1,177 @@
|
||||
## \-\-- Day 14: Restroom Redoubt \-\--
|
||||
|
||||
One of The Historians needs to use the bathroom; fortunately, you know
|
||||
there\'s a bathroom near an unvisited location on their list, and so
|
||||
you\'re all quickly teleported directly to the lobby of Easter Bunny
|
||||
Headquarters.
|
||||
|
||||
Unfortunately, EBHQ seems to have \"improved\" bathroom security *again*
|
||||
after your last [visit](/2016/day/2). The area outside the bathroom is
|
||||
swarming with robots!
|
||||
|
||||
To get The Historian safely to the bathroom, you\'ll need a way to
|
||||
predict where the robots will be in the future. Fortunately, they all
|
||||
seem to be moving on the tile floor in predictable *straight lines*.
|
||||
|
||||
You make a list (your puzzle input) of all of the robots\' current
|
||||
*positions* (`p`) and *velocities* (`v`), one robot per line. For
|
||||
example:
|
||||
|
||||
p=0,4 v=3,-3
|
||||
p=6,3 v=-1,-3
|
||||
p=10,3 v=-1,2
|
||||
p=2,0 v=2,-1
|
||||
p=0,0 v=1,3
|
||||
p=3,0 v=-2,-2
|
||||
p=7,6 v=-1,-3
|
||||
p=3,0 v=-1,-2
|
||||
p=9,3 v=2,3
|
||||
p=7,3 v=-1,2
|
||||
p=2,4 v=2,-3
|
||||
p=9,5 v=-3,-3
|
||||
|
||||
Each robot\'s position is given as `p=x,y` where `x` represents the
|
||||
number of tiles the robot is from the left wall and `y` represents the
|
||||
number of tiles from the top wall (when viewed from above). So, a
|
||||
position of `p=0,0` means the robot is all the way in the top-left
|
||||
corner.
|
||||
|
||||
Each robot\'s velocity is given as `v=x,y` where `x` and `y` are given
|
||||
in *tiles per second*. Positive `x` means the robot is moving to the
|
||||
*right*, and positive `y` means the robot is moving *down*. So, a
|
||||
velocity of `v=1,-2` means that each second, the robot moves `1` tile to
|
||||
the right and `2` tiles up.
|
||||
|
||||
The robots outside the actual bathroom are in a space which is `101`
|
||||
tiles wide and `103` tiles tall (when viewed from above). However, in
|
||||
this example, the robots are in a space which is only `11` tiles wide
|
||||
and `7` tiles tall.
|
||||
|
||||
The robots are good at navigating over/under each other (due to a
|
||||
combination of springs, extendable legs, and quadcopters), so they can
|
||||
share the same tile and don\'t interact with each other. Visually, the
|
||||
number of robots on each tile in this example looks like this:
|
||||
|
||||
1.12.......
|
||||
...........
|
||||
...........
|
||||
......11.11
|
||||
1.1........
|
||||
.........1.
|
||||
.......1...
|
||||
|
||||
These robots have a unique feature for maximum bathroom security: they
|
||||
can *teleport*. When a robot would run into an edge of the space
|
||||
they\'re in, they instead *teleport to the other side*, effectively
|
||||
wrapping around the edges. Here is what robot `p=2,4 v=2,-3` does for
|
||||
the first few seconds:
|
||||
|
||||
Initial state:
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
..1........
|
||||
...........
|
||||
...........
|
||||
|
||||
After 1 second:
|
||||
...........
|
||||
....1......
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
|
||||
After 2 seconds:
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
......1....
|
||||
...........
|
||||
|
||||
After 3 seconds:
|
||||
...........
|
||||
...........
|
||||
........1..
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
|
||||
After 4 seconds:
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
..........1
|
||||
|
||||
After 5 seconds:
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
.1.........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
|
||||
The Historian can\'t wait much longer, so you don\'t have to simulate
|
||||
the robots for very long. Where will the robots be after `100` seconds?
|
||||
|
||||
In the above example, the number of robots on each tile after 100
|
||||
seconds has elapsed looks like this:
|
||||
|
||||
......2..1.
|
||||
...........
|
||||
1..........
|
||||
.11........
|
||||
.....1.....
|
||||
...12......
|
||||
.1....1....
|
||||
|
||||
To determine the safest area, count the *number of robots in each
|
||||
quadrant* after 100 seconds. Robots that are exactly in the middle
|
||||
(horizontally or vertically) don\'t count as being in any quadrant, so
|
||||
the only relevant robots are:
|
||||
|
||||
..... 2..1.
|
||||
..... .....
|
||||
1.... .....
|
||||
|
||||
..... .....
|
||||
...12 .....
|
||||
.1... 1....
|
||||
|
||||
In this example, the quadrants contain `1`, `3`, `4`, and `1` robot.
|
||||
Multiplying these together gives a total *safety factor* of `12`.
|
||||
|
||||
Predict the motion of the robots in your list within a space which is
|
||||
`101` tiles wide and `103` tiles tall. *What will the safety factor be
|
||||
after exactly 100 seconds have elapsed?*
|
||||
|
||||
Your puzzle answer was `230686500`.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: \*
|
||||
|
||||
## \-\-- Part Two \-\-- {#part2}
|
||||
|
||||
During the bathroom break, someone notices that these robots seem
|
||||
awfully similar to ones built and used at the North Pole. If they\'re
|
||||
the same type of robots, they should have a hard-coded [Easter
|
||||
egg]{title="This puzzle was originally going to be about the motion of space rocks in a fictitious arcade game called Meteoroids, but we just had an arcade puzzle."}:
|
||||
very rarely, most of the robots should arrange themselves into *a
|
||||
picture of a Christmas tree*.
|
||||
|
||||
*What is the fewest number of seconds that must elapse for the robots to
|
||||
display the Easter egg?*
|
||||
|
||||
Answer:
|
||||
|
||||
Although it hasn\'t changed, you can still [get your puzzle
|
||||
input](14/input).
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#!/bin/python3
|
||||
import sys,time,re
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import list2int,get_re,lprint,loadFile,addTuples,grid_valid
|
||||
start_time = time.time()
|
||||
|
||||
# input_f = 'test'
|
||||
# size_r = 7
|
||||
# size_c = 11
|
||||
|
||||
input_f = 'input'
|
||||
size_r = 103
|
||||
size_c = 101
|
||||
|
||||
grid = [['.']*size_c]*size_r
|
||||
|
||||
def nprint(grid,pos,x):
|
||||
for r in range(size_r):
|
||||
for c in range(size_c):
|
||||
if (c,r) == pos:
|
||||
print(x,end='')
|
||||
else:
|
||||
print(grid[r][c],end='')
|
||||
print()
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
# #
|
||||
#########################################
|
||||
def part1():
|
||||
instructions = loadFile(input_f)
|
||||
for idx,inst in enumerate(instructions):
|
||||
match = get_re(r"^p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)",inst)
|
||||
|
||||
instructions[idx] = [(int(match.group(1)),int(match.group(2))),(int(match.group(3)),int(match.group(4)))]
|
||||
|
||||
coordinates = {}
|
||||
initial = {}
|
||||
for idx,inst in enumerate(instructions):
|
||||
#inst = [(2,4),(2,-3)]
|
||||
#print(inst)
|
||||
|
||||
#print('Initial state')
|
||||
pos = inst[0]
|
||||
vel = inst[1]
|
||||
if pos not in initial:
|
||||
initial[pos] = 0
|
||||
initial[pos] += 1
|
||||
|
||||
#nprint(grid,pos,'1')
|
||||
length = 100
|
||||
for i in range (0,length):
|
||||
pos = addTuples(pos,vel)
|
||||
#print('After',i+1,'seconds')
|
||||
if pos[0] < 0:
|
||||
pos = (pos[0]+size_c,pos[1])
|
||||
if pos[0] >= size_c:
|
||||
pos = (pos[0]-size_c,pos[1])
|
||||
if pos[1] < 0:
|
||||
pos = (pos[0],pos[1]+size_r)
|
||||
if pos[1] >= size_r:
|
||||
pos = (pos[0],pos[1]-size_r)
|
||||
|
||||
#print('Position inside grid: ', grid_valid(pos[1],pos[0],grid))
|
||||
#nprint(grid,pos,'1')
|
||||
#print(pos)
|
||||
#input()
|
||||
if pos not in coordinates:
|
||||
coordinates[pos] = 0
|
||||
coordinates[pos] += 1
|
||||
#print('End State')
|
||||
#nprint(grid,pos,'1')
|
||||
#input()
|
||||
|
||||
#pprint(coordinates)
|
||||
# print(instructions)
|
||||
# print()
|
||||
# print(initial)
|
||||
# print()
|
||||
# for r in range(size_r):
|
||||
# for c in range(size_c):
|
||||
# if (c,r) in initial.keys():
|
||||
# print(initial[(c,r)],end='')
|
||||
# else:
|
||||
# print(grid[r][c],end='')
|
||||
# print()
|
||||
# print('----------------------')
|
||||
# print(coordinates)
|
||||
|
||||
# for r in range(size_r):
|
||||
# for c in range(size_c):
|
||||
# if (c,r) in coordinates.keys():
|
||||
# print(coordinates[(c,r)],end='')
|
||||
# else:
|
||||
# print(grid[r][c],end='')
|
||||
# print()
|
||||
|
||||
center = (int((size_r-1)/2),int((size_c-1)/2))
|
||||
|
||||
TL = 0 #top left
|
||||
BL = 0 #bottom left
|
||||
TR = 0 #top right
|
||||
BR = 0 #bottom right
|
||||
for v in coordinates:
|
||||
if v[0] < center[1] and v[1] < center[0]:
|
||||
#print(v,'top left',coordinates[v])
|
||||
TL += coordinates[v]
|
||||
if v[0] > center[1] and v[1] < center[0]:
|
||||
#print(v,'top right',coordinates[v])
|
||||
TR += coordinates[v]
|
||||
if v[0] > center[1] and v[1] > center[0]:
|
||||
#print(v,'bot right',coordinates[v])
|
||||
BR += coordinates[v]
|
||||
if v[0] < center[1] and v[1] > center[0]:
|
||||
#print(v,'bot left',coordinates[v])
|
||||
BL += coordinates[v]
|
||||
|
||||
#print(center)
|
||||
return TL*TR*BR*BL
|
||||
start_time = time.time()
|
||||
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|
||||
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
def part2():
|
||||
return
|
||||
|
||||
start_time = time.time()
|
||||
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|
||||
Reference in New Issue
Block a user