Skip to content

Commit 617c90b

Browse files
committed
Alter 2 files
Update main.py Delete sample.txt
1 parent 7b894ac commit 617c90b

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

challenges/2024/06-guardGallivant/main.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,35 @@ def find_start(g: grid.Grid) -> coord.Coordinate:
1313
if g[pos] == "^":
1414
return pos
1515
assert False, "no start point found"
16-
17-
16+
17+
1818
def modplus(x: int) -> int:
1919
return (x + 1) % 4
2020

21-
22-
dirs = [coord.Direction.Up, coord.Direction.Right, coord.Direction.Down, coord.Direction.Left]
21+
22+
dirs = [
23+
coord.Direction.Up,
24+
coord.Direction.Right,
25+
coord.Direction.Down,
26+
coord.Direction.Left,
27+
]
2328

2429

2530
class LoopEncounteredException(Exception):
2631
pass
2732

2833

29-
def trace(g: grid.Grid, guard_pos: coord.Coordinate, guard_direction: int) -> set[tuple[coord.Coordinate, int]]:
34+
def trace(
35+
g: grid.Grid, guard_pos: coord.Coordinate, guard_direction: int
36+
) -> set[tuple[coord.Coordinate, int]]:
3037
visited_sequence = set()
31-
38+
3239
while guard_pos in g:
3340
if (guard_pos, guard_direction) in visited_sequence:
3441
raise LoopEncounteredException
35-
42+
3643
visited_sequence.add((guard_pos, guard_direction))
37-
44+
3845
nc = coord.add(guard_pos, dirs[guard_direction % 4].delta())
3946
if nc in g and g[nc] == "#":
4047
guard_direction = modplus(guard_direction)
@@ -51,11 +58,11 @@ def one(instr: str) -> int:
5158

5259
def two(instr: str) -> int:
5360
g = parse(instr)
54-
61+
5562
start_pos = find_start(g)
5663
seq = trace(g, start_pos, 0)
5764
known_blocks = set()
58-
65+
5966
for (pos, _) in tqdm(seq, file=sys.stderr):
6067
assert pos in g, "pos off the rails"
6168
g[pos] = "#"
@@ -64,7 +71,7 @@ def two(instr: str) -> int:
6471
except LoopEncounteredException:
6572
known_blocks.add(pos)
6673
g[pos] = "."
67-
74+
6875
return len(known_blocks)
6976

7077

@@ -81,4 +88,4 @@ def _debug(*args, **kwargs):
8188
if sys.argv[1] == "1":
8289
print(one(inp))
8390
else:
84-
print(two(inp))
91+
print(two(inp))

challenges/2024/06-guardGallivant/sample.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)