-
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
Showing
9 changed files
with
179 additions
and
5 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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""2024 - Day 5 Part 1: ...""" | ||
"""2024 - Day 5 Part 1: Print Queue""" | ||
|
||
from collections import defaultdict | ||
|
||
|
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
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,63 @@ | ||
"""2024 - Day 6 Part 1: Guard Gallivant""" | ||
|
||
SHIFTS = { | ||
# r c | ||
"^": (-1, 0), | ||
">": (0, +1), | ||
"v": (+1, 0), | ||
"<": (0, -1), | ||
} | ||
|
||
TURN = { | ||
"^": ">", | ||
">": "v", | ||
"v": "<", | ||
"<": "^", | ||
} | ||
|
||
|
||
def find_guard(data: list[list[str]]) -> tuple[int, int]: | ||
rows = len(data) | ||
cols = len(data[0]) | ||
|
||
for r in range(rows): | ||
for c in range(cols): | ||
if data[r][c] == "^": | ||
return r, c | ||
|
||
raise ValueError | ||
|
||
|
||
def solve(task: str) -> int: | ||
data = [list(line) for line in task.split("\n")] | ||
|
||
gr, gc = find_guard(data) | ||
|
||
rows = len(data) | ||
cols = len(data[0]) | ||
|
||
visited = {(gr, gc)} | ||
|
||
while True: | ||
guard = data[gr][gc] | ||
|
||
dr, dc = SHIFTS[guard] | ||
|
||
nr = gr + dr | ||
nc = gc + dc | ||
|
||
if not (0 <= nr < rows and 0 <= nc < cols): | ||
break # out of border | ||
|
||
if data[nr][nc] != "#": | ||
data[nr][nc] = guard | ||
|
||
gr = nr | ||
gc = nc | ||
else: | ||
new_guard = TURN[guard] | ||
data[gr][gc] = new_guard | ||
|
||
visited.add((gr, gc)) | ||
|
||
return len(visited) |
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,65 @@ | ||
"""2024 - Day 6 Part 2: Guard Gallivant""" | ||
|
||
from src.year2024.day06a import SHIFTS | ||
from src.year2024.day06a import TURN | ||
from src.year2024.day06a import find_guard | ||
|
||
|
||
def is_paradox(gr: int, gc: int, data: list[list[str]]) -> bool: | ||
rows = len(data) | ||
cols = len(data[0]) | ||
|
||
visited: set[tuple[str, int, int]] = set() | ||
|
||
while True: | ||
guard = data[gr][gc] | ||
|
||
if (guard, gr, gc) in visited: | ||
return True | ||
|
||
visited.add((guard, gr, gc)) | ||
|
||
dr, dc = SHIFTS[guard] | ||
|
||
nr = gr + dr | ||
nc = gc + dc | ||
|
||
if not (0 <= nr < rows and 0 <= nc < cols): | ||
data[gr][gc] = "." | ||
return False # out of border | ||
|
||
if data[nr][nc] not in {"#", "O"}: | ||
data[gr][gc] = "." | ||
data[nr][nc] = guard | ||
|
||
gr = nr | ||
gc = nc | ||
else: | ||
new_guard = TURN[guard] | ||
data[gr][gc] = new_guard | ||
|
||
|
||
def solve(task: str) -> int: | ||
data = [list(line) for line in task.split("\n")] | ||
|
||
gr, gc = find_guard(data) | ||
|
||
rows = len(data) | ||
cols = len(data[0]) | ||
|
||
count = 0 | ||
|
||
for r in range(rows): | ||
for c in range(cols): | ||
if data[r][c] == "#" or (r, c) == (gr, gc): | ||
continue | ||
|
||
data[r][c] = "O" | ||
data[gr][gc] = "^" | ||
|
||
if is_paradox(gr, gc, data): | ||
count += 1 | ||
|
||
data[r][c] = "." | ||
|
||
return count |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""2024 - Day 5 Part 1: ...""" | ||
"""2024 - Day 5 Part 1: Print Queue""" | ||
|
||
from textwrap import dedent | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""2024 - Day 5 Part 2: ...""" | ||
"""2024 - Day 5 Part 2: Print Queue""" | ||
|
||
from textwrap import dedent | ||
|
||
|
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,23 @@ | ||
"""2024 - Day 6 Part 1: Guard Gallivant""" | ||
|
||
from textwrap import dedent | ||
|
||
from src.year2024.day06a import solve | ||
|
||
|
||
def test_solve(): | ||
task = dedent( | ||
""" | ||
....#..... | ||
.........# | ||
.......... | ||
..#....... | ||
.......#.. | ||
.......... | ||
.#..^..... | ||
........#. | ||
#......... | ||
......#... | ||
""" | ||
).strip() | ||
assert solve(task) == 41 |
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,23 @@ | ||
"""2024 - Day 6 Part 2: Guard Gallivant""" | ||
|
||
from textwrap import dedent | ||
|
||
from src.year2024.day06b import solve | ||
|
||
|
||
def test_solve(): | ||
task = dedent( | ||
""" | ||
....#..... | ||
.........# | ||
.......... | ||
..#....... | ||
.......#.. | ||
.......... | ||
.#..^..... | ||
........#. | ||
#......... | ||
......#... | ||
""" | ||
).strip() | ||
assert solve(task) == 6 |