Day 06
This commit is contained in:
145
2024/06/06.py
Normal file
145
2024/06/06.py
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
import copy
|
||||||
|
import time
|
||||||
|
|
||||||
|
def makeMap(arr):
|
||||||
|
for i in range(0, len(arr)):
|
||||||
|
if "^" in arr[i]:
|
||||||
|
loc = [i, arr[i].index("^")]
|
||||||
|
break
|
||||||
|
flag = True
|
||||||
|
while flag:
|
||||||
|
match arr[loc[0]][loc[1]]:
|
||||||
|
case "^":
|
||||||
|
if loc[0] == 0:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0] - 1][loc[1]] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = ">"
|
||||||
|
else:
|
||||||
|
arr[loc[0] - 1][loc[1]] = "^"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[0] -= 1
|
||||||
|
case ">":
|
||||||
|
if loc[1] == len(arr[loc[0]]) - 1:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0]][loc[1] + 1] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = "V"
|
||||||
|
else:
|
||||||
|
arr[loc[0]][loc[1] + 1] = ">"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[1] += 1
|
||||||
|
case "V":
|
||||||
|
if loc[0] == len(arr) - 1:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0] + 1][loc[1]] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = "<"
|
||||||
|
else:
|
||||||
|
arr[loc[0] + 1][loc[1]] = "V"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[0] += 1
|
||||||
|
case "<":
|
||||||
|
if loc[1] == 0:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0]][loc[1] - 1] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = "^"
|
||||||
|
else:
|
||||||
|
arr[loc[0]][loc[1] - 1] = "<"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[1] -= 1
|
||||||
|
case _:
|
||||||
|
print("THIS IS AN ERROR")
|
||||||
|
return arr
|
||||||
|
|
||||||
|
def check(arr):
|
||||||
|
loc = []
|
||||||
|
pastlocs = []
|
||||||
|
for i in range(0, len(arr)):
|
||||||
|
if "^" in arr[i]:
|
||||||
|
loc = [i, arr[i].index("^")]
|
||||||
|
break
|
||||||
|
flag = True
|
||||||
|
while flag:
|
||||||
|
if [loc,arr[loc[0]][loc[1]]] in pastlocs:
|
||||||
|
return -1
|
||||||
|
match arr[loc[0]][loc[1]]:
|
||||||
|
case "^":
|
||||||
|
if loc[0] == 0:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0] - 1][loc[1]] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = ">"
|
||||||
|
else:
|
||||||
|
pastlocs.append([copy.deepcopy(loc), arr[loc[0]][loc[1]]])
|
||||||
|
arr[loc[0] - 1][loc[1]] = "^"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[0] -= 1
|
||||||
|
case ">":
|
||||||
|
if loc[1] == len(arr[loc[0]]) - 1:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0]][loc[1] + 1] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = "V"
|
||||||
|
else:
|
||||||
|
pastlocs.append([copy.deepcopy(loc), arr[loc[0]][loc[1]]])
|
||||||
|
arr[loc[0]][loc[1] + 1] = ">"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[1] += 1
|
||||||
|
case "V":
|
||||||
|
if loc[0] == len(arr) - 1:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0] + 1][loc[1]] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = "<"
|
||||||
|
else:
|
||||||
|
pastlocs.append([copy.deepcopy(loc), arr[loc[0]][loc[1]]])
|
||||||
|
arr[loc[0] + 1][loc[1]] = "V"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[0] += 1
|
||||||
|
case "<":
|
||||||
|
if loc[1] == 0:
|
||||||
|
flag = False
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
elif arr[loc[0]][loc[1] - 1] == "#":
|
||||||
|
arr[loc[0]][loc[1]] = "^"
|
||||||
|
else:
|
||||||
|
pastlocs.append([copy.deepcopy(loc), arr[loc[0]][loc[1]]])
|
||||||
|
arr[loc[0]][loc[1] - 1] = "<"
|
||||||
|
arr[loc[0]][loc[1]] = "X"
|
||||||
|
loc[1] -= 1
|
||||||
|
case _:
|
||||||
|
print("THIS IS AN ERROR")
|
||||||
|
ctr = 0
|
||||||
|
for l in arr:
|
||||||
|
for a in l:
|
||||||
|
ctr += 1 if a == "X" else 0
|
||||||
|
return ctr
|
||||||
|
|
||||||
|
def obstruct(arr):
|
||||||
|
ctr = 0
|
||||||
|
start = time.time()
|
||||||
|
map = makeMap(copy.deepcopy(arr))
|
||||||
|
for i in range(0,len(arr)):
|
||||||
|
spent = time.time() - start
|
||||||
|
print("After "+str(int(spent))+" seconds, checking "+str(i)+" out of "+str(len(arr)))
|
||||||
|
for j in range(0,len(arr[i])):
|
||||||
|
if map[i][j] == "X" and arr[i][j] != "^":
|
||||||
|
tmpArr = copy.deepcopy(arr)
|
||||||
|
tmpArr[i][j] = "#"
|
||||||
|
if check(tmpArr) == -1:
|
||||||
|
ctr += 1
|
||||||
|
return ctr
|
||||||
|
|
||||||
|
arr = []
|
||||||
|
with open("input06.txt") as file:
|
||||||
|
for line in file:
|
||||||
|
arr.append([])
|
||||||
|
for a in line:
|
||||||
|
if a != "\n":
|
||||||
|
arr[-1].append(a)
|
||||||
|
out = check(copy.deepcopy(arr))
|
||||||
|
print("Without new obstructions, the amount of positions the guard passes before exiting is: "+str(out))
|
||||||
|
out = obstruct(copy.deepcopy(arr))
|
||||||
|
print("Amount of positions a new obstruction can make the guard loop is: "+str(out))
|
||||||
130
2024/06/input06.txt
Normal file
130
2024/06/input06.txt
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
.............#........................................................#...#...........................................#...........
|
||||||
|
.....#...............................#.........................................#...#...............#........##...............#.#..
|
||||||
|
..................................#..................#.......................................#...#................................
|
||||||
|
.........................................#................#....#..............................................#................#.#
|
||||||
|
........#.........................................................#...........#.......#..............................#............
|
||||||
|
........................#.............................#.......................#.......#.......................#........#.........#
|
||||||
|
................................#.......#............................................#...........................................#
|
||||||
|
...........#........#...#..#...........................#..................#.............#.......#..........................#......
|
||||||
|
............................................#........#.......#..#......#..............................#..#................##......
|
||||||
|
............#..#...#......#.........................................##.......#..........#.......#...................#.............
|
||||||
|
.....................................................................................#.....#......................................
|
||||||
|
.......................................................#................#.....................................#...................
|
||||||
|
...........#.....#.#....................................#.......#.#...........#................#.......#.................#........
|
||||||
|
............#.............#....................#....#............................#.........................................#......
|
||||||
|
.#..........#......#................#........................#............#..#..............#.....................................
|
||||||
|
..................#....#...........................................#.....#.....#.........................................#.#......
|
||||||
|
.............#..............#..#...........................#..#..........#...............................#........................
|
||||||
|
.................................................................................................#................................
|
||||||
|
.......................................................#..............#..............................................#..#.........
|
||||||
|
......#............................#.......................#................................................................#.....
|
||||||
|
..........#.........#..................#....#....#...#......#.........#.................................#........................#
|
||||||
|
..#...#............................#............#.......###.................................#.............................#...#...
|
||||||
|
.................#....#...................#........................................................................#..........#...
|
||||||
|
....#..........................#......................................................................#.....#......#..............
|
||||||
|
.#........................................................................#..........#............#..................#............
|
||||||
|
...................##.#.......................#....#.#........................................#.........##........................
|
||||||
|
#...........................................................#..........#........#.#..................................#............
|
||||||
|
.......#...#.........................................#.......#........#...............##..#.......................................
|
||||||
|
......................#....#...............................#......................................................#..#............
|
||||||
|
......#.....................................................#...............................#......................#..#...........
|
||||||
|
.........................#.#......................................................................................................
|
||||||
|
.......................................#...............#...................#................#..........#.#......................#.
|
||||||
|
.......#......#...#...........................#.........................#...............#..#...............#.............##.......
|
||||||
|
.....#..#..#..................#............#......................................................#...#.........................#.
|
||||||
|
#.#...............#...........#................#...................................................#..........#...................
|
||||||
|
..........................................................................#...............................................#.......
|
||||||
|
.........#.......#..........................#.##.............................#..##...#.......................................#....
|
||||||
|
....#...............#..........#........................................#.....#.........................................#.........
|
||||||
|
...........#................................................#....##...................................................#..#........
|
||||||
|
........................#........#...........#...#............................................#......................##...........
|
||||||
|
......#........................................#...#.............................................#................................
|
||||||
|
#.....#......#..................................#.............#..#...............................#................................
|
||||||
|
..................#......................#..........#...................#............................................#....#.......
|
||||||
|
.................#.............................................................................#.......#..........................
|
||||||
|
......#........................................................................................#...#....................#.........
|
||||||
|
....#......................#..................##..................#.......#.......................#...........#.........#..#......
|
||||||
|
............................................................#......##.............................#.#.....#............#..........
|
||||||
|
..............#.......................#...^.....................................#.....................................#...........
|
||||||
|
..............#...##......#..#.....#..........................................................................#.......#.........#.
|
||||||
|
.#................................................................#........##......#....#.......#.................................
|
||||||
|
....#.....................#........#...........#.............................................#........#................#......#..#
|
||||||
|
...................#.............#.........#.........................#...........#..................#...................#....#....
|
||||||
|
.#...............................................#.....#..#...#...................................................................
|
||||||
|
........................##............#.....................................#........#..................................#.........
|
||||||
|
..............#................................#.....................................#...................................#........
|
||||||
|
...........#..........#...........#........#...................#..#..........................#....................................
|
||||||
|
.#..............................................................#....#.......#.....................................#..............
|
||||||
|
.....................#........#......#................#.............#........................#.......................#........#...
|
||||||
|
.........................#..............#...#....#..................#.............................................................
|
||||||
|
..#............................##.........................................................#...........................#.#..#......
|
||||||
|
.....#............#...##........#..........#....#......#................................#.................#...............#....#..
|
||||||
|
.....#...............#.............................#............#......#....#........................................#............
|
||||||
|
...#..........#.........#.....................##.......#.....#...................#................................................
|
||||||
|
.................................#...........................................#.................#...........................#......
|
||||||
|
..........................................................#........................#.......#.................................#...#
|
||||||
|
..#......#......................................#...#..#....................#...........#....#.............................#......
|
||||||
|
..#........#.............#......................#...#.....#....#........................#..#......................................
|
||||||
|
.........#...........#.......................................................................#.........#....#...#.................
|
||||||
|
..................................#....#.............##....#.......#.........#.........................................#..........
|
||||||
|
....#.................................................#.......#...........#.......................................##.....#........
|
||||||
|
..............................#.........................................................#.....#........................#..........
|
||||||
|
...#........#...........#..#..................................................#..............#.............................#......
|
||||||
|
................................................#...............................................................#.....#..#.....#..
|
||||||
|
...............#.........................#..#...................#....................................#................#...........
|
||||||
|
............................#...........................................................................................#.........
|
||||||
|
.....#.........................#.......#..#..##.............................#................##.......##............#.............
|
||||||
|
.........#........................................................................................................................
|
||||||
|
.........................#............................#........................#.....#..........#.....................#...#..#....
|
||||||
|
.......#........#..............................#..........................#...#.................#............#....................
|
||||||
|
#...#..#...............................#..........................................#...#...#.........................#.............
|
||||||
|
...........................................................#..............#..........#..................#.........................
|
||||||
|
...........................................................#..............................................................#....#..
|
||||||
|
...........#.......................#................#......#.....................................#..................#.....#.......
|
||||||
|
.....#.................................#........................#.#...................#.....#............#........................
|
||||||
|
...........................#.#............#..............#..............#..#...#..........................#................#......
|
||||||
|
.........#...................#........................#......#..#..................................................#..............
|
||||||
|
.............................................#.................................................................#.......#.......#..
|
||||||
|
#.......................#...............#.......................#...........#................................#....................
|
||||||
|
......................................#............#....................................................##...................#....
|
||||||
|
#...............#...........................................#......#....................................#...............#.........
|
||||||
|
...#...............#.................................#..................................#..#............#.........................
|
||||||
|
......................#..#............#..#..............................................#......#....#...............#.............
|
||||||
|
..................#.....................#.....#...................#..#................#....#..........#......#..........#.........
|
||||||
|
.......................#.....#..............#..........................#............#...................#.#.......................
|
||||||
|
......#...........#..............#........#........................................#......................................#.......
|
||||||
|
............#................#..##....................................................................#........................#..
|
||||||
|
...........#....................................#.............................#.....#....#...#.#......................#...........
|
||||||
|
...................................................#...............................#....................#.........#...............
|
||||||
|
........#......#..#................#..................................................................#.................#.........
|
||||||
|
.............................#................................................#...........#.....#....#............................
|
||||||
|
........#.............#.........#............#....#.....................#........#...............#................................
|
||||||
|
..#................................#................................#..#......#.............................................#.....
|
||||||
|
.........#...........................................#..................................#....#..#.....................#...........
|
||||||
|
.....................................................#.........................................................#..................
|
||||||
|
.........#...#.#.....#.......##................................#...#..........................#........#..........................
|
||||||
|
......................##...............................#..........................................................................
|
||||||
|
.........#...#.....#........................................#................#........#............#........#.....................
|
||||||
|
...............#............#.................................................................#................................#..
|
||||||
|
......#..............#...................................................#............#.....................#.....................
|
||||||
|
.........#.........#..............................................................................................................
|
||||||
|
.#........#...............#..##......#..............................#...........#..#.........#...........................#........
|
||||||
|
...................#................................................................................#...............#.............
|
||||||
|
..............................................................................#........................#................#.........
|
||||||
|
...........#.....#..#.#....................................................#......#......#..#................##...................
|
||||||
|
....#......#...................................................................#..............#.....#.........#..................#
|
||||||
|
..#...........#...#...........#.......##..........##......................#......................................................#
|
||||||
|
..................#................................................#.#............................................................
|
||||||
|
#................................................................#............#...##..........................................#...
|
||||||
|
......#............................................#.......#.#.................................................#..................
|
||||||
|
..............................#..............#.#....#............#..........................#.....................................
|
||||||
|
............................#..................................#.................#..#...........................................#.
|
||||||
|
.........................................#................#..................#...#............................#..#......#........#
|
||||||
|
....................#..............................................................#..............................#..........##...
|
||||||
|
...#......................................#...........#..#............................................#.....#....#................
|
||||||
|
................#..#....#.........................................................................................................
|
||||||
|
...................................#...................#...............................#................................#.......#.
|
||||||
|
..........#.................#.............#..................#...............................................................#....
|
||||||
|
.....#...........#........#................#.........................................#..........................#......##.........
|
||||||
|
..........................#................#..............................................................#.......................
|
||||||
|
........#.................................................................#......................#.#..........................#...
|
||||||
Reference in New Issue
Block a user