Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
1.4 kB · 77 lines
Ruby
at rust
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778#!/usr/bin/env ruby# frozen_string_literal: true
require_relative '../../../lib/aoc/intcode/cpu'
require 'matrix'
class Vector def x; self[0]; end def y; self[1]; endend
PATH = ARGV.first || 'input'
BLACK = 0WHITE = 1LEFT = 0RIGHT = 1
DIRS = { LEFT => { Vector[0, 0] => Vector[-1, 0], Vector[1, 0] => Vector[0, -1], Vector[0, 1] => Vector[1, 0], Vector[-1, 0] => Vector[0, 1], Vector[0, -1] => Vector[-1, 0] }, RIGHT => { Vector[0, 0] => Vector[1, 0], Vector[1, 0] => Vector[0, 1], Vector[0, 1] => Vector[-1, 0], Vector[-1, 0] => Vector[0, -1], Vector[0, -1] => Vector[1, 0] }}.freeze
def run(_start_c) colors = { Vector[0, 0] => start_color }
cpu = AoC::Intcode::CPU.new.load!(PATH).print_output!(false)
pos = Vector[0, 0] vel = Vector[0, 0]
until cpu.halted? cpu.input!(colors[pos]) cpu.run! c = cpu.output[-2] dir = cpu.output[-1] colors[pos] = c vel = DIRS[dir][vel] pos += vel end
colorsend
puts "Part 1: #{run(BLACK).size}"
colors = run WHITE
min_x = colors.keys.min_by(&:x).xmax_x = colors.keys.max_by(&:x).xmin_y = colors.keys.min_by(&:y).ymax_y = colors.keys.max_by(&:y).y
puts "Grid for part 2 goes from (#{min_x}, #{min_y}) to (#{max_x}, #{max_y})"
puts 'Part 2:'(min_y..max_y).each do |y| (min_x..max_x).each do |x| print colors[Vector[x, y]] == WHITE ? '██' : ' ' end putsend