Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.2 kB · 59 lines
Ruby
at rust
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960#!/usr/bin/env ruby# frozen_string_literal: true
require 'matrix'
require_relative '../../../lib/astar'
INPUT = ARGV[0]&.to_i || 1364GOAL = Vector[*(ARGV[1] || '31,39').split(?,).map(&:to_i)]# rubocop:disable Layout/FirstArrayElementIndentation, Layout/ArrayAlignmentADJACENTS = [ [0, -1], [-1, 0], [1, 0], [0, 1]].map { Vector[*_1] }# rubocop:enable Layout/FirstArrayElementIndentation, Layout/ArrayAlignment
class Vector def x; self[0]; end def y; self[1]; endend
class InfGrid def neighbors(pos) ADJACENTS.map { pos + _1 }.select do !wall?(_1) && _1.x >= 0 && _1.y >= 0 end end
def cost(_source, _destination) 1 end
private
def wall?(pos) x, y = pos.x, pos.y value = (x * x) + (3 * x) + (2 * x * y) + y + (y * y) + INPUT value.to_s(2).chars.count(?1).odd? endend
GRID = InfGrid.newSTART = Vector[1, 1]_, cost_so_far = AStar.find_path(GRID, START, GOAL)
puts cost_so_far[GOAL]
queue = [START]dist = { START => 0 }while (pos = queue.shift) GRID.neighbors(pos).each do |adj| next if dist.include? adj dist[adj] = dist[pos] + 1 queue << adj endend
puts dist.values.count { _1 <= 50 }