Solve 2016 day 1 to get rolling again.

This commit is contained in:
2024-03-18 00:20:31 -04:00
parent c1a5120510
commit ba80eb7a74
3 changed files with 48 additions and 1 deletions

39
2016/d1.py Normal file
View File

@@ -0,0 +1,39 @@
from lib import *
EXAMPLE = "R5, L5, R5, R3"
EXAMPLE = "R2, R2, R2"
EXAMPLE = "R8, R4, R4, R8"
DIRS = "NESW"
MOVES = {
"N": (-1, 0),
"E": (0, 1),
"S": (1, 0),
"W": (0, -1),
}
def solve(input: Input, second=False):
steps = input.text.split(", ")
coord, dir = (0, 0), "N"
allcoords = set([coord])
for step in steps:
turn, nsteps = step[0], int(step[1:])
os = -1 if turn == "L" else 1
dir = DIRS[(DIRS.index(dir) + os) % len(DIRS)]
rowo, colo = MOVES[dir]
if second:
for n in range(1, nsteps):
tcoord = (coord[0] + rowo * n, coord[1] + colo * n)
if tcoord in allcoords:
return abs(tcoord[0]) + abs(tcoord[1])
allcoords.add(tcoord)
coord = (coord[0] + rowo * nsteps, coord[1] + colo * nsteps)
return abs(coord[0]) + abs(coord[1])
def main():
DAY_INPUT = "i1.txt"
print("Solution 1:", solve(Input(DAY_INPUT)))
print("Solution 2:", solve(Input(DAY_INPUT), True))
if __name__ == "__main__":
main()

1
2016/lib.py Symbolic link
View File

@@ -0,0 +1 @@
../lib.py

View File

@@ -24,4 +24,11 @@ written in Python.
- Day 17: 9:05
- Day 18: 10:39
- Day 19:
- Day 20:
- Day 20: 10:54
- Day 21: 25:52 cute bug where I didn't consider that no armor is an option
- Day 22: That was bad. Did not know how to choose between dfs/bfs and logic errors.
- Day 23:
# 2016
- Day 1: 29:00 That was emberassingly slow. Out of my rhythm?