Advent of Code Solutions
I have set myself a personal challenge to try and get up to date with advent of code, a set of programming challenges released each December. Each day has 2 related problems, for a total of 50 problems per year. Each button below will show my code solving that problem. Green buttons where I have solved both problems of the day, orange where I have only solved the first one and red where I haven't solved either. Not started
Part 1 completed
Both parts completed
from util.input import get_input
def part_1():
puzzle_input = get_input(2016,1)
instructions = [x.strip() for x in puzzle_input.split(',')]
directions = ['N', 'E', 'S','W']
current_vertical = 0
current_horizontal = 0
current_direction = 'N'
for instruction in instructions:
turn = instruction[0]
distance = int(instruction[1:])
if turn == 'R':
current_direction = directions[(directions.index(current_direction) + 1) % 4]
else:
current_direction = directions[(directions.index(current_direction) - 1) % 4]
if current_direction == 'N':
current_vertical += distance
elif current_direction == 'E':
current_horizontal += distance
elif current_direction == 'S':
current_vertical -= distance
elif current_direction == 'W':
current_horizontal -= distance
return abs(current_horizontal) + abs(current_vertical)
def part_2():
puzzle_input = get_input(2016,1)
instructions = [x.strip() for x in puzzle_input.split(',')]
directions = ['N', 'E', 'S','W']
current_vertical = 0
current_horizontal = 0
current_direction = 'N'
visited_locations = set()
for instruction in instructions:
turn = instruction[0]
distance = int(instruction[1:])
if turn == 'R':
current_direction = directions[(directions.index(current_direction) + 1) % 4]
else:
current_direction = directions[(directions.index(current_direction) - 1) % 4]
for i in range(distance):
if current_direction == 'N':
current_vertical += 1
elif current_direction == 'E':
current_horizontal += 1
elif current_direction == 'S':
current_vertical -= 1
elif current_direction == 'W':
current_horizontal -= 1
if (current_vertical, current_horizontal) in visited_locations:
return abs(current_horizontal) + abs(current_vertical)
else:
visited_locations.add((current_vertical, current_horizontal))
return abs(current_horizontal) + abs(current_vertical)