add day6 part 1

This commit is contained in:
Dmitry Fedotov
2024-12-15 00:52:00 +03:00
parent 3658678bdb
commit 0f2951b9e1
4 changed files with 458 additions and 0 deletions

138
day6/code.py Normal file
View File

@@ -0,0 +1,138 @@
import sys
sys.path.append('../aoclib')
from aoclib import Input
from time import sleep
D_U = '^'
D_R = '>'
D_D = 'v'
D_L = '<'
class Guard(object):
dirs = [D_U, D_R, D_D, D_L]
def __init__(self, x, y, dir: str):
self.d = dir
self.x = x
self.y = y
def turn(self):
self.d = self.dirs[(self.dirs.index(self.d) + 1) % len(self.dirs)]
def curr(self):
return self.x, self.y
def want(self):
if self.d == D_U:
return self.x, self.y-1
elif self.d == D_R:
return self.x+1, self.y
elif self.d == D_D:
return self.x, self.y+1
elif self.d == D_L:
return self.x-1, self.y
def next(self):
self.x, self.y = self.want()
def __str__(self):
return self.d
class Map(list):
def __init__(self, l: list):
super().__init__(l)
def has_obstacle(self, x, y: int) -> bool:
return self[y][x] == '#'
def out_of_bounds(self, x, y):
return (x < 0 or y < 0) or (x >= len(self[0]) or y >= len(self))
class Lab(object):
def __init__(self, m: Map, g: Guard):
self.m = m
self.g = g
self.visited = set()
self.print_visited = False
self.g_init = Guard(g.x, g.y, g.d)
def next(self) -> bool:
self.visited.add(self.g.curr())
x, y = self.g.want()
if self.m.out_of_bounds(x, y):
return False
elif self.m.has_obstacle(x, y):
self.g.turn()
return True
self.g.next()
return True
def reset(self):
self.visited = set()
self.g = Guard(self.g_init.x, self.g_init.y, self.g_init.d)
def solve_part1(self):
while self.next():
pass
return len(self.visited)
def animate(self, visited=False):
'''
Just wanted to show kids what it looks like
'''
cls = lambda: print(chr(27) + "[2J")
self.print_visited = visited
cls()
print('Watch this')
sleep(1)
cls()
print(self)
while self.next():
cls()
print(self)
sleep(0.5)
def __str__(self):
out = [l.copy() for l in self.m]
if self.print_visited:
for t in self.visited:
x, y = t
out[y][x] = 'X'
out[self.g.y][self.g.x] = self.g.__str__()
return '\n'.join([''.join(line) for line in out])
def parse_input(lst) -> (Lab):
g, m = None, None
for y, l in enumerate(lst):
if D_U in l:
x = l.index('^')
g = Guard(x, y, D_U)
lst[y][x] = '.'
m = Map(lst)
return Lab(m, g)
if __name__ == '__main__':
lines = Input('input_test.txt').lines_as_lists()
#lines = Input('input.txt').lines_as_lists()
lab = parse_input(lines)
#lab.animate(True)
lab.animate()
lab.reset()
#part 1
print('part 1:', lab.solve_part1())
#part 2
print('part 2:', '')

130
day6/input.txt Normal file
View File

