Created
December 13, 2021 18:20
-
-
Save sarahzrf/3c34e578b837bafbd420d91008394a74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
def fold(line, val) | |
if val < line | |
val | |
elsif val > line | |
line - (val - line) | |
else | |
raise "dot on line!" | |
end | |
end | |
def print_dots(dots) | |
width, height = | |
dots.reduce([0, 0]) {|(px, py), (x, y)| [[px, x].max, [py, y].max]} | |
(0..height).each do |y| | |
(0..width).each do |x| | |
print(dots.include?([x, y]) ? '#' : '.') | |
end | |
puts | |
end | |
end | |
dots = Set.new | |
while gets =~ /(\d+),(\d+)/ | |
dots << [$1.to_i, $2.to_i] | |
end | |
while gets =~ /fold along ([xy])=(\d+)/ | |
line = $2.to_i | |
case $1 | |
when 'x' | |
dots.map! {|x, y| [fold(line, x), y]} | |
when 'y' | |
dots.map! {|x, y| [x, fold(line, y)]} | |
end | |
end | |
print_dots(dots) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment