Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created December 13, 2021 18:15
Show Gist options
  • Save sarahzrf/eca9ec194bffcafe9900fcc7824ee2d6 to your computer and use it in GitHub Desktop.
Save sarahzrf/eca9ec194bffcafe9900fcc7824ee2d6 to your computer and use it in GitHub Desktop.
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