@@ -0,0 +1,130 @@
.......................#...................#....#..#......#.................#.#..#........................#.#..............#.....#
................#.#.......#..#.......#.................................................................................#.##.......
............#..............#......................#...#.....#...#..........##.........#.....#.....................................
..#...#......................................#............................#..................#..........#..#......................
....#........#.#....................#..........#.....#.............#..#............................#..................#......#....
..........................................................#................#............................................#.........
..#............................#.....#...........................#...........#........##...........................#..#...........
.....................#...........................#.#..............#......................................#.........#....#.....#...
.#...#.#.................#....................#...............................................................#.#..........#......
....#....................................#...........................................#.....#..........#...........................
.......................#..........#..............#......#....#.......#........................................#.#.................
......#...........................................................#...#............#.................#............................
.......#.................#..#.....................#...............................................................................
..............#....................#.#..........#........#......#................#.................#..........#...................
#.#.......................................#.........................#...................................#.........................
...#.........................................................................................#................#..................#
#.....#....#............................................................#.........................#..................#.#..........
.#.....#.#.......#.#..................#.#......................#.......#.....#..#..................##.....#.......#...#...........
....#.........#.................................#..........#........#...............#................#...................#....#...
.................#...............#...#.................#.................................................#........................
....#...#...............................#..................................................#..........#................#.....#....
...........#........#....#.......#...#..........#...............#.....#.....#.....#..........#....................................
............#.........#.................#................#.................#.#..................................................#.
............#......................................#...................................................................#..........
...........#....#.....................................................................................#............#..........#...
..........#........................................#.............#.................#........................#..#.......#........#.
......#.............................#.................##..................................#.....................#.................
.#.................................#.............................................#.................##.....#..............#......#.
.........................#..............................#..........#...........#....#................#......................#.....
.......#...................#......##........................#.........#...........................................................
......................................................................##...........#................#.............................
....#...................................##...............................................#........................................
...##.....#..#........#.................#..............................................................#..........................
........##......#....................#..............................#......................#...................#..................
.........................................................#.......................................................#.#..............
..........#.............................#.........#...............#................#.#.........#..........................#.#.....
....................#...................#..#.......................................................#..........#...............#...
...........................................................#........................#.........#........#..........................
.......#..............#......#.................................#...................##...........................#.........#.......
#...#...............................................................................................................#.............
................................#..#....#............#...........#..................#.............................#...............
....................#..............#.........#.................................#.............#..##................................
#...#..................................................................................#.....#...........#........................
........#...........#........#...#..................#.....#....#.........#.........................#...........................#..
.............#...#...................................................................#...........#................................
.............#........................................#...#.............#..............................#..#.........#.............
...#.................................................................................#.............#..........................##..
...#..............................................................................................................................
................#.........#..................................#...................#................................................
...#................#...#.#.....................#...........#..................................#........#....#......#.............
..........................#.....................#..#...................................##........................................#
#........................#...............................................#........#..#..........#...........................#.....
.#............#.#..............................................................#...........#......................................
......#..................................................................#.......#..............................#...#....#........
........#........................#.........#.............#.....#..................................................................
...............................................................................#...........................................#......
........................#..................................#.....................................................#................
............................................................#..........................#.................#.......................#
.....................#..#.........................#..............#.............................#........#..............##......#..
.....................#.................................................#.......#............#........................#.......#....
......................#.....................................................#......#............................#.................
................................................................#...#..##..#.....................................#...............#
..............#..............................................................................................#....................
............................................#......#..........#...............................................................#...
....#.................................#..................................................................#..........##..#.........
............#.....#................................................................................#..........#.........#.........
.....................#......................................#.........#....................................................#....#.
.........................#...............................#...............................#....................#...................
.#.....................#.#.......................................#....#...........#.......................#.......................
.....................................................#.#..#......................................................................#
#..........#..........................#............................#.............#..................#.............#...........#...
........#.........................................................................................................................
..........#.............#.........................................#................................................#..............
...........#................................#.................#...........................#.............................#.........
..................#.........................................#.#..................#...#...........#.......#..#.....................
........................................................#...#.......................................................#.............
.........................................................................#.......#..#.....#.......................................
............#..............................................................#.........#..............#.............#...............
#...#..........................................#...........#.....#.....................................#...............#..........
...........................#............#.........................................................................................
...#..........................................#.......................................................................#...........
..............#......#.....#.....................#..........................#.....................................................
.........................#....#...................#...#.......#..............................#........................#...........
.............................#............^.#................................................#......#..#.#...........#............
....................................................................................#...#.#...................................#...
.......................#..............................##..............................................#...........................
......................#...................................................#..........................................#............
......#........................................................................#.....................................#............
.......................#.............................#........................#...........#...........#............#...........#..
....#...............#..#......#....#..................#.............#.......#...............#.......................#.............
............#...#..................#..............................................................................................
....#.......#.................#.......................................................#..#......#.................................
.........#......#..........#......................#.....................#..................#.#....................................
...............##.......................................................#...#.............................#.......#...............
........................................................##...................#...#............#......#............................
...........#...#..............#.............................................................#...................#............#....
...............................................#.....#.....#......................................................................
.#....................#...#........#....................#.#.........#..........#................................#................#
.............#.....#..#....................................#.#...........#.................................................#..#.#.
......#.....................................#....................................................................#................
............................................................##......#.....................#.......#.....................#...#.....
.#.......#......................#.................................................................................................
................#....#......................#.........#...................#........#.......##.....#...............................
..#....#....................#............................................................#.#......#............#.....#.#....#.....
.................................#.................................................#.......#......................................
..................#...........#....#.....................#......#...............#..................#....................##........
.##...............................................#............#...........................#......................................
.............#...........#...........................#..#.......#...#..........#......................#..........#................
.......#...................................#.................#....#..............................................................#
........#....................................................#.................#.....#.........#.#................................
................#.................................#.....................#.....#.....#............................##...............
..............................................##.................#..##........#...........#............#..........................
....#......................................#...............................................................................#......
..#..............#.#................#..#..............#.......................................#.#.......#..............#......#..#
.....#..............................................................................................................#.......#.....
..#.......#.#..........#.......#..................................#..........#.......#.................#...........#...........#..
...............#.....#................#............#................#............#.....#.#........#...............................
....#.....................#................#.....................................#......................#.#.......#....#..........
........#...............#...#.....................................................#........#.........#.........#..................
...#......#..........................#.....#..........................##.......#....#.............................................
.............#................................#............................#.....#.......#......#................................#
.......................#............#..........................#..................................................................
.................................................#..............#...#...........................#.................................
......#...................#..............................................................................................#....#..#
....................................#......#.....................#..............#.......#...................#.....................
............#...................................#...........#........#........#...........................................##......
#...........#..........................#....................................#.........#................................#..........
......##..#...............................................................#...............................#.........#.............
.......................#.....#.........................#...#.........................#.....#..........#.#...#.....................
.......................#..#.....#.....#..........................#.........#.....#....................#.....#....................#

