Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
878 B · 43 lines
Ruby
at rust
1234567891011121314151617181920212223242526272829303132333435363738394041424344#!/usr/bin/env ruby# frozen_string_literal: true
DEFAULT_WIDTH = 25DEFAULT_HEIGHT = 6
PATH = ARGV[0] || 'input'WIDTH = ARGV[1]&.to_i || DEFAULT_WIDTHHEIGHT = ARGV[2]&.to_i || DEFAULT_HEIGHT
LAYER_SIZE = WIDTH * HEIGHT
data = File.read(PATH).striplayers = data.scan(%r{.{#{LAYER_SIZE}}}).map { |s| s.chars.map(&:to_i) }
part1_slice = layers.min_by { |s| s.count { |d| d == 0 } }part1_ones = part1_slice.count { |d| d == 1 }part1_twos = part1_slice.count { |d| d == 2 }part1 = part1_ones * part1_twos
part2 = [2] * LAYER_SIZE
layers.each do |layer| layer.each_with_index do |d, i| part2[i] = d if part2[i] == 2 && d != 2 endend
puts "Part 1: #{part1}"
puts 'Part 2:'
def print_pixel(pixel) print pixel == 0 ? ' ' : '█'end
(0...HEIGHT).each do |y| (0...WIDTH).each do |x| pixel = part2[(y * WIDTH) + x] print_pixel pixel end putsend