Mercurial > repos > other > adventofcode2023
changeset 16:9b1d04091335
Path finding through the pipes for day 10
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 10 Dec 2023 19:17:56 +0000 |
parents | 5c8d2d181b94 |
children | 92144824cbb7 |
files | day10.rb day10.txt |
diffstat | 2 files changed, 186 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day10.rb Sun Dec 10 19:17:56 2023 +0000 @@ -0,0 +1,82 @@ +#! /usr/bin/env ruby + +if ARGV.length != 1 + abort("Incorrect arguments - needs input file") +elsif not File.exist? (ARGV[0]) + abort("File #{ARGV[0]} did not exist") +end + +file = ARGV[0] + +charmap = File.open(file, "r").each_line(chomp: true).map {|line| line.each_char.to_a}.to_a +$map_height = charmap.length +$map_width = charmap[0].length + +# Ugly way to find the start, because we know there's one (for now) +start = charmap.each_with_index.flat_map { |columns, i| j = columns.find_index("S"); if j then [j, i] else nil end }.compact +start_x, start_y = start + +$direction_inversion = {north: :south, south: :north, east: :west, west: :east} + +$pipemap = charmap.map do |row| + row.map do |cell| + case cell + when "|" + [:north, :south] + when "-" + [:east, :west] + when "L" + [:north, :east] + when "J" + [:north, :west] + when "7" + [:south, :west] + when "F" + [:south, :east] + when "S" + # Assume all directions are valid + [:north, :source, :east, :west] + else + [] + end + end +end + +def valid_position? (x, y) + x >= 0 and x < $map_width and y >= 0 and y < $map_height +end + +def get_target_for_step(step) + case step.direction + when :north then [step.x, step.y-1] + when :south then [step.x, step.y+1] + when :east then [step.x+1, step.y] + when :west then [step.x-1, step.y] + end +end + +def get_next_step(step) + new_x, new_y = get_target_for_step(step) + target_inputs = valid_position?(new_x, new_y) ? $pipemap[new_y][new_x] : [] + required_direction = $direction_inversion[step.direction] + target_inputs.include?(required_direction) ? Step.new(new_x, new_y, (target_inputs - [required_direction])[0]) : nil +end + +Step = Struct.new(:x, :y, :direction) + +routes = [:north, :south, :east, :west].map {|dir| [Step.new(start_x, start_y, dir)]} + +while ! routes.any? {|route| last_step = route[-1]; route.length > 1 and last_step.x == start_x and last_step.y == start_y} + routes = routes.map do |route| + next_step = get_next_step(route[-1]) + if next_step + route << next_step + else + nil + end + end.compact +end + +routes.each {|route| puts "Route: #{route.map{|step| [step.x, step.y]}}"} +# Subtract one because we end up back at the start +puts "Steps: #{routes.map{|route| (route.length - 1)/2}}"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/day10.txt Sun Dec 10 19:17:56 2023 +0000 @@ -0,0 +1,104 @@ +--- Day 10: Pipe Maze --- + +You use the hang glider to ride the hot air from Desert Island all the way up to the floating metal island. This island is surprisingly cold and there definitely aren't any thermals to glide on, so you leave your hang glider behind. + +You wander around for a while, but you don't find any people or animals. However, you do occasionally find signposts labeled "Hot Springs" pointing in a seemingly consistent direction; maybe you can find someone at the hot springs and ask them where the desert-machine parts are made. + +The landscape here is alien; even the flowers and trees are made of metal. As you stop to admire some metal grass, you notice something metallic scurry away in your peripheral vision and jump into a big pipe! It didn't look like any animal you've ever seen; if you want a better look, you'll need to get ahead of it. + +Scanning the area, you discover that the entire field you're standing on is densely packed with pipes; it was hard to tell at first because they're the same metallic silver color as the "ground". You make a quick sketch of all of the surface pipes you can see (your puzzle input). + +The pipes are arranged in a two-dimensional grid of tiles: + + | is a vertical pipe connecting north and south. + - is a horizontal pipe connecting east and west. + L is a 90-degree bend connecting north and east. + J is a 90-degree bend connecting north and west. + 7 is a 90-degree bend connecting south and west. + F is a 90-degree bend connecting south and east. + . is ground; there is no pipe in this tile. + S is the starting position of the animal; there is a pipe on this tile, but your sketch doesn't show what shape the pipe has. + +Based on the acoustics of the animal's scurrying, you're confident the pipe that contains the animal is one large, continuous loop. + +For example, here is a square loop of pipe: + +..... +.F-7. +.|.|. +.L-J. +..... + +If the animal had entered this loop in the northwest corner, the sketch would instead look like this: + +..... +.S-7. +.|.|. +.L-J. +..... + +In the above diagram, the S tile is still a 90-degree F bend: you can tell because of how the adjacent pipes connect to it. + +Unfortunately, there are also many pipes that aren't connected to the loop! This sketch shows the same loop as above: + +-L|F7 +7S-7| +L|7|| +-L-J| +L|-JF + +In the above diagram, you can still figure out which pipes form the main loop: they're the ones connected to S, pipes those pipes connect to, pipes those pipes connect to, and so on. Every pipe in the main loop connects to its two neighbors (including S, which will have exactly two pipes connecting to it, and which is assumed to connect back to those two pipes). + +Here is a sketch that contains a slightly more complex main loop: + +..F7. +.FJ|. +SJ.L7 +|F--J +LJ... + +Here's the same example sketch with the extra, non-main-loop pipe tiles also shown: + +7-F7- +.FJ|7 +SJLL7 +|F--J +LJ.LJ + +If you want to get out ahead of the animal, you should find the tile in the loop that is farthest from the starting position. Because the animal is in the pipe, it doesn't make sense to measure this by direct distance. Instead, you need to find the tile that would take the longest number of steps along the loop to reach from the starting point - regardless of which way around the loop the animal went. + +In the first example with the square loop: + +..... +.S-7. +.|.|. +.L-J. +..... + +You can count the distance each tile in the loop is from the starting point like this: + +..... +.012. +.1.3. +.234. +..... + +In this example, the farthest point from the start is 4 steps away. + +Here's the more complex loop again: + +..F7. +.FJ|. +SJ.L7 +|F--J +LJ... + +Here are the distances for each tile on that loop: + +..45. +.236. +01.78 +14567 +23... + +Find the single giant loop starting at S. How many steps along the loop does it take to get from the starting position to the point farthest from the starting position?