2024(6): add verbose bool and write idea for part two out

This commit is contained in:
2024-12-24 17:51:44 +01:00
parent 2144ecc5fb
commit 923f63b2ce
2 changed files with 46 additions and 14 deletions

View File

@ -2,12 +2,7 @@ import numpy as np
from enum import Enum
'''
idee:
sla ook het kleine voorbeeld op
guard_directions = ["^", ">", "v", "<"]
guard_path = [] # list of tuples (x, y)
'''
verbose = False
class Map:
directions = ["^", ">", "v", "<"]
@ -30,20 +25,25 @@ class Map:
"""
next_pos = tuple([self.pos[i] + self.step_per_direction[self.directions.index(self.direction)][i] for i in range(len(self.pos))])
print("next_pos", next_pos)
if verbose:
print("next_pos", next_pos)
return next_pos
def pos_in_map(self, pos):
"""
Returns whether pos is in the map.
"""
print("pos_in_map", pos)
if verbose:
print("pos_in_map", pos)
return pos[0] >= 0 and pos[0] < self.map.shape[0] \
and pos[1] >= 0 and pos[0] < self.map.shape[1]
def look_ahead(self):
next_step = self.next_pos()
print("look_ahead.next_step", next_step)
if verbose:
print("look_ahead.next_step", next_step)
if not self.pos_in_map(next_step):
return self.Tiles.BORDER
if self.map[*next_step] == "#":
@ -89,10 +89,11 @@ class Map:
# Find the location of the guard in the map.
guards = self.seek_guard_in_map()
print(guards)
if verbose:
print(guards)
assert len(guards) == 1, "There should only be one guard in the map."
print(guards)
self.pos = guards[0]
self.trace.append(self.pos)
self.direction = m.map[self.pos]
@ -106,9 +107,11 @@ class Map:
"""
next_tile = self.look_ahead()
print("step.next_tile", next_tile)
if verbose:
print("step.next_tile", next_tile)
if next_tile == self.Tiles.FREE:
# TODO: Really take a step! Do not only turn.
self.pos = self.next_pos()
self.trace.append(self.pos)
return True
@ -137,7 +140,8 @@ if __name__ == "__main__":
m.load_map("input")
while m.step():
print(m.pos, m.direction)
if verbose:
print(m.pos, m.direction)
#m.show()
print(len(m.trace), m.trace)
#print(len(m.trace), m.trace)
print(len(set(m.trace)))