10
day6/input_test.txt Normal file
View File

@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

180
day6/task.txt Normal file
View File

@@ -0,0 +1,180 @@
--- Day 6: Guard Gallivant ---
The Historians use their fancy device again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year 1518! It turns out that having direct access to history is very convenient for a group of historians.
You still have to be careful of time paradoxes, and so it will be important to avoid anyone from 1518 while The Historians search for the Chief. Unfortunately, a single guard is patrolling this part of the lab.
Maybe you can work out where the guard will go ahead of time so that The Historians can search safely?
You start by making a map (your puzzle input) of the situation. For example:
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
The map shows the current position of the guard with ^ (to indicate the guard is currently facing up from the perspective of the map). Any obstructions - crates, desks, alchemical reactors, etc. - are shown as #.
Lab guards in 1518 follow a very strict patrol protocol which involves repeatedly following these steps:
If there is something directly in front of you, turn right 90 degrees.
Otherwise, take a step forward.
Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit prototypes):
....#.....
....^....#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...
Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction:
....#.....
........>#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...
Reaching another obstacle (a spool of several very long polymers), she turns right again and continues downward:
....#.....
.........#
..........
..#.......
.......#..
..........
.#......v.
........#.
#.........
......#...
This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent):
....#.....
.........#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#v..
By predicting the guard's route, you can determine which specific positions in the lab will be in the patrol path. Including the guard's starting position, the positions visited by the guard before leaving the area are marked with an X:
....#.....
....XXXXX#
....X...X.
..#.X...X.
..XXXXX#X.
..X.X.X.X.
.#XXXXXXX.
.XXXXXXX#.
#XXXXXXX..
......#X..
In this example, the guard will visit 41 distinct positions on your map.
Predict the path of the guard. How many distinct positions will the guard visit before leaving the mapped area?
Your puzzle answer was 5153.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
While The Historians begin working around the guard's patrol route, you borrow their fancy device and step outside the lab. From the safety of a supply closet, you time travel through the last few months and record the nightly status of the lab's guard post on the walls of the closet.
Returning after what seems like only a few seconds to The Historians, they explain that the guard's patrol area is simply too large for them to safely search the lab without getting caught.
Fortunately, they are pretty sure that adding a single new obstruction won't cause a time paradox. They'd like to place the new obstruction in such a way that the guard will get stuck in a loop, making the rest of the lab safe to search.
To have the lowest chance of creating a time paradox, The Historians would like to know all of the possible positions for such an obstruction. The new obstruction can't be placed at the guard's starting position - the guard is there right now and would notice.
In the above example, there are only 6 different positions where a new obstruction would cause the guard to get stuck in a loop. The diagrams of these six situations use O to mark the new obstruction, | to show a position where the guard moves up/down, - to show a position where the guard moves left/right, and + to show a position where the guard moves both up/down and left/right.
Option one, put a printing press next to the guard's starting position:
....#.....
....+---+#
....|...|.
..#.|...|.
....|..#|.
....|...|.
.#.O^---+.
........#.
#.........
......#...
Option two, put a stack of failed suit prototypes in the bottom right quadrant of the mapped area:
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
......O.#.
#.........
......#...
Option three, put a crate of chimney-squeeze prototype fabric next to the standing desk in the bottom right quadrant:
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
.+----+O#.
#+----+...
......#...
Option four, put an alchemical retroencabulator near the bottom left corner:
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
..|...|.#.
#O+---+...
......#...
Option five, put the alchemical retroencabulator a bit to the right instead:
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
....|.|.#.
#..O+-+...
......#...
Option six, put a tank of sovereign glue right next to the tank of universal solvent:
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
.+----++#.
#+----++..
......#O..
It doesn't really matter what you choose to use as an obstacle so long as you and The Historians can put it into position without the guard noticing. The important thing is having enough options that you can find one that minimizes time paradoxes, and in this example, there are 6 different positions you could choose.
You need to get the guard stuck in a loop by adding a single new obstruction. How many different positions could you choose for this obstruction?