Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.0 kB · 48 lines
Ruby
at rust
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849#!/usr/bin/env ruby# frozen_string_literal: true
require 'matrix'
class Nanobot include Comparable
@@id = 0 # rubocop:disable Style/ClassVars
attr_reader :position, :range
def initialize(x, y, z, range) @id = @@id += 1 # rubocop:disable Style/ClassVars @position = Vector[x.to_i, y.to_i, z.to_i] @range = range.to_i end
def x; position[0]; end def y; position[1]; end def z; position[2]; end
def <=>(other) range <=> other.range end
def distance_to(other) (position - (other.respond_to?(:position) ? other.position : other)).sum(&:abs) end
def can_reach?(other) distance_to(other) <= range end
def to_s "pos <=#{position[0]},#{position[1]},#{position[2]}>, r=#{range}" endend
def count_in_range(bots, position); end
bots = File.read('input.txt').scan(/(-?\d+).+?(-?\d+).+?(-?\d+).+?(\d+)/).map { |x, y, z, r| Nanobot.new x, y, z, r }
strongest = bots.max
in_range = bots.select { |b| strongest.can_reach? b }
puts "Part 1: #{in_range.size}"