Added template and 2016
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
## \-\-- Day 1: No Time for a Taxicab \-\--
|
||||
|
||||
Santa\'s sleigh uses a [very high-precision
|
||||
clock]{title="An atomic clock is too inaccurate; he might end up in a wall!"}
|
||||
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](https://en.wikipedia.org/wiki/Taxicab_geometry), 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?
|
||||
|
||||
Your puzzle answer was `236`.
|
||||
|
||||
## \-\-- Part Two \-\-- {#part2}
|
||||
|
||||
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*?
|
||||
|
||||
Your puzzle answer was `182`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars:
|
||||
\*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2016) and
|
||||
try another puzzle.
|
||||
|
||||
If you still want to see it, you can [get your puzzle
|
||||
input](1/input).
|
||||
@@ -0,0 +1,84 @@
|
||||
#!/bin/python3
|
||||
import sys
|
||||
from pprint import pprint
|
||||
|
||||
# Read instructions from file
|
||||
with open("input", "r") as file:
|
||||
instructions = file.read().strip().split(", ")
|
||||
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
# #
|
||||
#########################################
|
||||
|
||||
# Define directions and movements in terms of (x, y) vectors
|
||||
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # North, East, South, West
|
||||
|
||||
# Starting position and direction
|
||||
x, y = 0, 0
|
||||
current_direction = 0 # Start facing North (index 0 in directions)
|
||||
|
||||
# Process each instruction
|
||||
for instruction in instructions:
|
||||
turn, steps = instruction[0], int(instruction[1:])
|
||||
|
||||
# Update direction based on the turn
|
||||
if turn == 'R':
|
||||
current_direction = (current_direction + 1) % 4 # Turn right
|
||||
elif turn == 'L':
|
||||
current_direction = (current_direction - 1) % 4 # Turn left
|
||||
|
||||
# Move in the current direction
|
||||
dx, dy = directions[current_direction]
|
||||
x += dx * steps
|
||||
y += dy * steps
|
||||
|
||||
# Calculate Manhattan distance from the origin
|
||||
distance = abs(x) + abs(y)
|
||||
print(distance)
|
||||
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
|
||||
|
||||
# Define directions and movements in terms of (x, y) vectors
|
||||
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # North, East, South, West
|
||||
|
||||
# Starting position and direction
|
||||
x, y = 0, 0
|
||||
current_direction = 0 # Start facing North (index 0 in directions)
|
||||
visited = set() # To track visited positions
|
||||
visited.add((x, y)) # Starting point is visited
|
||||
|
||||
# Read instructions from file
|
||||
with open("input", "r") as file:
|
||||
instructions = file.read().strip().split(", ")
|
||||
|
||||
# Process each instruction
|
||||
for instruction in instructions:
|
||||
turn, steps = instruction[0], int(instruction[1:])
|
||||
|
||||
# Update direction based on the turn
|
||||
if turn == 'R':
|
||||
current_direction = (current_direction + 1) % 4 # Turn right
|
||||
elif turn == 'L':
|
||||
current_direction = (current_direction - 1) % 4 # Turn left
|
||||
|
||||
# Move step by step in the current direction
|
||||
dx, dy = directions[current_direction]
|
||||
for _ in range(steps):
|
||||
x += dx
|
||||
y += dy
|
||||
if (x, y) in visited:
|
||||
# Found the first location visited twice
|
||||
distance = abs(x) + abs(y)
|
||||
print(distance)
|
||||
exit()
|
||||
visited.add((x, y))
|
||||
|
||||
Reference in New Issue
Block a user