Created
December 13, 2021 18:15
-
-
Save sarahzrf/eca9ec194bffcafe9900fcc7824ee2d6 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 = dots.max {|x, y| x} | |
height = dots.max {|x, y| y} | |
height.times do |y| | |
width.times do |x| | |
print(if dots.include? [x, y] then '#' else '.') | |
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