Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.1 kB · 64 lines
Ruby
at rust
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#!/usr/bin/env ruby# frozen_string_literal: true
require 'matrix'
class Particle attr_reader :position
def initialize(pos, vel, acc) @position = pos @velocity = vel @acceleration = acc @orig_pos = @position @orig_vel = @velocity @orig_acc = @acceleration end
def tick! @velocity += @acceleration @position += @velocity end
def reset! @position = @orig_pos @velocity = @orig_vel @acceleration = @orig_acc end
def distance @position.sum(&:abs) endend
PARTICLES = ARGF.readlines.map { |l| Particle.new(*l.scan(/-?\d+/).map(&:to_i).each_slice(3).map { |s| Vector[*s] }) }
def tick! PARTICLES.each(&:tick!)end
def min_index PARTICLES.each_with_index.min_by { |p, _| p.distance }[1]end
# 1000 iterations seem to be enough# TODO: Find a proper solutionITERATIONS = 1000
ITERATIONS.times { tick! }
puts min_index
PARTICLES.each(&:reset!)
def clean! counts = PARTICLES.map(&:position).tally PARTICLES.reject! { |p| counts[p.position] > 1 }end
ITERATIONS.times do tick! clean!end
puts PARTICLES.size