Code for the Advent of Code event
aoc
advent-of-code
1#!/usr/bin/env ruby
2# frozen_string_literal: true
3
4input, instructions = ARGF.read.split("\n\n")
5grid = input.scan(/\d+/).map(&:to_i).each_slice(2).to_set
6instructions = instructions.scan(/(x|y)=(\d+)/).map { [_1 == 'x' ? 0 : 1, _2.to_i] }
7
8def vis(grid)
9 max_x = grid.map(&:first).max
10 max_y = grid.map { _1[1] }.max
11 puts (0..max_y).map { |y| (0..max_x).map { grid.include?([_1, y]) ? '██' : ' ' }.join }
12end
13
14def fold(grid, axis, pos)
15 grid.to_set { |k| k.dup.tap { _1[axis] = pos - (k[axis] - pos).abs } }
16end
17
18puts fold(grid, *instructions[0]).size
19vis(instructions.reduce(grid) { |a, e| fold(a, *e) })