Added 2017/19

This commit is contained in:
2024-11-28 13:58:15 +01:00
parent c970981aa9
commit 81990b5833
4 changed files with 152 additions and 109 deletions
+42 -109
View File
@@ -2,129 +2,62 @@
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
from fred import list2int,get_value_in_direction
input_f = 'test'
input_f = 'input'
part = 1
#########################################
# #
# Part 1 #
# Part 1+2 #
# #
#########################################
#########################################
grid = []
start = ()
prev = ()
with open(input_f) as file:
for line in file:
grid.append(line.rstrip())
def valid_n(grid,cur):
r,c = cur
count = 0
if grid[r-1][c] != ' ':
count += 1
if grid[r+1][c] != ' ':
count += 1
if grid[r][c+1] != ' ':
count += 1
if grid[r][c-1] != ' ':
count += 1
return count
start = (0,grid[0].index('|'))
if part == 1:
with open(input_f) as file:
for line in file:
grid.append(list(line.rstrip()))
dir = (1,0)
visited = []
cur = start
count = 1
for r, row in enumerate(grid):
for c, col in enumerate(row):
print(grid[r][c],end='')
print()
while get_value_in_direction(grid,cur) != 'S': # last letter in my input
new = (cur[0]+dir[0],cur[1]+dir[1])
if get_value_in_direction(grid,new) != ' ':
cur = new
count += 1
if get_value_in_direction(grid,cur).isalpha():
visited.append(get_value_in_direction(grid,cur))
if get_value_in_direction(grid,cur) == '+':
for r, row in enumerate(grid):
for c, col in enumerate(row):
if r == 0:
if grid[r][c] == '|':
start = (r,c)
if get_value_in_direction(grid,cur,'right') == '-' and dir[1] != -1:
dir = (0,1)
continue
end = False
cur = start
prev = (-1,-1)
prev_dir = {
'up': False,
'down': False,
'left': False,
'right': False
}
if get_value_in_direction(grid,cur,'left') == '-' and dir[1] != 1:
dir = (0,-1)
continue
while not end:
r,c = cur
try:
if grid[r-1][c] != ' ' and prev != (r-1,c):
print('up',grid[r-1][c])
prev = cur
prev_dir = {
'up': True,
'down': False,
'left': False,
'right': False
}
cur = (r-1,c)
except Exception as e:
print(e)
try:
if grid[r+1][c] != ' ' and prev != (r+1,c):
print('down',grid[r+1][c])
if valid_n(grid,cur) >= 1 and prev_dir['down']:
prev = cur
prev_dir = {
'up': False,
'down': True,
'left': False,
'right': False
}
cur = (r+1,c)
except Exception as e:
print(e)
try:
if grid[r][c-1] != ' ' and prev != (r,c-1) and not prev_dir['left']:
print('left',grid[r][c-1])
prev = cur
prev_dir = {
'up': False,
'down': False,
'left': True,
'right': False
}
cur = (r,c-1)
except Exception as e:
print(e)
try:
if grid[r][c+1] != ' ' and prev != (r,c+1) and not prev_dir['right']:
print('right',grid[r][c+1])
prev = cur
prev_dir = {
'up': False,
'down': False,
'left': False,
'right': True
}
cur = (r,c+1)
except Exception as e:
print(e)
input()
if get_value_in_direction(grid,cur,'up') == '|' and dir[0] != 1:
dir = (-1,0)
continue
if get_value_in_direction(grid,cur,'down') == '|' and dir[0] != -1:
dir = (1,0)
continue
for i in visited:
print(i,end='')
print()
print(count)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()
+59
View File
@@ -0,0 +1,59 @@
## \-\-- Day 20: Particle Swarm \-\--
Suddenly, the GPU contacts you, asking for
help.
Someone has asked it to simulate *too many particles*, and it won\'t be
able to finish them all in time to render the next frame at this rate.
It transmits to you a buffer (your puzzle input) listing each particle
in order (starting with particle `0`, then particle `1`, particle `2`,
and so on). For each particle, it provides the `X`, `Y`, and `Z`
coordinates for the particle\'s position (`p`), velocity (`v`), and
acceleration (`a`), each in the format `<X,Y,Z>`.
Each tick, all particles are updated simultaneously. A particle\'s
properties are updated in the following order:
- Increase the `X` velocity by the `X` acceleration.
- Increase the `Y` velocity by the `Y` acceleration.
- Increase the `Z` velocity by the `Z` acceleration.
- Increase the `X` position by the `X` velocity.
- Increase the `Y` position by the `Y` velocity.
- Increase the `Z` position by the `Z` velocity.
Because of seemingly tenuous rationale involving
[z-buffering](https://en.wikipedia.org/wiki/Z-buffering), the GPU would
like to know which particle will stay closest to position `<0,0,0>` in
the long term. Measure this using the [Manhattan
distance](https://en.wikipedia.org/wiki/Taxicab_geometry), which in this
situation is simply the sum of the absolute values of a particle\'s `X`,
`Y`, and `Z` position.
For example, suppose you are only given two particles, both of which
stay entirely on the X-axis (for simplicity). Drawing the current states
of particles `0` and `1` (in that order) with an adjacent a number line
and diagram of current `X` positions (marked in parentheses), the
following would take place:
p=< 3,0,0>, v=< 2,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
p=< 4,0,0>, v=< 0,0,0>, a=<-2,0,0> (0)(1)
p=< 4,0,0>, v=< 1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
p=< 2,0,0>, v=<-2,0,0>, a=<-2,0,0> (1) (0)
p=< 4,0,0>, v=< 0,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
p=<-2,0,0>, v=<-4,0,0>, a=<-2,0,0> (1) (0)
p=< 3,0,0>, v=<-1,0,0>, a=<-1,0,0> -4 -3 -2 -1 0 1 2 3 4
p=<-8,0,0>, v=<-6,0,0>, a=<-2,0,0> (0)
At this point, particle `1` will never be closer to `<0,0,0>` than
particle `0`, and so, in the long run, particle `0` will stay closest.
*Which particle will stay closest to position `<0,0,0>`* in the long
term?
To begin, [get your puzzle input](20/input).
Answer: