From 96f25dee8b23f8d73592fa5cced8a1129637cf11 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Mon, 16 Dec 2024 05:31:07 +0000 Subject: [PATCH] day 16 hello A* my old friend --- day16/astar.py | 112 +++++++++++++++++++++++++++++++++++++ day16/input | 141 +++++++++++++++++++++++++++++++++++++++++++++++ day16/sample1.in | 15 +++++ day16/sample3.in | 17 ++++++ day16/sol.py | 44 +++++++++++++++ 5 files changed, 329 insertions(+) create mode 100644 day16/astar.py create mode 100644 day16/input create mode 100644 day16/sample1.in create mode 100644 day16/sample3.in create mode 100644 day16/sol.py diff --git a/day16/astar.py b/day16/astar.py new file mode 100644 index 0000000..18188bd --- /dev/null +++ b/day16/astar.py @@ -0,0 +1,112 @@ +from heapq import heappush, heappop + +def search(start, is_goal, neighbors, heuristic=None, worst=None): + if heuristic == None: + def heuristic(x): + return 0 + if not callable(is_goal): + goal = is_goal + def is_goal(this): + return this == goal + if not isinstance(start, list): + start = [start] + i = 0 # tiebreaker + q = [] + for s in start: + i += 1 + heappush(q, (heuristic(s), i, s, None)) + done = {} + if worst: + best_worst = min(worst(s) for s in start) + while q: + z, _, this, prev = heappop(q) + if this in done: + z -= heuristic(this) + if z == done[this][0]: + done[this] = (z, done[this][1]+[prev]) + continue + cost_so_far = z - heuristic(this) + #print(this,z, cost_so_far) + + done[this] = (cost_so_far, [prev]) + + if is_goal(this): + print("astar: visited", len(done), "nodes") + print("astar: pending", len(q), "nodes") + # find all nodes on a best path + Q = [this] + path = set() + while Q: + n = Q.pop() + if n not in path and n != None: + path.add(n) + Q.extend(done[n][1]) + return cost_so_far, this, path + + for c, n in neighbors(this): + c = cost_so_far + c + if n not in done or c < done[n][0]: + h = heuristic(n) + if worst: + if c+h > best_worst: + # if the best possible cost for this node + # is worse than the lowest worst-case cost we've seen + # then don't even bother exploring it + continue + w = worst(n) + if c+w < best_worst: + best_worst = c+w + i += 1 + heappush(q, (c+h, i, n, this)) + elif n in done: + if c == done[n][0]: + done[n] = (c, done[n][1]+[n]) + + return float('inf'), None, [] + +def test(): + data = [ + "aabqponm", + "abcryxxl", + "accszzxk", + "acctuvwj", + "abdefghi", + ] + #data = [ + # "aabbaaba", + # "abcbaaba", + # "accabcbb", + # "accbbbba", + # "abbbabab", + #] + start = (0,0) + goal = (5,2) + + def get(x,y): + return ord(data[y][x]) + + def neighbors(src): + x,y = src + here = get(x,y) + n = [] + def push(x,y): + if 0 <= y < len(data): + if 0 <= x < len(data[y]): + if get(x,y) <= here+1: + n.append((1, (x,y))) + push(x-1, y) + push(x+1,y) + push(x, y-1) + push(x, y+1) + return n + + def heuristic(n): + return abs(goal[0] - n[0]) + abs(goal[1] - n[1]) + + c, _, path = search(start, goal, neighbors, heuristic) + print(*data, sep="\n") + print(*path, sep="\n") + assert c == 31, c + +if __name__ == '__main__': + test() diff --git a/day16/input b/day16/input new file mode 100644 index 0000000..96fe298 --- /dev/null +++ b/day16/input @@ -0,0 +1,141 @@ +############################################################################################################################################# +#.................#.....#.....#.............#...#.......#.....#.......#.#.........#...........#...#...............#........................E# +###########.#######.#.#.#.###.#.###########.#.#.#.###.###.#.#.#.#.#.#.#.#.#####.#.###.#######.#.#.###.#########.#.###.###########.#.#####.#.# +#.........#.#.......#...#.#...#...#...#...#.#.#.....#.#...#.#.#...#.#...#.#.#...#...#.#...#.....#...#.......#.........#.....#...#.#...#.#...# +#.#######.#.#.#########.#.#.#####.#.#.###.#.#.#######.#.###.###.#.#.###.#.#.#.#####.#.#.#.#########.#.#####.#.#.#########.#.###.#.###.#.###.# +#.......#.#.#.......#.#.#.#.#...#...#.........................#.#...#.#.#...#.#...#...#.#.....#...#.#.....#.#...#.........#.....#.#...#.#...# +#.#####.#.#.#######.#.#.#.#.#.#.#######.#.#######.#.#.#######.#.#.###.#.###.#.#.#.#####.#.###.#.###.#####.#.#.#.#.#####.#####.###.#.#.#.#.#.# +#.#.....#.........#...#...#...#.#.......................#.............................#.#.#...#.....#.....#.......................#.....#.#.# +#.#.#######.###.#####.#.#######.#.#############.###.#####.#.#.#.#.#.#.###.###.#.#####.#.#.#.###.#########.#.###.#########.###.#.#.#####.#.### +#.#.#.......#.........#...#.....#...#...........#.#.#...#.#.....................#...#.....#.#.#.....#...#.#.....#.......#...#...#.#.#...#...# +#.#.#########.#.#######.#.#.#######.#.#######.#.#.#.#.#.#.#####.#.#######.#.###.#.#######.#.#.#####.#.#.###.###.#.#####.###.#.###.#.#.#####.# +#.#.......#...#...........#.....#...#.#...#...#...#...#.#.......#.........#.....#.#.....#...#.....#...#.........#.#...#.#...#.....#.....#...# +#.#######.#.###.###.#.#.#.#####.#####.#.#.#.#.#########.###.#####.#############.#.#.###.#.###.#####.###########.#.#.#.#.#####.#.###.#####.#.# +#...#.....#.#.............#...#...........#.#.........#...#.#.......#...#.....#...#.#.#...#.#.......#.......#...#...#.#.......#.#...#.....#.# +#####.#####.#.#.#.###.#.#.###.#############.#########.###.#.#.###.#.#.#.#.###.#####.#.###.#.#.###.###.#####.#.#.#####.#######.#.#####.#####.# +#...#.....#.#.#.#.#...#.#...#...#...#...#...#.....#...#...#.#.#.#...#.#.#...#...#...#.#.........#.....#...#.....#.....#.....#.#.......#...#.# +#.#.#####.#.###.#.#.###.###.#.###.#.#.#.#.###.###.#.###.###.#.#.#.###.#####.###.#.###.#.#########.#####.#.#.###.#.#########.#.#####.###.#.#.# +#.#.......#.....#...#...#.....#...#...#.....#...#.#...#.#...#.#...#...#.....#.#...#...#.#.#.....#...#...#.#...#.#.#.......#.#...#...#...#.#.# +#.#######.#######.###.###.#####.#####.#####.#.#.#####.#.#.###.#.###.###.#####.#####.#.#.#.#.###.#.#.#.###.#.#.#.#.#.#####.#.###.#####.###.### +#...#...#.#.........#.....#.#...#.....#...#...#.....#...#.....#.......#.#.......#...#.#...#...#.#.#...#...#.#.#.#...#.#.......#.#...#.#.#...# +#.#.#.#.#.#.###.###.#.#####.#.#####.###.#.#########.###########.#####.#.#.#####.#.###.#.###.#.#.#.#####.###.#.#######.#.#######.#.#.#.#.###.# +#.#.#.#...#...#.#.#.#...#...#.#...#...#.#.#.......#...........#...#...#.#.....#...#...#.#...#...#.......#...#.......#...#.....#...#.#.....#.# +#.#.#.#######.#.#.#.###.#.###.#.#.#####.#.#.#####.#########.#####.#.#.#.###.#.#.#######.#.###.#.#######.#.#.###.#####.###.###.#####.#####.#.# +#.#.#.....#...#.#...#.#.#...#...#...#...#...#...#.....#...#.....#...#.#...#.#.#.#.......#.....#.#...#...#.#...#.#.....#.#...#.........#...#.# +#.#.#####.###.#.#.###.#.###.#######.#.#####.###.#####.#.#.#####.###.#####.#.#.###.#.#####.###.#.#.#.###.#.###.#.#.#####.###.#####.#####.###.# +#.#...#.#...#.#.#.....#...#.....#...#.....#.....#.......#.....#.....#.....#.#.....#.......#...#...#...#.#.....#...#...#...#.....#.#.....#.#.# +#####.#.###.###.#.#.#.###.###.#.#.#.#####.###.#.###.#########.#.###.#.#####.#.#####.#######.#.#######.#######.#####.#.#.#.#####.#.#.#####.#.# +#.....#...#.#...#...#...#.....#.#.#...#.#...#.#...#.......#...#.....#...#...#.#...#.#.....#.#...............#.#.#...#...#.#...............#.# +#.#######.#.#.#########.#######.#.###.#.###.#.###.###.###.#.#######.###.#.###.#.#.#.#.#.#.#.#.#.###.#####.#.#.#.#.#.#####.#.#.###.###.#.###.# +#.....#...#.........#.....#...#.#.#.......#...#.#.....#.#.#.......#.#...#.#.#...#.#.#.#.#...#.#...#.....#.....#.#.#.#.......#.#.....#.#.#...# +#.#.#.#.###.#.#.#####.#####.#.#.#.#######.#####.#######.#.###.#.###.#.###.#.#.###.#.###.#.###.###.#.#.#########.#.#.#######.#.#######.#.#.#.# +#...#.#...#.#.#.#.....#.....#...#...#.........#...#.....#...#.#.....#.#.#.#...#...#...#.#.#...#.#...#.......#...#.#.....#.#.#.#.......#.#.#.# +###.#.###.#.#.#.#.#####.###########.###.#####.#.#.#.###.###.#.#.#####.#.#.#.###.#.###.#.###.###.###.###.#.#.###.#.#####.#.#.#.#.#######.#.### +#...#...#...#.#.#.#.#...#...#...#...#...#.......#.#...#...#.#.#.....#.#.........#.#.#.#.#.........#.....#.......#.#.#...#.#.#.#.#.......#...# +#.#.###.#.###.#.#.#.#.###.#.#.#.#.###.#.#.#######.#####.###.#.#######.###########.#.#.#.#.#######.#.###.#.#.#####.#.#.###.#.#.#.#.###.#####.# +#.#.#...#...........#.#...#.#.#...#...#.#.#.#...#.......#...#...#.....#...#...#.....#.#...#...#...#...#.#.#.#.......#.#.....#.#...#...#.....# +#.#.#.#####.#.#####.#.#.###.#.#####.#####.#.#.#.#########.#####.#.#####.#.#.#.#######.###.#.#.#.#####.###.#.#.#######.#######.#####.###.##### +#...#.#...#.#...#.#.#.#...#.#.#...#.......#.#.#...........#...#.#.......#...#.#.........#.#.#.#.#.....#...#.#.#.#.....#.......#.....#...#...# +###.#.#.#.#.###.#.#.#.###.#.#.###.#.#######.#.#############.###.#.###########.#.###.###.#.#.#.#.#.###.#.#####.#.#.###.#.#######.#####.###.#.# +#...#...#.#.....#...#.....#.#...#.#...#.....#.........#.....#...#.#...#.....#.#...#...#.#.#.#.#...#...#...#.....#.#.#.#...#.....#...#.....#.# +#.#.###.#.###.#####.#.#########.#.###.#.#############.#.###.#.#.#.#.#.#####.#.###.#.#.#.###.#.#.#########.#.#####.#.#.###.#.#.###.#.#######.# +#.#.......#...#.....#.#.....#...#...............#...#.#.#...#.#.#.#.#.....#.#.#...#.#.#.....#.#.#.......#...#.....#...#...#.#.....#.........# +###.###.###.###.###.#.#.###.#.###.#######.###.###.#.#.#.#####.#.#.#.#####.#.#.#.###.###.#.###.###.#####.#####.#####.###.###.#####.#####.#.### +#...#.....#.#.....#.#.#.#...#.#...#...#...#...#...#...#.#.....#...#...#...#.#...#.#.....#.........#...#.......#.......#.....#...#.....#.#...# +#.#.###.###.#.#####.#.#.#.###.#.###.###.###.###.#######.#.#####.#####.#.###.#####.#######.#.#######.#.###.#######.#####.#####.#.#####.#####.# +#.#...#.....#.#.....#.#.#.....#.#...#...#.#...#...#.....#...#.#.......#.#.........#.#.....#.......#.#.#...#.....#.#...#.#.....#.....#.....#.# +#.#.#.#.###.#.#.#####.#.###.#.#.#.###.###.###.#.#.###.#.###.#.#########.###.#####.#.#.###########.#.###.###.###.###.#.#.#####.#####.#.###.#.# +#...#.....#.#.#.......#.#...#.#...#...#...#.#...#.#...#.#...#.........#...#.....#.#.#.#...#.....#.#.....#...#...#...#.#.....#.#.....#.#...#.# +#.#.###.#.###.#########.#.#.#.###.#.###.#.#.#.###.#.#.###.###.#######.###.#####.#.#.#.#.#.###.#.#.#.#####.###.###.###.#####.#.###.#.#.#.###.# +#...#...............#...#.#...#...#...#.#...#...#...#.#...#.....#...#...#.......#...#...#...#.....#...#...#.#.#.....#...#.#.#...#.#...#.#...# +###.###.#########.###.###.#.###.#####.#.###.###.#####.#.###.###.#.#.#.###.#####.###########.#.#########.###.#.#####.###.#.#.#.#.#.#.###.#.### +#.#.....#.....#.....#.#.#.#.#.#.........#.#...#.#.....#.#.#...#...#...#...#...#.#.........#.#.#.......#.#...........#...#...#.....#.....#...# +#.#####.#.###.#.###.#.#.#.#.#.#####.#####.###.#.#.#####.#.###.#######.#.###.#.###.#######.#.#.#####.#.#.#############.###.#####.#.#.###.###.# +#.....#.#.#...#.#.#...#.#...#.....#.#.#.......#.#.#...#.#...#.#.....#...#...#...#.....#...#.#.......#...#.........#.............#.#...#.#...# +#.###.#.#.#.###.#.#####.#####.#####.#.#.#######.#.#.#.#.#.#.#.###.#.#####.#.###.#.###.#.###.#.#########.#.###.#.###.#####.#####.#.#.#.###.#.# +#...#.#.#.#.....#.....#...............#.#.......#...#.#.#.#.#.....#...#.........#.#...#.#...#.#.....#...#...#.#.........#.#.....#.....#...#.# +###.#.#.#.#######.#####.###############.###.#.#######.#.###.#.###.#####.###.#####.#.###.###.#.#.###.###.###.#.#.#.###.#.###.###.#####.#.###.# +#.#.#...#.#...........#.....#...#.#.....#...#.......#.#...#.......#...#...#.#.#...#...#.....#.#.#...#...#...#.........#.#...#.#...#...#...#.# +#.#.###.#.#####.#####.#####.#.#.#.#.#####.#########.#.###.#########.#.###.#.#.#.#####.#########.#.###.#.#.###.#########.#.###.#.#.###.###.### +#.#...#.#.#...#.....#...#.#...#...#...#.....#.....#...#.#...........#.......#.......#...#...#...#...#.#.....#.#.....#.....#.....#...#.#.#...# +#.###.#.#.#.#.#######.#.#.#####.#####.#.###.#.#.#.#####.###################.#######.###.#.#.#.#.###.#.#######.#.###.#####.#.###.###.#.#.###.# +#.......#...#.....#...#...#.....#...#.#...#...#.#.#.............#.........#...#...#.#.#...#.....#...#...#...#.....#.....#...#...#...#...#...# +#.#####.#########.#.#######.#####.#.#.###.#####.#.#########.#.###.#.#####.###.###.#.#.###########.###.#.#.#.###########.#.###.#.#.###.###.#.# +#.....#.#.......#.#.........#.....#.#...#.#.....#...........#.#...#.#...#...#...#.#...#.......#...#.....#.#.....#.......#...#...............# +#.#.###.#.#.###.#.###########.#####.###.#.#.###################.###.#.#.###.###.#.###.###.###.#.#.#.#####.#.#.#.#.#########.#.#####.#.#.##### +#.#.....#.......#.....#...........#.....#.#.....#.....#.......#.#...#.#...#...#.#.....#...#...#.#.#.#...#.#.#.#...#.........#.#.....#.......# +#.#####.###.#.#######.#.###########.#.#########.#.###.#.#.#.#.#.#####.#.#.###.#.#.#####.#.###.#.###.#.#.#.#.#.#####.#.#######.#.#####.###.#.# +#...#.......#.#.....#.........#.....#.#.....#.....#.#...#...#...#...#.#.#...#.#.#.......#.#.........#.#...#...#.....#...#.....#.#.......#.#.# +###.#.#######.#.#########.#.###.###.#.###.#.#.#.#.#.#.#########.#.#.#.#.###.#.#.#####.#.###.#.###.###.#########.#######.#.#####.#.###.#.#.#.# +#.#...#...#...#.......#...#...#...#.#.....#.....#.#...#.......#.#.#.#.#.....#...#.....#...#.#.#...#.#.#.................#.#.....#.#...#.#.#.# +#.#####.#.#.#########.#.#####.#.#.#.###.#######.#.#####.#####.###.#.#.###.#######.###.#.#.#.###.###.#.#######.#####.###.#.#.#####.#.#.#.#.#.# +#.......#.#.#.......#.#.#.....#.#.#.#...#.....#.#.....#.....#...#.#...#.#.#.....#.#.#...#...#.......#.......#.#...#.....#.#.....#.#...#...#.# +#.#####.#.#.#.#####.#.#.#.###.#.#.#.#.###.###.#.#####.#.#.#.###.#.###.#.#.###.#.#.#.#.###.###.#.#.#.#######.###.#.#.#.###.#####.#.###.###.#.# +#.#.......#...#.#...#...#.#...#.#.....#...#.#.#...#...#.#.#.#.....#...#.#...#...#.#.#.#...#...#...#.......#...#.#.#.#.......#...#...#...#.#.# +#.#####.#######.#.###.#.#.#.#.#.#.###.#.###.#.#####.#.#.###.#.#########.###.#.###.#.#.#####.#####.#.#.#.#.###.#.#.#########.#.#####.#.#.#.#.# +#...........#.#...#...#...#...#...#.....#...#...#...#.#...#.....#.......#.#.#.....#.#.......#.....#.#.#.....#.........#...#.#.....#...#...#.# +#####.#.#.#.#.#.#######.#####.#.#.#.#######.###.#.#.#####.#######.#######.#.###.###.###########.###.#.#####.#########.#.#.#.#####.###.#.#.#.# +#...#...#.#...#.....#...#...#.#.#.#.#.........#.....#...#.......#.....#...#...#.#...........#...#.#.#...#...........#...#.#.#.......#...#...# +#.#.###.#.#########.#.###.#.#.#.#.#.###.#####.#.###.#.#.#######.#####.#.#####.#.#.###.#####.#.###.#.#####.#########.#####.#.#.#######.###.#.# +#.#...#.#.........#.#...#.#.#.#...#...#.....#.......#.#.#.....#.....#.#.#.....#.#.#...#...#...#...........#.......#.#.....#...............#.# +#.###.#.#########.#.###.#.###.#.#####.#####.#.#####.#.#.#.#.#####.#.#.#.#.#####.###.###.#.#.###############.#####.#.#.#########.###.###.#.### +#...#.#.....#.....#.....#.#.........#.....#.#.#...#.#.#.#.#.......#...#...#.#...#...#...#.#.#.............#.....#...#.........#...#.....#...# +#.#.#.#.###.#.###.#######.#.###.###.#####.#.#.#.#.###.#.#.#######.#####.###.#.###.#####.###.#.###########.#.#####.###########.#.#######.#.#.# +#.#.#.#.....#.#.#.#.......#.#.....#...#...#...#.#.....#...#.....#.....#.#...#.........#...#.#.#.....#.#...#.#...#.......#...#...#.....#...#.# +###.#.#.#####.#.#.###.#.#.#.#########.#.#######.###########.#.#######.#.###.#####.#.#.###.#.#.#.#.#.#.#.#####.#.#####.###.#.###.#.###.###.#.# +#...#.......#.#.#.#...#.#.#.......#...#.........#.........#.#.......#.....#.......#.....#...#.#.#.#.#.#.#.....#...#...#...#...#.#.#.#.....#.# +#.#########.#.#.#.#.#.#.#.#######.#.#############.#####.###.#####.###.###.#########.###.#####.###.#.#.#.###.#####.###.#.#####.###.#.#####.#.# +#...#.....#.#.#...#.#...#.....#...#.#...........#.#.....#...#...#...#...#...#.......#.........#...#...#.#...#.......#.#.#...#.....#.....#.#.# +#.#.#####.#.#.#.###.###.#####.#.###.#.#######.#.#.#.#####.###.#.###.###.###.#.#################.###.###.#.#.#######.#.#.#.###.#####.###.#.#.# +#.#.....#...#.#...#...#.#...#.........#.......#.#.#.....#...#.....#...#...#.................#.....#.#...#.#.#.....#.#.#.#.....#...#...#.#.#.# +#####.#.###.#.###.###.#.#.###.###.#####.#######.###.###.###.#.#######.#.#.#.#.#############.#####.#.#.###.###.###.#.###.###.###.#.###.#.#.#.# +#...#.#...#...#.......#.#.#.....#...#...#.....#...#.#.#...#.#.#.....#.#.#.#.#...#.....#...#.#.....#.#.#.#.....#...#...#...#.#.#.#.#...#.#.#.# +#.#.#####.###.#########.#.#.###.###.#.###.###.###.#.#.#.#.#.#.#.###.#.#.###.###.#.#.#.#.###.#.#####.#.#.#.#####.#####.###.#.#.#.#.#.#.#.#.#.# +#.#.......#...#.........#.#.....#.#...#.....#.#...#...#...#.#.#.#.#...#...#...#.#...#...#...#.#...#.#.#...#...#.#...#...#.#...#.#...#.#.#.#.# +#.#######.#####.#########.#.#.###.#####.###.###.#######.###.#.#.#.###.###.###.#.###.###.#.###.#.#.###.#####.#.#.#.#.###.#.#####.###.###.#.#.# +#.#...#...#.....#.........#.#.....#...#...#.#...#.....#...#.#.#.......#.....#.#.........#.#.....#...#.......#.#.#.#.#...#.#.....#.#...#.....# +#.#.#.#####.#####.#.#.#####.#####.#.###.###.#.###.###.#.#.#.#.#.#.#####.#.#.#.#####.#####.#########.#########.#.###.#.###.#.###.#.###.###.#.# +#.#.#.#.....#...#...#.#.....#.....#.#...#...#.#...#.#.#.#.....#.#.#.....#.#.#.#.#...#...#.#.......#.........#.#.......#...#...#.....#...#...# +#.#.#.#.#####.#.#.#.#.#.#####.#####.#.###.###.#.###.#.###.#####.#.#.###.#.###.#.#.###.#.#.#.#####.#######.#.#.#.#.###.#.#.###.#.#.#####.#.### +#...#.#.......#.#.#.#.#.#.........#.....#...#.#...#.#...#...#.....#...#.#.....#.....#.#.#.....#...#.......#.#.#.#...#.#.#.......#.#.....#.#.# +#.###.#.#######.#.#.###.#########.#.#######.#.###.#.###.#####.###.###.#.#######.#####.#.#.#####.###.#########.#####.#.#.###.###.#.#.#####.#.# +#.#.#.............#...#...#.....#.#...#...#.#.....#...#.......#...#...#.#...#...#.....#.#.#.....#.............#.....#...#.....#...#...#.#.#.# +#.#.#########.#.#.###.###.#.###.#.###.#.#.#.#########.#########.###.###.#.#.#.###.#####.###.#####.###########.#.###############.#.###.#.#.#.# +#.....#...#...#.#...#...#...#.#.#...#.#.#.......#.......#.#.....#.#.#...#.#...#...#...#...#.......#...........#...#.............#...........# +#####.#.#.#.###.#####.#######.#.###.###.#######.###.###.#.#.#####.#.#.###.###.#.#####.#.#.#.#####.#.#######.#.###.#.#########.#.#.#.#.#.#.#.# +#.....#...#.#.......#.........#...#.....#.....#...#.#...#.#.....#...#.........#.....#...#.#.....#.#.#.......#...#.#.#.......#.......#.#...#.# +#.#######.#.#.#####.###.###.#####.#######.#.#####.#.###.#.#####.#.#########.#####.#.###.#.###.#.###.#.#########.#.#.#.#####.#.#######.#.#.#.# +#.#.....#.#...#...#...#.#...#.....#...#...#.#.....#...#...#.....#.#.......#.....#.#.#...#...#.#.....#.....#...#...#...#...#.................# +#.#.###.#.#####.#.#.#.#.#.###.###.#.###.#.#.#.#####.#.#####.#####.###.#.###.###.###.#.###.#.#.#######.###.#.#.#.#######.#.#.###.#.#######.#.# +#...#.....#...#.#...#...#.......#.....#.#.#.#.#.....#.#...#.#...#...#...#...#.#...#.#.....#.#.#.....#.#.#.#.#...#.......#.#.........#.......# +#.#######.#.#.#.###.###.#####.#.#####.#.#.###.#######.#.#.#.#.#.###.###.#.###.###.#.#.###.#.###.###.#.#.#.#.#####.#.#########.#.#.#.#.####### +#.#.....#.#.#.#.#.#...#.#...#.#.....#...#.....#.......#.....#.#.#...........#...#...#.#...#.....#.#.#...#.#.....#.#.........#.#.#.#.#.#.....# +#.#.###.#.#.#.#.#.###.#.#.#.#######.#.#######.#.###.###.#####.#.#######.###.###.#####.#.#########.#.###.#.#######.#######.###.#.#.#.#.#.###.# +#.#.#.......#...#...#.....#.......#.#...#...#...#...#...#.....#.....#...#...#.......#.#.........#...#...#.....#...#.....#.#...#.#.#.#.#.#...# +###.#####.#########.#############.#.#####.#.#####.###.###.#.#######.#####.###.#####.#.#####.###.#.#####.#####.#.#######.#.#.###.#.#.#.###.#.# +#...#.............#.....#.........#...#...#...#.....#...#.#.#.............#.....#...#.#.#...#...#.....#.....#.......#...#...#...#.#.......#.# +#.###.###########.###.#.#.###########.#.###.#.#########.#.#.#.###.###.#####.###.#####.#.#.###.#######.#############.#.#.#####.#.#.#.#######.# +#...#.#.....#...#...#.#...............#.#...#.........#.#.#...#...#.....#.#...#...#...#.#.#.........#...........#.#.#.#...#...#.#.#.#...#.#.# +#.#.#.#.#.#.###.###.#.#####.#####.###.#.#.#.#########.#.#######.#.#.###.#.###.###.#.###.#.#####.#.#.#####.#####.#.#.#.###.#####.#.###.#.#.#.# +#.....#.#.#...#.#...#...#.........#.....#.#.......#.#...........#.#.#.#.#.......#...#...#.....#.#.#.#.....#.....#.#...#.#.#...#.#.#...#.#.#.# +#.#######.###.#.#.###.#.###.#####.#.###########.#.#.#####.###.#.###.#.#.#.#.###.#####.#.#####.#.#.#.###.#.#.#####.#####.#.#.#.#.#.#.###.#.#.# +#.........#.#...#...#.......#.....#.#.........#.#.......#...#.........#.#.#...#...#...#...#...#.#.#.....#.#.....#...........#...#...#.#.#.#.# +###########.#.#.###.#.#########.###.#.#.#####.#.#######.###.#.#########.#.###.###.###.#####.#.#.#.#######.#####.###############.#####.#.#.#.# +#.....#.......#...#.#...#.......#...#.#.#...#...#.....#.....#.#.........#.......#.....#.....#.#.#.....#.#.....#...........#...#...#...#.#.#.# +#.#.#.#.#.###.###.#.###.#.#####.#.#####.#.#######.###.#######.#.###############.#####.#.#######.#####.#.###.#############.###.#.#.#.###.#.#.# +#.#.#.#.#.........#.#...#...#...#.#.....#.......#...#.#.....#.#.....#.....#.......#...#...#.....#.#...#.....#.........#...#.....#...#...#...# +#.#.#.#.#####.#####.#####.###.#.#.#.#####.###.###.#.#.#####.#.#####.#.###.#.#.#####.#####.#.###.#.#.###.#####.#.#######.#.#.#######.#.#####.# +#.#...#.#.#.....#...#.....#...#.#...#.......#.....#.#.......#.......#...#...#.#.....#...#...#.....#.#...#...#.#.........#.#.........#.#...#.# +#.###.#.#.#.#.#.#.#.#.#####.#############.#########.#.#####.###########.#####.#.#######.#########.#.###.#.#.#.###########.#.#########.#.#.#.# +#...#.#...#...#...#.#...#.#.............#.#.........#.#...#.#...#.......#.#...#.#.....#.........#.#...#.#.#...#.......#...#...#.....#.#.#.#.# +###.#.#.#.###.#########.#.#######.#####.###.#########.#.#.#.#.#.#.#######.#.###.#.###.#.#######.#.###.#.#.#####.#####.#.#.###.#####.#.#.#.### +#.#.#...#...#...........#...#.....#...#...#.#.#.....#.#.#.#.#.#.#...#.........#...#...#.......#.....#.#.#...#...#...#.......................# +#.#.#.#.###.#.#.###########.#.###.#.#.###.#.#.#.###.#.#.#.#.#.#.###.#########.#####.#####.###.#####.#.#####.#.#####.#.#.#.#.#.#.###.#.#.###.# +#.#.#.....#...#.....................#...#...#.....#...#.#.#...#.....#.....#.#...#.#.#...#...#.....#.#.....#...#.....#.#.#.#.#.#.#.#.#.#.#.#.# +#.#.#.#####.#.#.#######.#####.#.###.###.###.#######.###.#.#######.###.###.#.###.#.#.#.#.###.#.###.#.#####.#####.#.###.#.#.#.#.#.#.#.#.#.#.#.# +#.#.#.#...#...#.#...#...#.....#.#.#.#.#...#.#.....#.#...#.#...#...#...#...#.......#...#.#.......#.#...#...#...#.#.....#...#.....#...#.....#.# +#.#.#.#.#.#.#.###.#.#.#.#.#####.#.#.#.###.###.###.###.###.#.#.#.###.###.#######.#######.#.###.###.#.###.###.#.#.#############.###########.#.# +#.#.#...#...#.....#...#.#.#...#.............#.#.#...#.#.#.#.#.....#.#.#.......#.#.....#.#...............#...#.#.#.....#.....#.#.........#.#.# +#.#.#.###.#.#.#########.#.#.#.#####.#.#.###.#.#.###.#.#.#.#########.#.#######.###.###.#.#####.#.#.#.#.###.###.#.#.###.#.###.###.#######.###.# +#S............#.........#...#.........#...#.......#.....#.....................................#...#.#.......#.....#.....#.......#...........# +############################################################################################################################################# diff --git a/day16/sample1.in b/day16/sample1.in new file mode 100644 index 0000000..2c21676 --- /dev/null +++ b/day16/sample1.in @@ -0,0 +1,15 @@ +############### +#.......#....E# +#.#.###.#.###.# +#.....#.#...#.# +#.###.#####.#.# +#.#.#.......#.# +#.#.#####.###.# +#...........#.# +###.#.#####.#.# +#...#.....#.#.# +#.#.#.###.#.#.# +#.....#...#.#.# +#.###.#.#.#.#.# +#S..#.....#...# +############### diff --git a/day16/sample3.in b/day16/sample3.in new file mode 100644 index 0000000..bc61c57 --- /dev/null +++ b/day16/sample3.in @@ -0,0 +1,17 @@ +################# +#...#...#...#..E# +#.#.#.#.#.#.#.#.# +#.#.#.#...#...#.# +#.#.#.#.###.#.#.# +#...#.#.#.....#.# +#.#.#.#.#.#####.# +#.#...#.#.#.....# +#.#.#####.#.###.# +#.#.#.......#...# +#.#.###.#####.### +#.#.#...#.....#.# +#.#.#.#####.###.# +#.#.#.........#.# +#.#.#.#########.# +#S#.............# +################# diff --git a/day16/sol.py b/day16/sol.py new file mode 100644 index 0000000..3379b3f --- /dev/null +++ b/day16/sol.py @@ -0,0 +1,44 @@ +import astar + + +def solve(file): + map = [line.strip() for line in file] + for y in range(len(map)): + for x,c in enumerate(map[y]): + if c == 'S': + start = (x,y,'>') + elif c == 'E': + end = (x,y) + + def isgoal(n): + x,y,_ = n + #print("isgoal", n) + return (x,y) == end + + def get(x,y): + return map[y][x] + + print(map) + print(start, end) + + def neighbors(src): + x,y,dir = src + n = [] + def push(x,y, newdir): + if 0 <= y < len(map): + if 0 <= x < len(map[y]): + if get(x,y) != '#': + cost = 1 if dir == '.' or newdir == dir else 1001 + n.append((cost, (x,y,newdir))) + push(x-1, y, '<') + push(x+1,y, '>') + push(x, y-1, '^') + push(x, y+1, 'v') + return n + + cost, _, nodes = astar.search(start, isgoal, neighbors) + print(cost) + print(len(set((x,y) for x,y,_ in nodes))) + +solve(open('sample1.in')) +solve(open('input'))