Skip to content

Commit

Permalink
2024 day 5 part 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Dec 6, 2024
1 parent 6f1fb27 commit 4637e3f
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- 2021 - ★★★★★ ★★★★★ ★★★★★ ★★★
- 2022 - ★★★★★ ★★★★★ ★★★★★ ★☆
- 2023 - ★★★★★ ★★★★★ ★★★★★ ★★★☆
- 2024 - ★★★★★
- 2024 - ★★★★★

## How to use

Expand Down
2 changes: 1 addition & 1 deletion src/year2024/day05a.py
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

Expand Down
2 changes: 1 addition & 1 deletion src/year2024/day05b.py
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"""

import functools
from collections import defaultdict
Expand Down
63 changes: 63 additions & 0 deletions src/year2024/day06a.py
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)
65 changes: 65 additions & 0 deletions src/year2024/day06b.py
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
2 changes: 1 addition & 1 deletion tests/src/year2024/test_day05a.py
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

Expand Down
2 changes: 1 addition & 1 deletion tests/src/year2024/test_day05b.py
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

Expand Down
23 changes: 23 additions & 0 deletions tests/src/year2024/test_day06a.py
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
23 changes: 23 additions & 0 deletions tests/src/year2024/test_day06b.py
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

0 comments on commit 4637e3f

Please sign in to comment.