From 8a3b68304b838e42d6ce463e132a441f7507dc58 Mon Sep 17 00:00:00 2001 From: Luna Lailatova Date: Sun, 22 Jan 2023 01:36:28 +0100 Subject: [PATCH] day24 dont look at part 2 --- day24-1.py | 132 ++++++++++++++++++++++++++++++++++++++ day24-2.py | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++ in2.txt | 13 ++-- input24.txt | 27 ++++++++ 4 files changed, 346 insertions(+), 7 deletions(-) create mode 100644 day24-1.py create mode 100644 day24-2.py create mode 100644 input24.txt diff --git a/day24-1.py b/day24-1.py new file mode 100644 index 0000000..f448fd0 --- /dev/null +++ b/day24-1.py @@ -0,0 +1,132 @@ +from queue import PriorityQueue + +qu = PriorityQueue() +qu2 = PriorityQueue() +currentbest = None +timestamps = {} +solutions = 0 +visted = [] + +class Vortex(): + def __init__(self, y, x): + self.y = y + self.x = x + + def givePosition(self, time): + return self.calcPosition(time) + + +class VortexRigth(Vortex): + def calcPosition(self, time): + current = (self.x + time) % width + return self.y, current + def __str__(self): + return 'Rigth' + +class VortexLeft(Vortex): + def calcPosition(self, time): + current = self.x - time + while current < 0: + current = width + current + return self.y, current + def __str__(self): + return 'Left' + +class VortexUp(Vortex): + def calcPosition(self, time): + current = self.y - time + while current < 0: + current = heigth + current + return current, self.x + def __str__(self): + return 'Up' + +class VortexDown(Vortex): + def calcPosition(self, time): + current = (self.y + time) % heigth + return current, self.x + def __str__(self): + return 'Down' + +def findPath(y, x, time): + #print(y, x, time) + global currentbest + global timestamps + global solutions + if y == heigth - 1 and x == width - 1: + solutions += 1 + if currentbest is None or currentbest > time: + currentbest = time + #print('solution found', currentbest) + return + if currentbest is not None and time + ((width - 1 - x) + (heigth - 1 - y))>= currentbest - 1: + return + if time + 1 not in timestamps: + timeList = [] + for vort in vorteces: + timeList.append(vort.givePosition(time + 1)) + timestamps[time + 1] = timeList + for nextMove in [(y, x),(y - 1, x),(y + 1, x),(y, x- 1),(y, x + 1)]: + #print(timestamps[time + 1], nextMove) + if nextMove not in timestamps[time + 1] and 0 <= nextMove[0] < heigth and 0 <= nextMove[1] < width: + if nextMove + (time + 1,) not in visted: + visted.append(nextMove + (time + 1,)) + if switch: + qu2.put((time + (heigth - nextMove[0]) + (width - nextMove[1]), (time + 1), -nextMove[0], -nextMove[1])) + else: + qu.put((-(time + 1), -((nextMove[0]+1)*(nextMove[1]+1)), -nextMove[0], -nextMove[1])) + +#reading input +initialState = [] +vorteces = [] +with open('input24.txt', 'r') as f: + inp = f.read().splitlines(keepends=False) + +for line in inp: + initialState.append(list(line)) + +heigth = len(initialState) - 2 +width = len(initialState[0]) - 2 +currentbest = heigth * width + +for y in range(len(initialState)): + for x in range(len(initialState[0])): + if initialState[y][x] == '>': + newVortex = VortexRigth(y - 1, x - 1) + vorteces.append(newVortex) + if initialState[y][x] == '<': + newVortex = VortexLeft(y - 1, x - 1) + vorteces.append(newVortex) + if initialState[y][x] == '^': + newVortex = VortexUp(y - 1, x - 1) + vorteces.append(newVortex) + if initialState[y][x] == 'v': + newVortex = VortexDown(y - 1, x - 1) + vorteces.append(newVortex) +switch = False +n = 0 +qu.put((-1, 0, 0, 0)) +while not qu.empty(): + if not switch and solutions > 0: + switch = True + time, _, y, x = qu.get() + n += 1 + if n % 10000 == 0: + print(n, solutions, time, currentbest) + findPath(abs(y), abs(x), abs(time)) + +while not qu2.empty(): + _, time, y, x = qu2.get() + n += 1 + if n % 10000 == 0: + print('n=', n, 'solutions=', solutions, 'time=', time, 'currentbest=', currentbest+1, 'queue=', qu2.qsize()) + findPath(abs(y), abs(x), abs(time)) + +print(currentbest + 1) +# for vort in vorteces: +# print(vort, '(', vort.y, vort.x, ')', vort.givePosition(3)) + +# for line in initialState: +# for point in line: +# print(point, end = ' ') +# print('\n') \ No newline at end of file diff --git a/day24-2.py b/day24-2.py new file mode 100644 index 0000000..37c0f2f --- /dev/null +++ b/day24-2.py @@ -0,0 +1,181 @@ +from queue import PriorityQueue + +qu = PriorityQueue() +qu2 = PriorityQueue() +currentbest = None +timestamps = {} +solutions = 0 +visted = [] +total = 0 + +class Vortex(): + def __init__(self, y, x): + self.y = y + self.x = x + + def givePosition(self, time): + return self.calcPosition(time) + + +class VortexRigth(Vortex): + def calcPosition(self, time): + current = (self.x + time) % width + return self.y, current + def __str__(self): + return 'Rigth' + +class VortexLeft(Vortex): + def calcPosition(self, time): + current = self.x - time + while current < 0: + current = width + current + return self.y, current + def __str__(self): + return 'Left' + +class VortexUp(Vortex): + def calcPosition(self, time): + current = self.y - time + while current < 0: + current = heigth + current + return current, self.x + def __str__(self): + return 'Up' + +class VortexDown(Vortex): + def calcPosition(self, time): + current = (self.y + time) % heigth + return current, self.x + def __str__(self): + return 'Down' + +def findPath(y, x, time): + #print(y, x, time) + global currentbest + global timestamps + global solutions + if y == goal[0] and x == goal[1]: + solutions += 1 + if currentbest is None or currentbest > time: + currentbest = time + #print('solution found', currentbest) + return + if currentbest is not None and time + (abs(goal[1] - x) + abs(goal[0] - y))>= currentbest - 1: + return + if time + 1 not in timestamps: + timeList = [] + for vort in vorteces: + timeList.append(vort.givePosition(time + 1)) + timestamps[time + 1] = timeList + for nextMove in [(y, x),(y - 1, x),(y + 1, x),(y, x- 1),(y, x + 1)]: + #print(timestamps[time + 1], nextMove) + if nextMove not in timestamps[time + 1] and ((0 <= nextMove[0] < heigth and 0 <= nextMove[1] < width) or (nextMove[0] == heigth and nextMove[1] == width - 1) or (nextMove[0] == -1 and nextMove[1] == 0)): + if nextMove + (time + 1,) not in visted: + visted.append(nextMove + (time + 1,)) + if switch: + qu2.put((time + abs(goal[0] - nextMove[0]) + abs(goal[1] - nextMove[1]), (time + 1), -nextMove[0], -nextMove[1])) + else: + qu.put((-(time + 1), -((nextMove[0]+1)*(nextMove[1]+1)), -nextMove[0], -nextMove[1])) + +#reading input +initialState = [] +vorteces = [] +with open('input24.txt', 'r') as f: + inp = f.read().splitlines(keepends=False) + +for line in inp: + initialState.append(list(line)) + +heigth = len(initialState) - 2 +width = len(initialState[0]) - 2 +currentbest = heigth * width + +for y in range(len(initialState)): + for x in range(len(initialState[0])): + if initialState[y][x] == '>': + newVortex = VortexRigth(y - 1, x - 1) + vorteces.append(newVortex) + if initialState[y][x] == '<': + newVortex = VortexLeft(y - 1, x - 1) + vorteces.append(newVortex) + if initialState[y][x] == '^': + newVortex = VortexUp(y - 1, x - 1) + vorteces.append(newVortex) + if initialState[y][x] == 'v': + newVortex = VortexDown(y - 1, x - 1) + vorteces.append(newVortex) +switch = False +n = 0 +qu.put((-1, 0, 0, 0)) +goal = (heigth - 1, width - 1) +while not qu.empty(): + if not switch and solutions > 0: + switch = True + time, _, y, x = qu.get() + n += 1 + if n % 10000 == 0: + print(n, solutions, time, currentbest) + findPath(abs(y), abs(x), abs(time)) + +while not qu2.empty(): + _, time, y, x = qu2.get() + n += 1 + if n % 10000 == 0: + print('n=', n, 'solutions=', solutions, 'time=', time, 'currentbest=', currentbest, 'queue=', qu2.qsize()) + findPath(abs(y), abs(x), abs(time)) + +print(currentbest + 1) +total = currentbest + 1 +currentbest = None +goal = (0, 0) +solutions = 0 +switch = True +n = 0 +qu.put((-(total), 0, heigth, width - 1)) +visted = [] +timestamps = {} + +while not qu.empty(): + if not switch and solutions > 0: + switch = True + time, _, y, x = qu.get() + n += 1 + if n % 10 == 0: + print(n, solutions, time, currentbest, y, x) + findPath(abs(y), abs(x), abs(time)) + +while not qu2.empty(): + _, time, y, x = qu2.get() + n += 1 + if n % 10000 == 0: + print('n=', n, 'solutions=', solutions, 'time=', time, 'currentbest=', currentbest, 'queue=', qu2.qsize()) + findPath(abs(y), abs(x), abs(time)) + +print(currentbest + 1) +total = currentbest + 1 +currentbest = None +goal = (heigth -1, width - 1) +solutions = 0 +switch = True +n = 0 +qu.put((-(total), 0, -1, 0)) +visted = [] +timestamps = {} + +while not qu.empty(): + if not switch and solutions > 0: + switch = True + time, _, y, x = qu.get() + n += 1 + if n % 10 == 0: + print(n, solutions, time, currentbest, y, x) + findPath(abs(y), abs(x), abs(time)) + +while not qu2.empty(): + _, time, y, x = qu2.get() + n += 1 + if n % 10000 == 0: + print('n=', n, 'solutions=', solutions, 'time=', time, 'currentbest=', currentbest, 'queue=', qu2.qsize()) + findPath(abs(y), abs(x), abs(time)) + +print(currentbest + 1) \ No newline at end of file diff --git a/in2.txt b/in2.txt index 7ac3ba9..6b9b892 100644 --- a/in2.txt +++ b/in2.txt @@ -1,7 +1,6 @@ -....#.. -..###.# -#...#.# -.#...## -#.###.. -##.#.## -.#..#.. \ No newline at end of file +#.###### +#>>.<^<# +#.<..<<# +#>v.><># +#<^v^^># +######.# \ No newline at end of file diff --git a/input24.txt b/input24.txt new file mode 100644 index 0000000..69c9eca --- /dev/null +++ b/input24.txt @@ -0,0 +1,27 @@ +#.######################################################################################################################## +#>v>^>^<<^>v^>vv<.><>v^>vvv><.>^^.<.^>^vv>v^v>.>>>.v^>>^v^v<^>^<^<><>^v<.^v^^v>>^<.v<^^v>^^<>>.^<.>^v^<^>>^v^>v>v<.v>v^v.>^><>v<>.^v><>..v<<^..>^<<^<<>vv^>v^><.>v^^^^>..<<><>>^>^^<.>>v...# +#<<.><<>..<<>>>^<.<^vv^^<^<>>v<^>.<>^>v<>^>v>v^>v><>>v^>vv>v^v>.>^v<^<^><^vv<^v>v^^v^.^<>v.^<>^.>v>v<# +#<^v^^><^^<^.<^v..>>><>v>^v>v><>><^^.>>.<>>^>v>^v.>.><<<<>^>^>v>v>^><^^v<^vv<># +#>><>>^^^<<>^^^<^>^v^v<^>>v.><<<<.>>^.v>^<<^<^^>^><^<>.<>vv^<.<^.^v><^<<^^># +#<>>^^>vvvvv.><>>^>v^^<<>^>vv^<^^^<^v<v^v<^>>v^vv^<>^<>^v<.vv<<.<<^^^v<^>vv^<.>v<.v^v^vv^.# +#.>v>.>^<>v>>.^vvv<^<^><<^^>.<>v<<<^.<>>>v>^>><>v<^>.v^.^v>v^<>><>><^.v><.>v<>^<^^>v>>v^^v^<v.v.^>^^^^<><># +#>v<^^><^>.>^^^>vvv>>vv<^>^>^v>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<<..v<<^>>^.^vv<>vv>v^><>^vv>^>>.>^.^.v<<<.^v<>v^<^<>><><>^v^^v>^v.v>^..<^v<>.>..^^.>^v># +#><.^v>.>^<.v<>.<<^>.<>>v^v>^>^^>>^>>>v><>v>>.>>vv.v<>.^^<>>>vv^>v..<>^<^>v<>>.>v.^v>.^v>.<.>^<<^>.# +#>^>>^vv>vv<^<^<<<>v>v>^^<^^<.>^<>vv<>^>v>>^^^^^<>^>>>v.v^>v<.^<>.v^^^.>vvvv^>>v.^.^^^.^vv^^v^.^^.v># +#<<^v.>>>v>^<^<.vv>vv^^^.^<^<<^vv.^^>.<>^.^..<>v>v^>v>><^>^^^vv<^>v>>>v>^^>^><^^>vv^>.<>^^v^^^>^v>># +#<>.v<>.>^v^<^.<<<>v^.^>v><.<>vv<^^v^>v><.^...v>>^<<^><>>><^^>>.><>>>^v^>vv<<# +#<.^^>^.^^>>>^<^^^^<<.>^<<<.>>>.>^^v.>vv^..^.>.>>.<^^^v>^^vv<<>^v.>>>>^><>>v^v<>^^<^v.v^v^.>vvv..>v.>^># +########################################################################################################################.# \ No newline at end of file