Advent of Code is a fun competition. Here is a link to the first day. Each day has two parts.
--- Day 1: No Time for a Taxicab ---
Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.
The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection.
There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the street grid of the city, how far is the shortest path to the destination?
For example:
Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away. R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away. R5, L5, R5, R3 leaves you 12 blocks away.
How many blocks away is Easter Bunny HQ?
And then part 2:
Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice.
For example, if your instructions are R8, R4, R4, R8, the first location you visit twice is 4 blocks away, due East.
How many blocks away is the first location you visit twice?
Going to definitely be some low hanging fruit here as I got it to work and then didn't really even touch it.
input = 'L1, L5, R1, R3, L4, L5, R5, R1, L2, L2, L3, R4, L2, R3, R1, L2, R5, R3, L4, R4, L3, R3, R3, L2, R1, L3, R2, L1, R4, L2, R4, L4, R5, L3, R1, R1, L1, L3, L2, R1, R3, R2, L1, R4, L4, R2, L189, L4, R5, R3, L1, R47, R4, R1, R3, L3, L3, L2, R70, L1, R4, R185, R5, L4, L5, R4, L1, L4, R5, L3, R2, R3, L5, L3, R5, L1, R5, L4, R1, R2, L2, L5, L2, R4, L3, R5, R1, L5, L4, L3, R4, L3, L4, L1, L5, L5, R5, L5, L2, L1, L2, L4, L1, L2, R3, R1, R1, L2, L5, R2, L3, L5, L4, L2, L1, L2, R3, L1, L4, R3, R3, L2, R5, L1, L3, L3, L3, L5, R5, R1, R2, L3, L2, R4, R1, R1, R3, R4, R3, L3, R3, L5, R2, L2, R4, R5, L4, L3, L1, L5, L1, R1, R2, L1, R3, R4, R5, R2, R3, L2, L1, L5'
dirs = input.split(', ')
def get_next_dirs(cur_xdir, cur_ydir, turn_dir):
if turn_dir == 'L':
if cur_xdir != 0:
return 0, cur_xdir
return -cur_ydir, 0
elif turn_dir == 'R':
if cur_xdir != 0:
return 0, -cur_xdir
return cur_ydir, 0
else:
raise RuntimeError('Unknown turn direction')
assert (get_next_dirs(0, 1, 'L') == (-1, 0))
assert (get_next_dirs(1, 0, 'L') == (0, 1))
assert (get_next_dirs(-1, 0, 'L') == (0, -1))
assert (get_next_dirs(0, -1, 'L') == (1, 0))
assert (get_next_dirs(0, 1, 'R') == (1, 0))
assert (get_next_dirs(1, 0, 'R') == (0, -1))
assert (get_next_dirs(-1, 0, 'R') == (0, 1))
assert (get_next_dirs(0, -1, 'R') == (-1, 0))
dx = 0
dy = 0
xdir = 0
ydir = 1
locations = set()
loc_found = False
duplicate_location = None
for d in dirs:
xdir, ydir = get_next_dirs(xdir, ydir, d[0])
steps = int(d[1:])
for s in range(steps):
loc = dx, dy
if loc not in locations:
locations.add(loc)
else:
if not loc_found:
duplicate_location = loc
loc_found = True
dx += xdir
dy += ydir
print('Part 1: dx={dx}, dy={dy}, dist={d}'.format(dx=dx, dy=dy, d=abs(dx) + abs(dy)))
print('Part 2: duplicated location={loc}'.format(loc=duplicate_location))
It's all in one script, for simplicity. It feels like the get_next_dirs
function would be easy to make better, but I'm not actually sure at first glance.
And I'm sure my hacky testing will get some feedback ;-) I'm never really sure when doing programming challenges like this whether to do a "real" unittest setup or just run basic tests with asserts. The asserts "work" well enough.