2024 day 6 💀

This commit is contained in:
rawhide kobayashi 2024-12-06 19:36:09 -06:00
parent 68708d28f2
commit c364e8cf90
Signed by: rawhide_k
GPG Key ID: E71F77DDBC513FD7
3 changed files with 326 additions and 0 deletions

28
2024/Day 6/gold.py Normal file
View File

@ -0,0 +1,28 @@
with open('puzzle.txt', 'r') as f:
lines = f.read().splitlines()
xmas = 0
for y in range(len(lines)):
for x in range(len(lines[y])):
if lines[y][x] == 'A':
extra_check = []
if 0 < x < len(lines[y]) - 1 and 0 < y < len(lines) - 1:
if (lines[y - 1][x - 1] == 'M' and lines[y - 1][x + 1] == 'S' and
lines[y + 1][x - 1] == 'M' and lines[y + 1][x + 1] == 'S'):
xmas += 1
if (lines[y - 1][x - 1] == 'M' and lines[y - 1][x + 1] == 'M' and
lines[y + 1][x - 1] == 'S' and lines[y + 1][x + 1] == 'S'):
xmas += 1
if (lines[y - 1][x - 1] == 'S' and lines[y - 1][x + 1] == 'M' and
lines[y + 1][x - 1] == 'S' and lines[y + 1][x + 1] == 'M'):
xmas += 1
if (lines[y - 1][x - 1] == 'S' and lines[y - 1][x + 1] == 'S' and
lines[y + 1][x - 1] == 'M' and lines[y + 1][x + 1] == 'M'):
xmas += 1
print(xmas)

168
2024/Day 6/main.py Normal file
View File

@ -0,0 +1,168 @@
with open('puzzle.txt', 'r') as f:
lines = f.read().splitlines()
import time
import copy
guard_map = {}
guard_orientation = '^'
total_visits = 0
for line_index, line in enumerate(lines):
for column_index, column in enumerate(list(line)):
if line_index not in guard_map:
guard_map[line_index] = {}
if column_index not in guard_map[line_index]:
guard_map[line_index][column_index] = {
'value': line[column_index],
}
if guard_map[line_index][column_index]['value'] == '^':
guard_map[line_index][column_index]['value'] = 'X'
guard_initial_x = column_index
guard_initial_y = line_index
guard_x = guard_initial_x
guard_y = guard_initial_y
#print(guard_map)
while True:
guard_map[guard_y ][guard_x]['value'] = 'X'
try:
#map_string = ""
#for line in guard_map:
# for column in guard_map[line]:
# if column == guard_x and line == guard_y:
# map_string += guard_orientation
# else:
# map_string += guard_map[line][column]['value']
# map_string += "\n"
#print(map_string)
match guard_orientation:
case '^':
if guard_map[guard_y - 1][guard_x]['value'] == '#':
guard_orientation = '>'
else:
guard_y -= 1
case '>':
if guard_map[guard_y][guard_x + 1]['value'] == '#':
guard_orientation = 'v'
else:
guard_x += 1
case 'v':
if guard_map[guard_y + 1][guard_x]['value'] == '#':
guard_orientation = '<'
else:
guard_y += 1
case '<':
if guard_map[guard_y][guard_x - 1]['value'] == '#':
guard_orientation = '^'
else:
guard_x -= 1
except KeyError:
break
for line in guard_map:
for column in guard_map[line]:
if guard_map[line][column]['value'] == 'X':
total_visits += 1
total_obstacles = 0
obstacle_coords = []
for line in guard_map:
for column in guard_map[line]:
guard_x = guard_initial_x
guard_y = guard_initial_y
obstacle_encountered = 0
encounter_x = None
encounter_y = None
guard_orientation = '^'
init_time = time.time()
if guard_map[line][column]['value'] == 'X':
guard_map_copy = copy.deepcopy(guard_map)
guard_map_copy[line][column]['value'] = '#'
while True:
#map_string = ""
#for line_map in guard_map_copy:
# for column_map in guard_map_copy[line_map]:
# if column_map == guard_x and line_map == guard_y:
# map_string += guard_orientation
# else:
# map_string += guard_map[line_map][column_map]['value']
# map_string += "\n"
#print(map_string)
guard_prev_orientation = guard_orientation
try:
match guard_orientation:
case '^':
if guard_map_copy[guard_y - 1][guard_x]['value'] == '#':
guard_orientation = '>'
else:
guard_y -= 1
case '>':
if guard_map_copy[guard_y][guard_x + 1]['value'] == '#':
guard_orientation = 'v'
else:
guard_x += 1
case 'v':
if guard_map_copy[guard_y + 1][guard_x]['value'] == '#':
guard_orientation = '<'
else:
guard_y += 1
case '<':
if guard_map_copy[guard_y][guard_x - 1]['value'] == '#':
guard_orientation = '^'
else:
guard_x -= 1
#if (abs(column - guard_x) == 1 and line - guard_y == 0) or (column - guard_x == 0 and abs(line - guard_y) == 1):
# obstacle_encountered += 1
# if obstacle_encountered == 1:
# encounter_orientation = guard_prev_orientation
# encounter_x = guard_x
# encounter_y = guard_y
#
#if (obstacle_encountered >= 2 and guard_x == encounter_x and guard_y == encounter_y and guard_prev_orientation == encounter_orientation):
# total_obstacles += 1
# print(line, column)
# obstacle_coords.append([line, column])
# print(total_obstacles)
# break
if time.time() - init_time > 0.1:
total_obstacles += 1
print(total_obstacles)
break
except KeyError:
break
print(total_visits)
print(total_obstacles)
print(obstacle_coords)

130
2024/Day 6/puzzle.txt Normal file
View File

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