changeset 30:6de4f4d5404d

Implement Day 21 "possible spaces in a maze" Part 2 needs something like "find the repeats and lowest common multiple" that I'm not bothering with
author IBBoard <dev@ibboard.co.uk>
date Wed, 03 Jan 2024 12:01:18 +0000
parents 739415015d27
children 47dc75915e91
files day21.rb day21.txt
diffstat 2 files changed, 79 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day21.rb	Wed Jan 03 12:01:18 2024 +0000
@@ -0,0 +1,42 @@
+#! /usr/bin/env ruby
+
+require 'set'
+
+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]
+
+start = [0,0]
+
+$maze = File.open(file, "r").each_line(chomp: true).with_index.map do |line, y|
+    line.each_char.with_index.map do |char, x|
+        start = [x,y] if char == "S"
+        # Plot "blocked" as true
+        char == "#"
+    end
+end
+
+puts "#{$maze.map{|row| row.map{|cell| cell ? "#" : " "}.join("")}.join("\n")}"
+
+steps = 64
+spaces = Set.new
+spaces << start
+
+$moves = [[0,1], [0,-1], [1,0], [-1,0]]
+
+$maze_height = 0...$maze.length
+$maze_width = 0...$maze[0].length
+
+def next_spaces(space)
+    $moves.map {|move| [move[0] + space[0], move[1] + space[1]]}.filter {|new_space| $maze_width.cover?(new_space[0]) && $maze_height.cover?(new_space[1]) && !$maze[new_space[1]][new_space[0]]}
+end
+
+steps.times do |step|
+    spaces = Set.new(spaces.flat_map {|space| next_spaces(space)})
+end
+
+puts "#{spaces.length} candidates: #{spaces}"
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/day21.txt	Wed Jan 03 12:01:18 2024 +0000
@@ -0,0 +1,37 @@
+--- Day 21: Step Counter ---
+
+You are in a maze. `S` is your starting position. `.` is open. `#` is blocked. How many spaces can you get to in N steps?
+
+e.g. for this map:
+
+...........
+.....###.#.
+.###.##..#.
+..#.#...#..
+....#.#....
+.##..S####.
+.##..#...#.
+.......##..
+.##.#.####.
+.##..##.##.
+...........
+
+You can be in any of 16 spaces after 6 steps.
+
+...........
+.....###.#.
+.###.##.O#.
+.O#O#O.O#..
+O.O.#.#.O..
+.##O.O####.
+.##.O#O..#.
+.O.O.O.##..
+.##.#.####.
+.##O.##.##.
+...........
+
+How many spaces are there after 64 steps?
+
+-- Part 2 --
+
+The map tiles infinitely and you need to take 26501365 steps. How many spaces can you reach now? And does anyone care?
\ No newline at end of file