Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.0 kB · 47 lines
Ruby
at rust
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#!/usr/bin/env ruby# frozen_string_literal: true
require 'matrix'
CANDIDATES = { Vector[0, 1] => [Vector[1, 0], Vector[-1, 0]], Vector[0, -1] => [Vector[1, 0], Vector[-1, 0]], Vector[1, 0] => [Vector[0, 1], Vector[0, -1]], Vector[-1, 0] => [Vector[0, 1], Vector[0, -1]]}.freeze
def next_direction(grid, pos, direction) x, y = pos[0], pos[1] CANDIDATES[direction].find do |c| new_x, new_y = x + c[0], y + c[1] return false if x < 0 || x >= grid[0].size || y < 0 || y > grid.size grid[new_y][new_x] != ' ' endend
grid = ARGF.readlines.map { |l| l.chomp.chars }
position = Vector[grid[0].index('|'), 0]direction = Vector[0, 1]letters = []steps = 0
loop do x, y = position[0], position[1] cell = grid[position[1]][position[0]]
break if cell == ' '
steps += 1
letters << cell if cell =~ /[A-Za-z]/
direction = next_direction(grid, position, direction) if grid[y][x] == '+'
break if direction.nil?
position += directionend
puts letters.joinputs steps