[py] Port 2016_01
This commit is contained in:
parent
d27285ef23
commit
bd2e910569
3 changed files with 75 additions and 74 deletions
|
|
@ -1,74 +0,0 @@
|
|||
import sys
|
||||
|
||||
def load_steps(filename):
|
||||
steps = []
|
||||
with open(filename, "r") as f:
|
||||
for step in f.read()[:-1].split(","):
|
||||
step = step.strip()
|
||||
step = (step[0], int(step[1:]))
|
||||
steps.append(step)
|
||||
return steps
|
||||
|
||||
# PART 1
|
||||
|
||||
def turn(direction, where):
|
||||
dir_x, dir_y = direction
|
||||
|
||||
if where == "R":
|
||||
return (dir_y, -dir_x)
|
||||
elif where == "L":
|
||||
return (-dir_y, dir_x)
|
||||
else:
|
||||
raise Exception("HEY! Don't do that!")
|
||||
|
||||
def manhattan(position):
|
||||
x, y = position
|
||||
return abs(x) + abs(y)
|
||||
|
||||
def find_distance(steps):
|
||||
pos_x, pos_y = 0, 0
|
||||
direction = (1, 0)
|
||||
|
||||
for step in steps:
|
||||
where, amount = step
|
||||
direction = turn(direction, where)
|
||||
dir_x, dir_y = direction
|
||||
pos_x += dir_x * amount
|
||||
pos_y += dir_y * amount
|
||||
|
||||
return manhattan((pos_x, pos_y))
|
||||
|
||||
# PART 2
|
||||
|
||||
def first_visited_twice(steps):
|
||||
pos_x, pos_y = 0, 0
|
||||
direction = (1, 0)
|
||||
visited = {(pos_x, pos_y)}
|
||||
|
||||
for step in steps:
|
||||
where, amount = step
|
||||
direction = turn(direction, where)
|
||||
dir_x, dir_y = direction
|
||||
|
||||
for i in range(amount):
|
||||
pos_x += dir_x
|
||||
pos_y += dir_y
|
||||
position = (pos_x, pos_y)
|
||||
|
||||
if position in visited:
|
||||
return position
|
||||
else:
|
||||
visited.add(position)
|
||||
|
||||
def main(filename):
|
||||
steps = load_steps(filename)
|
||||
print(f"Solutions for {filename}")
|
||||
distance = find_distance(steps)
|
||||
print(f"Part 1: {distance}")
|
||||
pos = first_visited_twice(steps)
|
||||
distance_2 = manhattan(pos)
|
||||
print(f"Part 2: {distance_2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
|
|
@ -2,6 +2,7 @@ import sys
|
|||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from .y2016 import d01
|
||||
from .y2017 import d01, d02, d03, d04, d05, d06, d07, d08, d09
|
||||
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
||||
from .y2020 import d10
|
||||
|
|
@ -9,6 +10,7 @@ from .y2021 import d14
|
|||
from .y2022 import d01, d02, d03, d04, d05, d06
|
||||
|
||||
DAYS = {
|
||||
"2016_01": y2016.d01.solve,
|
||||
"2017_01": y2017.d01.solve,
|
||||
"2017_02": y2017.d02.solve,
|
||||
"2017_03": y2017.d03.solve,
|
||||
|
|
|
|||
73
py/aoc/y2016/d01.py
Normal file
73
py/aoc/y2016/d01.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
def load_steps(inputstr):
|
||||
steps = []
|
||||
for step in inputstr[:-1].split(","):
|
||||
step = step.strip()
|
||||
step = (step[0], int(step[1:]))
|
||||
steps.append(step)
|
||||
return steps
|
||||
|
||||
|
||||
# PART 1
|
||||
|
||||
|
||||
def turn(direction, where):
|
||||
dir_x, dir_y = direction
|
||||
|
||||
if where == "R":
|
||||
return (dir_y, -dir_x)
|
||||
elif where == "L":
|
||||
return (-dir_y, dir_x)
|
||||
else:
|
||||
raise Exception("HEY! Don't do that!")
|
||||
|
||||
|
||||
def manhattan(position):
|
||||
x, y = position
|
||||
return abs(x) + abs(y)
|
||||
|
||||
|
||||
def find_distance(steps):
|
||||
pos_x, pos_y = 0, 0
|
||||
direction = (1, 0)
|
||||
|
||||
for step in steps:
|
||||
where, amount = step
|
||||
direction = turn(direction, where)
|
||||
dir_x, dir_y = direction
|
||||
pos_x += dir_x * amount
|
||||
pos_y += dir_y * amount
|
||||
|
||||
return manhattan((pos_x, pos_y))
|
||||
|
||||
|
||||
# PART 2
|
||||
|
||||
|
||||
def first_visited_twice(steps):
|
||||
pos_x, pos_y = 0, 0
|
||||
direction = (1, 0)
|
||||
visited = {(pos_x, pos_y)}
|
||||
|
||||
for step in steps:
|
||||
where, amount = step
|
||||
direction = turn(direction, where)
|
||||
dir_x, dir_y = direction
|
||||
|
||||
for i in range(amount):
|
||||
pos_x += dir_x
|
||||
pos_y += dir_y
|
||||
position = (pos_x, pos_y)
|
||||
|
||||
if position in visited:
|
||||
return position
|
||||
else:
|
||||
visited.add(position)
|
||||
|
||||
|
||||
def solve(inputstr):
|
||||
steps = load_steps(inputstr)
|
||||
distance = find_distance(steps)
|
||||
print(f"Part 1: {distance}")
|
||||
pos = first_visited_twice(steps)
|
||||
distance_2 = manhattan(pos)
|
||||
print(f"Part 2: {distance_2}")
|
||||
Loading…
Add table
Add a link
Reference in a new issue