Part 1 | Part 2 | Total | |
---|---|---|---|
Time | 16:18:48 | >24h | >24h |
This is another day that I didn't stay up until midnight to do the problem when it came out. After I did Part 1, I didn't have any time to do Part 2 until 12/24.
Part 1 was farily simple enough. I used a dictionary to store the positions and a function to loop around if necessary.
from helpers.datagetter import data_in, submit
from helpers.matrix import Matrix
import re
from collections import Counter, defaultdict
import time
import math
from tqdm import tqdm
ans = 0
din = data_in(split=True, numbers=False)
path = ""
grid = defaultdict(int)
bounds = [0, 0]
for i in range(len(din)):
if not din[i]:
path = din[i+1]
break
for j in range(len(din[i])):
char = din[i][j]
if char != " ":
grid[(i, j)] = char
bounds = max(bounds[0], i), max(bounds[1], j)
print(grid)
move_set = [int(x) if x.isnumeric() else x for x in re.findall(r'\d+|[RL]', path)]
print(move_set)
print(bounds)
for i in range(bounds[1]):
if grid[(0, i)] != 0:
break
# Initial position
at = (0, i)
def go(at, num, dir):
def get_next(at, dir):
x, y = at
dx, dy = dir
if grid[(x + dx, y + dy)] != 0:
return tuple([x + dx, y + dy])
else:
while grid[(x - dx, y - dy)] != 0:
x -= dx
y -= dy
return tuple([x, y])
while grid[get_next(at, dir)] != "#" and num > 0:
at = get_next(at, dir)
num -= 1
return at
# Right down left up
dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
# Start facing right
facing = 0
for move in move_set:
if type(move) is int:
print(at)
at = go(at, move, dirs[facing])
elif move == "R":
facing = (facing + 1) % 4
elif move == "L":
facing = (facing - 1) % 4
ans = 1000 * (at[0] + 1) + 4 * (at[1] + 1) + facing
print(ans)
submit(ans)
I wanted to make a general solution for Part 2, but unfortunately in the interest of time I decided to make a ghastly if statement so I could get this puzzle over with and move on to other ones I had gotten behind on.
from helpers.datagetter import data_in, submit
from helpers.matrix import Matrix
import re
from collections import Counter, defaultdict
import time
import math
from tqdm import tqdm
ans = 0
din = data_in(split=True, numbers=False)
path = ""
grid = defaultdict(int)
bounds = [0, 0]
for i in range(len(din)):
if not din[i]:
path = din[i+1]
break
for j in range(len(din[i])):
char = din[i][j]
if char != " ":
grid[(i, j)] = char
bounds = max(bounds[0], i), max(bounds[1], j)
#print(grid)
move_set = [int(x) if x.isnumeric() else x for x in re.findall(r'\d+|[RL]', path)]
for i in range(bounds[1]):
if grid[(0, i)] != 0:
break
# Initial position
at = (0, i)
def go(at, num, dir):
def get_next(at, facin):
x, y = at
dx, dy = dirs[facin]
if grid[(x + dx, y + dy)] != 0:
return tuple([x + dx, y + dy]), facin
# Off 2 facing right
elif y == 149 and x >= 0 and x < 50 and facin == 0:
return tuple([49 - x + 100, 99]), (facin + 2) % 4
# Off 2 facing down
elif y >= 100 and y < 150 and x == 49 and facin == 1:
return tuple([y - 50, 99]), (facin + 1) % 4
# Off 2 facing up
elif y >= 100 and y < 150 and x == 0 and facin == 3:
return tuple([199, y - 100]), facin
# Off 3 facing right
elif y == 99 and x >= 50 and x < 100 and facin == 0:
return tuple([49, (x - 50) + 100]), (facin - 1) % 4
# Off 3 facing left
elif y == 50 and x >= 50 and x < 100 and facin == 2:
return tuple([100, x - 50]), (facin - 1) % 4
# Off 5 facing right
elif y == 99 and x >= 100 and x < 150 and facin == 0:
return tuple([49 - (x - 100), 149]), (facin - 2) % 4
# Off 5 facing down
elif y >= 50 and y < 100 and x == 149 and facin == 1:
return tuple([(y - 50) + 150, 49]), (facin + 1) % 4
# Off 6 facing down
elif y >= 0 and y < 50 and x == 199 and facin == 1:
return tuple([0, y + 100]), facin
# Off 6 facing right
elif y == 49 and x >= 150 and x < 200 and facin == 0:
return tuple([149, (x - 150) + 50]), (facin - 1) % 4
# Off 6 facing left
elif y == 0 and x >= 150 and x < 200 and facin == 2:
return tuple([0, (x - 150) + 50]), (facin + 3) % 4
# Off 4 facing left
elif y == 0 and x >= 100 and x < 150 and facin == 2:
return tuple([49 - (x - 100), 50]), (facin + 2) % 4
# Off 4 facing up
elif y >= 0 and y < 50 and x == 100 and facin == 3:
return tuple([y + 50, 50]), (facin + 1) % 4
# Off 1 facing left
elif y == 50 and x >= 0 and x < 50 and facin == 2:
return tuple([(49 - x) + 100, 0]), (facin - 2) % 4
# Off 1 facing up
elif y >= 50 and y < 100 and x == 0 and facin == 3:
return tuple([y + 100, 0]), (facin - 3) % 4
else:
exit("It shouldn't get here")
while grid[get_next(at, dir)[0]] != "#" and num > 0:
at, dir = get_next(at, dir)
num -= 1
return at, dir
# Right down left up
dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
# Start facing right
facing = 0
for move in move_set:
if type(move) is int:
at, facing = go(at, move, facing)
elif move == "R":
facing = (facing + 1) % 4
elif move == "L":
facing = (facing - 1) % 4
ans = 1000 * (at[0] + 1) + 4 * (at[1] + 1) + facing
print(ans)
submit(ans)