From b0d458b72e1e214bc89c6e3aaa012e95b1a91b9d Mon Sep 17 00:00:00 2001 From: Jake Pullen Date: Sat, 23 Dec 2023 09:14:01 +0000 Subject: [PATCH] day 23 Done --- 2023/23/part 1 solution.py | 107 ++++++++++++++++++++++++++++++++++ 2023/23/part 2 solution.py | 115 +++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+) create mode 100644 2023/23/part 1 solution.py create mode 100644 2023/23/part 2 solution.py diff --git a/2023/23/part 1 solution.py b/2023/23/part 1 solution.py new file mode 100644 index 0000000..7edd4d9 --- /dev/null +++ b/2023/23/part 1 solution.py @@ -0,0 +1,107 @@ +import sys +from collections import deque + +with open(r'advent_of_code\2023\23\input.txt', 'r') as file: + input = file.read() + +test_input = '''#.##################### +#.......#########...### +#######.#########.#.### +###.....#.>.>.###.#.### +###v#####.#v#.###.#.### +###.>...#.#.#.....#...# +###v###.#.#.#########.# +###...#.#.#.......#...# +#####.#.#.#######.#.### +#.....#.#.#.......#...# +#.#####.#.#.#########v# +#.#...#...#...###...>.# +#.#.#v#######v###.###v# +#...#.>.#...>.>.#.###.# +#####v#.#.###v#.#.###.# +#.....#...#...#.#.#...# +#.#########.###.#.#.### +#...###...#...#...#.### +###.###.#.###v#####v### +#...#...#.#.>.>.#.>.### +#.###.###.#.###.#.#v### +#.....###...###...#...# +#####################.#''' + +# Set recursion limit +sys.setrecursionlimit(10**6) + +# Read input +input_data = input +lines = input_data.split('\n') + +# Create grid from input +grid = [[char for char in row] for row in lines] +num_rows = len(grid) +num_cols = len(grid[0]) + +# Directions for navigation +directions = [['^',-1,0],['v', 1,0],['<', 0,-1],['>',0,1]] + +def solve(): + # Set of vertices + vertices = set() + + # Identify vertices + for row in range(num_rows): + for col in range(num_cols): + num_neighbors = sum(1 for char,dr,dc in directions if 0<=row+dr2 and grid[row][col]!='#': + vertices.add((row,col)) + + # Identify start and end points + start, end = None, None + for col in range(num_cols): + if grid[0][col]=='.': + vertices.add((0,col)) + start = (0,col) + if grid[num_rows-1][col]=='.': + vertices.add((num_rows-1,col)) + end = (num_rows-1,col) + + # Create edges + edges = {vertex: [] for vertex in vertices} + for vertex in vertices: + queue = deque([(vertex[0],vertex[1],0)]) + seen = set() + while queue: + r,c,d = queue.popleft() + if (r,c) in seen: + continue + seen.add((r,c)) + if (r,c) in vertices and (r,c) != vertex: + edges[vertex].append(((r,c),d)) + continue + for char,dr,dc in directions: + if (0<=r+dr', '^', 'v'] and grid[r][c]!=char: + continue + queue.append((r+dr,c+dc,d+1)) + + # Depth-first search + max_distance = 0 + seen_grid = [[False for _ in range(num_cols)] for _ in range(num_rows)] + def dfs(v,d): + nonlocal max_distance + r,c = v + if seen_grid[r][c]: + return + seen_grid[r][c] = True + if r==num_rows-1: + max_distance = max(max_distance, d) + for (next_vertex,next_distance) in edges[v]: + dfs(next_vertex,d+next_distance) + seen_grid[r][c] = False + + # Start DFS from start point + dfs(start,0) + + return max_distance + +# Print results +print(solve()) \ No newline at end of file diff --git a/2023/23/part 2 solution.py b/2023/23/part 2 solution.py new file mode 100644 index 0000000..daa066d --- /dev/null +++ b/2023/23/part 2 solution.py @@ -0,0 +1,115 @@ +import sys +from collections import deque + +with open(r'advent_of_code\2023\23\input.txt', 'r') as file: + input = file.read() + +test_input = '''#.##################### +#.......#########...### +#######.#########.#.### +###.....#.>.>.###.#.### +###v#####.#v#.###.#.### +###.>...#.#.#.....#...# +###v###.#.#.#########.# +###...#.#.#.......#...# +#####.#.#.#######.#.### +#.....#.#.#.......#...# +#.#####.#.#.#########v# +#.#...#...#...###...>.# +#.#.#v#######v###.###v# +#...#.>.#...>.>.#.###.# +#####v#.#.###v#.#.###.# +#.....#...#...#.#.#...# +#.#########.###.#.#.### +#...###...#...#...#.### +###.###.#.###v#####v### +#...#...#.#.>.>.#.>.### +#.###.###.#.###.#.#v### +#.....###...###...#...# +#####################.#''' + +# Set recursion limit +sys.setrecursionlimit(10**6) + +# Read input +input_data = input +lines = input_data.split('\n') + +# Create grid from input +grid = [[char for char in row] for row in lines] +num_rows = len(grid) +num_cols = len(grid[0]) + +# Directions for navigation +directions = [['^',-1,0],['v', 1,0],['<', 0,-1],['>',0,1]] + +def solve(is_part1): + # Set of vertices + vertices = set() + + # Identify vertices + for row in range(num_rows): + for col in range(num_cols): + num_neighbors = 0 + for char,dr,dc in directions: + if (0<=row+dr2 and grid[row][col]!='#': + vertices.add((row,col)) + + # Identify start and end points + start, end = None, None + for col in range(num_cols): + if grid[0][col]=='.': + vertices.add((0,col)) + start = (0,col) + if grid[num_rows-1][col]=='.': + vertices.add((num_rows-1,col)) + end = (num_rows-1,col) + + # Create edges + edges = {} + for vertex in vertices: + edges[vertex] = [] + queue = deque([(vertex[0],vertex[1],0)]) + seen = set() + while queue: + r,c,d = queue.popleft() + if (r,c) in seen: + continue + seen.add((r,c)) + if (r,c) in vertices and (r,c) != vertex: + edges[vertex].append(((r,c),d)) + continue + for char,dr,dc in directions: + if (0<=r+dr', '^', 'v'] and grid[r][c]!=char: + continue + queue.append((r+dr,c+dc,d+1)) + + # Depth-first search + count = 0 + max_distance = 0 + seen_grid = [[False for _ in range(num_cols)] for _ in range(num_rows)] + def dfs(v,d): + nonlocal count + nonlocal max_distance + count += 1 + r,c = v + if seen_grid[r][c]: + return + seen_grid[r][c] = True + if r==num_rows-1: + max_distance = max(max_distance, d) + for (next_vertex,next_distance) in edges[v]: + dfs(next_vertex,d+next_distance) + seen_grid[r][c] = False + + # Start DFS from start point + dfs(start,0) + + return max_distance + +# Print results +print(solve(True)) +print(solve(False)) \ No newline at end of file