Alter 2 files
Update main.py Delete sample.txt
This commit is contained in:
parent
7b894ac8c9
commit
617c90bb14
2 changed files with 19 additions and 22 deletions
|
@ -13,28 +13,35 @@ def find_start(g: grid.Grid) -> coord.Coordinate:
|
||||||
if g[pos] == "^":
|
if g[pos] == "^":
|
||||||
return pos
|
return pos
|
||||||
assert False, "no start point found"
|
assert False, "no start point found"
|
||||||
|
|
||||||
|
|
||||||
def modplus(x: int) -> int:
|
def modplus(x: int) -> int:
|
||||||
return (x + 1) % 4
|
return (x + 1) % 4
|
||||||
|
|
||||||
|
|
||||||
dirs = [coord.Direction.Up, coord.Direction.Right, coord.Direction.Down, coord.Direction.Left]
|
dirs = [
|
||||||
|
coord.Direction.Up,
|
||||||
|
coord.Direction.Right,
|
||||||
|
coord.Direction.Down,
|
||||||
|
coord.Direction.Left,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class LoopEncounteredException(Exception):
|
class LoopEncounteredException(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def trace(g: grid.Grid, guard_pos: coord.Coordinate, guard_direction: int) -> set[tuple[coord.Coordinate, int]]:
|
def trace(
|
||||||
|
g: grid.Grid, guard_pos: coord.Coordinate, guard_direction: int
|
||||||
|
) -> set[tuple[coord.Coordinate, int]]:
|
||||||
visited_sequence = set()
|
visited_sequence = set()
|
||||||
|
|
||||||
while guard_pos in g:
|
while guard_pos in g:
|
||||||
if (guard_pos, guard_direction) in visited_sequence:
|
if (guard_pos, guard_direction) in visited_sequence:
|
||||||
raise LoopEncounteredException
|
raise LoopEncounteredException
|
||||||
|
|
||||||
visited_sequence.add((guard_pos, guard_direction))
|
visited_sequence.add((guard_pos, guard_direction))
|
||||||
|
|
||||||
nc = coord.add(guard_pos, dirs[guard_direction % 4].delta())
|
nc = coord.add(guard_pos, dirs[guard_direction % 4].delta())
|
||||||
if nc in g and g[nc] == "#":
|
if nc in g and g[nc] == "#":
|
||||||
guard_direction = modplus(guard_direction)
|
guard_direction = modplus(guard_direction)
|
||||||
|
@ -51,11 +58,11 @@ def one(instr: str) -> int:
|
||||||
|
|
||||||
def two(instr: str) -> int:
|
def two(instr: str) -> int:
|
||||||
g = parse(instr)
|
g = parse(instr)
|
||||||
|
|
||||||
start_pos = find_start(g)
|
start_pos = find_start(g)
|
||||||
seq = trace(g, start_pos, 0)
|
seq = trace(g, start_pos, 0)
|
||||||
known_blocks = set()
|
known_blocks = set()
|
||||||
|
|
||||||
for (pos, _) in tqdm(seq, file=sys.stderr):
|
for (pos, _) in tqdm(seq, file=sys.stderr):
|
||||||
assert pos in g, "pos off the rails"
|
assert pos in g, "pos off the rails"
|
||||||
g[pos] = "#"
|
g[pos] = "#"
|
||||||
|
@ -64,7 +71,7 @@ def two(instr: str) -> int:
|
||||||
except LoopEncounteredException:
|
except LoopEncounteredException:
|
||||||
known_blocks.add(pos)
|
known_blocks.add(pos)
|
||||||
g[pos] = "."
|
g[pos] = "."
|
||||||
|
|
||||||
return len(known_blocks)
|
return len(known_blocks)
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,4 +88,4 @@ if __name__ == "__main__":
|
||||||
if sys.argv[1] == "1":
|
if sys.argv[1] == "1":
|
||||||
print(one(inp))
|
print(one(inp))
|
||||||
else:
|
else:
|
||||||
print(two(inp))
|
print(two(inp))
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
....#.....
|
|
||||||
.........#
|
|
||||||
..........
|
|
||||||
..#.......
|
|
||||||
.......#..
|
|
||||||
..........
|
|
||||||
.#..^.....
|
|
||||||
........#.
|
|
||||||
#.........
|
|
||||||
......#...
|
|
Loading…
Add table
Add a link
Reference in a new issue