trying p2
This commit is contained in:
parent
311ab759a7
commit
2fe6b8bb7d
@ -17,7 +17,7 @@ class Vector:
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x}, {self.y}'
|
||||
|
||||
|
||||
def __eq__(self, value):
|
||||
return self.x == value.x and self.y == value.y
|
||||
|
||||
@ -29,20 +29,19 @@ class Guard:
|
||||
self.pos = p
|
||||
self.dir = d
|
||||
|
||||
def move(self, ob) -> bool:
|
||||
def move(self, ob) -> Vector | None | int:
|
||||
new_pos = self.pos + self.dir
|
||||
|
||||
# Check if need to turn
|
||||
if new_pos in ob:
|
||||
self.turn()
|
||||
return self.move(ob)
|
||||
return new_pos
|
||||
|
||||
# Check if move out of bounds
|
||||
if new_pos.y < 0 or new_pos.y >= y_bound or new_pos.x < 0 or new_pos.x >= x_bound:
|
||||
return False
|
||||
|
||||
return -1
|
||||
|
||||
self.pos = new_pos
|
||||
return True
|
||||
return None
|
||||
|
||||
|
||||
def turn(self):
|
||||
@ -67,7 +66,14 @@ starting_pos = guard.pos
|
||||
# Part 1
|
||||
been = [starting_pos]
|
||||
|
||||
while guard.move(obstacles):
|
||||
while True:
|
||||
ob = guard.move(obstacles)
|
||||
|
||||
if type(ob) == Vector:
|
||||
guard.turn()
|
||||
elif type(ob) == int:
|
||||
break
|
||||
|
||||
if guard.pos not in been:
|
||||
been.append(guard.pos)
|
||||
|
||||
@ -77,19 +83,44 @@ print(f'Part 1: {len(been)}')
|
||||
total = 0
|
||||
|
||||
been.remove(starting_pos)
|
||||
for place in been:
|
||||
for i, place in enumerate(been):
|
||||
print(i)
|
||||
temp_obstacles = obstacles.copy()
|
||||
temp_obstacles.append(place)
|
||||
|
||||
guard.pos = starting_pos
|
||||
guard.dir = Vector(0, -1)
|
||||
|
||||
# hit_new_ob_pos = None
|
||||
# hit_new_ob_dir = None
|
||||
|
||||
steps = 0
|
||||
while guard.move(temp_obstacles):
|
||||
steps += 1
|
||||
while True:
|
||||
ob = guard.move(temp_obstacles)
|
||||
|
||||
if type(ob) == Vector:
|
||||
# if ob == place and not hit_new_ob_pos:
|
||||
# hit_new_ob_pos = guard.pos
|
||||
# hit_new_ob_dir = guard.dir
|
||||
|
||||
# elif hit_new_ob_dir and hit_new_ob_pos:
|
||||
# if guard.pos == hit_new_ob_pos and guard.dir == hit_new_ob_dir:
|
||||
# total += 1
|
||||
# break
|
||||
|
||||
guard.turn()
|
||||
|
||||
|
||||
# guard.turn()
|
||||
|
||||
elif type(ob) == int:
|
||||
break
|
||||
else:
|
||||
steps += 1
|
||||
|
||||
|
||||
# Yup, this is what I count as in a loop...
|
||||
if steps >= 10 ** 5:
|
||||
if steps >= 6500:
|
||||
total += 1
|
||||
break
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user