-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19fd754
commit 46d8057
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from helper import get_input_split, print_result | ||
|
||
DAY = 1 | ||
|
||
inp = get_input_split(day=1, sep=",", strip=True) | ||
|
||
# Face directions | ||
NORTH = 0 | ||
EAST = 1 | ||
SOUTH = 2 | ||
WEST = 3 | ||
|
||
# Directions | ||
DIRS = {"R": 0, "L": 1} | ||
RIGHT = 0 | ||
LEFT = 1 | ||
|
||
# make_move[face_dir][dir] = (x_move, y_move, new_face_dir) | ||
make_move = ( | ||
((+1, 0, EAST), (-1, 0, WEST)), | ||
((0, -1, SOUTH), (0, +1, NORTH)), | ||
((-1, 0, WEST), (+1, 0, EAST)), | ||
((0, +1, NORTH), (0, -1, SOUTH)), | ||
) | ||
|
||
START_POS = [0, 0, NORTH] | ||
|
||
|
||
def p1(): | ||
pos = START_POS | ||
END_POS = () | ||
|
||
for move in inp: | ||
dir = DIRS[move[0]] | ||
dist = int(move[1:]) | ||
|
||
x, y, face = pos | ||
next_x, next_y, next_face = make_move[face][dir] | ||
next_x = x + next_x * dist | ||
next_y = y + next_y * dist | ||
pos = (next_x, next_y, next_face) | ||
END_POS = (next_x, next_y) | ||
|
||
return abs(END_POS[0]) + abs(END_POS[1]) | ||
|
||
|
||
def p2(): | ||
pos = START_POS | ||
points = set() | ||
points.add((0, 0)) | ||
END_POS = () | ||
|
||
for move in inp: | ||
dir = DIRS[move[0]] | ||
dist = int(move[1:]) | ||
|
||
x, y, face = pos | ||
next_x, next_y, next_face = make_move[face][dir] | ||
next_x = x + next_x * dist | ||
next_y = y + next_y * dist | ||
pos = (next_x, next_y, next_face) | ||
END_POS = (next_x, next_y) | ||
|
||
for i in range(x + 1, next_x): | ||
_pos = (i, y) | ||
if _pos in points: | ||
return abs(_pos[0]) + abs(_pos[1]) | ||
points.add(_pos) | ||
|
||
for i in range(x - 1, next_x, -1): | ||
_pos = (i, y) | ||
if _pos in points: | ||
return abs(_pos[0]) + abs(_pos[1]) | ||
points.add(_pos) | ||
|
||
for i in range(y + 1, next_y): | ||
_pos = (x, i) | ||
if _pos in points: | ||
return abs(_pos[0]) + abs(_pos[1]) | ||
points.add(_pos) | ||
|
||
for i in range(y - 1, next_y, -1): | ||
_pos = (x, i) | ||
if _pos in points: | ||
return abs(_pos[0]) + abs(_pos[1]) | ||
points.add(_pos) | ||
|
||
if END_POS in points: | ||
return abs(END_POS[0]) + abs(END_POS[1]) | ||
points.add(END_POS) | ||
|
||
|
||
print_result(DAY, p1(), p2()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
def get_input(day, no_strip=False): | ||
inp = [] | ||
|
||
with open(f"input/day_{0 if day < 10 else ''}{day}", "r") as f: | ||
inp = f.readlines() | ||
|
||
if no_strip: | ||
return inp | ||
return [x.strip() for x in inp] | ||
|
||
|
||
def get_input_split(day, sep=" ", strip=False): | ||
""" | ||
Split input | ||
""" | ||
|
||
inp = [] | ||
|
||
with open(f"input/day_{0 if day < 10 else ''}{day}", "r") as f: | ||
inp = f.read().split(sep) | ||
|
||
return [x.strip() for x in inp] if strip else inp | ||
|
||
|
||
def print_result(day, part1_sol, part2_sol): | ||
day = f"D{0 if day < 10 else ''}{day}" | ||
print(f"{day}P1: {part1_sol}") | ||
print(f"{day}P2: {part2_sol}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
R5, R4, R2, L3, R1, R1, L4, L5, R3, L1, L1, R4, L2, R1, R4, R4, L2, L2, R4, L4, R1, R3, L3, L1, L2, R1, R5, L5, L1, L1, R3, R5, L1, R4, L5, R5, R1, L185, R4, L1, R51, R3, L2, R78, R1, L4, R188, R1, L5, R5, R2, R3, L5, R3, R4, L1, R2, R2, L4, L4, L5, R5, R4, L4, R2, L5, R2, L1, L4, R4, L4, R2, L3, L4, R2, L3, R3, R2, L2, L3, R4, R3, R1, L4, L2, L5, R4, R4, L1, R1, L5, L1, R3, R1, L2, R1, R1, R3, L4, L1, L3, R2, R4, R2, L2, R1, L5, R3, L3, R3, L1, R4, L3, L3, R4, L2, L1, L3, R2, R3, L2, L1, R4, L3, L5, L2, L4, R1, L4, L4, R3, R5, L4, L1, L1, R4, L2, R5, R1, R1, R2, R1, R5, L1, L3, L5, R2 |