Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created December 13, 2021 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarahzrf/3c34e578b837bafbd420d91008394a74 to your computer and use it in GitHub Desktop.
Save sarahzrf/3c34e578b837bafbd420d91008394a74 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, 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