Skip to content

Instantly share code, notes, and snippets.

@Irene-123
Created December 21, 2020 12:48
Show Gist options
  • Save Irene-123/784654befec08c144e02795b7fde552b to your computer and use it in GitHub Desktop.
Save Irene-123/784654befec08c144e02795b7fde552b to your computer and use it in GitHub Desktop.
Advent of Code Day-12 part 1
with open("day12.txt", "r") as file:
lines = [(line.rstrip()[0], int(line.rstrip()[1:])) for line in file.readlines()]
dirs = ["E", "S", "W", "N", "L", "R", "F"]
curr_dir = "E"
curr_state = {"E":0, "W":0, "N":0, "S":0}
for line in lines:
if line[0] == "F":
curr_state[curr_dir] += line[1]
if line[0] == "R":
av = ["E", "S", "W", "N"]
angle = line[1]
v = int(angle/90)
ndir = (av.index(curr_dir)+v) % len(av)
ndir = av[ndir]
curr_dir = ndir
if line[0] == "L":
av = ["E", "N", "W", "S"]
angle = line[1]
v = int(angle/90)
ndir = (av.index(curr_dir)+v) % len(av)
ndir = av[ndir]
curr_dir = ndir
if line[0] == "N":
curr_state["N"] += line[1]
if line[0] == "S":
curr_state["S"] += line[1]
if line[0] == "W":
curr_state["W"] += line[1]
if line[0] == "E":
curr_state["E"] += line[1]
d = curr_state["E"]-curr_state["W"], curr_state["S"]-curr_state["N"]
print(f"Manhattan Distance of current positions from starting i.e. {curr_state} = {d[0]+d[1]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment