Skip to content

Commit 7b894ac

Browse files
committed
2024.06
1 parent 84dfa73 commit 7b894ac

File tree

7 files changed

+118
-2
lines changed

7 files changed

+118
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# [Day 6: Guard Gallivant](https://adventofcode.com/2024/day/6)
2+
3+
Part 2 is:
4+
* higher than 456
5+
* less than 1689
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import sys
2+
from gridutil import grid, coord
3+
from typing import Optional
4+
from tqdm import tqdm
5+
6+
7+
def parse(instr: str) -> grid.Grid:
8+
return grid.parse(instr)
9+
10+
11+
def find_start(g: grid.Grid) -> coord.Coordinate:
12+
for pos in g:
13+
if g[pos] == "^":
14+
return pos
15+
assert False, "no start point found"
16+
17+
18+
def modplus(x: int) -> int:
19+
return (x + 1) % 4
20+
21+
22+
dirs = [coord.Direction.Up, coord.Direction.Right, coord.Direction.Down, coord.Direction.Left]
23+
24+
25+
class LoopEncounteredException(Exception):
26+
pass
27+
28+
29+
def trace(g: grid.Grid, guard_pos: coord.Coordinate, guard_direction: int) -> set[tuple[coord.Coordinate, int]]:
30+
visited_sequence = set()
31+
32+
while guard_pos in g:
33+
if (guard_pos, guard_direction) in visited_sequence:
34+
raise LoopEncounteredException
35+
36+
visited_sequence.add((guard_pos, guard_direction))
37+
38+
nc = coord.add(guard_pos, dirs[guard_direction % 4].delta())
39+
if nc in g and g[nc] == "#":
40+
guard_direction = modplus(guard_direction)
41+
else:
42+
guard_pos = nc
43+
44+
return visited_sequence
45+
46+
47+
def one(instr: str) -> int:
48+
g = parse(instr)
49+
return len(set(map(lambda x: x[0], trace(g, find_start(g), 0))))
50+
51+
52+
def two(instr: str) -> int:
53+
g = parse(instr)
54+
55+
start_pos = find_start(g)
56+
seq = trace(g, start_pos, 0)
57+
known_blocks = set()
58+
59+
for (pos, _) in tqdm(seq, file=sys.stderr):
60+
assert pos in g, "pos off the rails"
61+
g[pos] = "#"
62+
try:
63+
trace(g, start_pos, 0)
64+
except LoopEncounteredException:
65+
known_blocks.add(pos)
66+
g[pos] = "."
67+
68+
return len(known_blocks)
69+
70+
71+
def _debug(*args, **kwargs):
72+
kwargs["file"] = sys.stderr
73+
print(*args, **kwargs)
74+
75+
76+
if __name__ == "__main__":
77+
if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]:
78+
print("Missing day argument", file=sys.stderr)
79+
sys.exit(1)
80+
inp = sys.stdin.read().strip()
81+
if sys.argv[1] == "1":
82+
print(one(inp))
83+
else:
84+
print(two(inp))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
....#.....
2+
.........#
3+
..........
4+
..#.......
5+
.......#..
6+
..........
7+
.#..^.....
8+
........#.
9+
#.........
10+
......#...
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"1": [
3+
{
4+
"is": "41",
5+
"input": "....#.....\n.........#\n..........\n..#.......\n.......#..\n..........\n.#..^.....\n........#.\n#.........\n......#...\n\n"
6+
}
7+
],
8+
"2": [
9+
{
10+
"is": "6",
11+
"input": "....#.....\n.........#\n..........\n..#.......\n.......#..\n..........\n.#..^.....\n........#.\n#.........\n......#...\n"
12+
}
13+
]
14+
}

challenges/2024/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Solutions to the [2024 Advent of Code](https://adventofcode.com/2024)!
44

55
---
66

7-
Total stars: **10**
7+
Total stars: **12**
88

99
![Benchmark graph](./benchmark-graph.png)
1010

@@ -18,4 +18,5 @@ A day denoted with an asterisk means it has a visualisation.
1818
| 02 - Red-Nosed Reindeer | ★ ★ | Python ||
1919
| 03 - Mull It Over | ★ ★ | Python | The first instance of Advent of Parsing this year! |
2020
| 04* - Ceres Search | ★ ★ | Python | When it says a cross, it does not mean a plus. |
21-
| 05 - Print Queue | ★ ★ | Python | Before you dismiss and idea as being "too simple", make sure you check that it doesn't work. |
21+
| 05 - Print Queue | ★ ★ | Python | Before you dismiss and idea as being "too simple", make sure you check that it doesn't work. |
22+
| 06 - Guard Gallivant | ★ ★ | Python | oh dear runtime (also I knew what I wanted to do for so long it just took me 3 hours to implement it properly) |

challenges/2024/benchmark-graph.png

4.73 KB
Loading

challenges/2024/benchmarks.jsonl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@
88
{"day": 4, "part": 2, "runner": "py", "min": 0.05280470848083496, "max": 0.06299543380737305, "avg": 0.05627016305923462, "n": 100}
99
{"day": 5, "part": 1, "runner": "py", "min": 0.02001357078552246, "max": 0.030559301376342773, "avg": 0.02152919292449951, "n": 100}
1010
{"day": 5, "part": 2, "runner": "py", "min": 0.02507805824279785, "max": 0.03197765350341797, "avg": 0.027084295749664308, "n": 100}
11+
{"day": 6, "part": 1, "runner": "py", "min": 0.0671079158782959, "max": 0.0671079158782959, "avg": 0.0671079158782959, "n": 1}
12+
{"day": 6, "part": 2, "runner": "py", "min": 61.63975167274475, "max": 61.63975167274475, "avg": 61.63975167274475, "n": 1}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy