Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
617 B · 19 lines
Ruby
at rust
1234567891011121314151617181920#!/usr/bin/env ruby# frozen_string_literal: true
input, instructions = ARGF.read.split("\n\n")grid = input.scan(/\d+/).map(&:to_i).each_slice(2).to_setinstructions = instructions.scan(/(x|y)=(\d+)/).map { [_1 == 'x' ? 0 : 1, _2.to_i] }
def vis(grid) max_x = grid.map(&:first).max max_y = grid.map { _1[1] }.max puts (0..max_y).map { |y| (0..max_x).map { grid.include?([_1, y]) ? '██' : ' ' }.join }end
def fold(grid, axis, pos) grid.to_set { |k| k.dup.tap { _1[axis] = pos - (k[axis] - pos).abs } }end
puts fold(grid, *instructions[0]).sizevis(instructions.reduce(grid) { |a, e| fold(a, *e) })