You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2021/ruby/day_13.rb

19 lines
539 B

dots, folds = ARGF.read.split("\n\n")
dots = dots.scan(/(\d+),(\d+)/).map { _1.map(&:to_i) }
folds = folds.scan(/(x|y)=(\d+)/).map { [_1, _2.to_i] }
dirs = { ?x => 0, ?y => 1 }
folds.each do |dir, axis|
index = dirs.fetch(dir)
dots.select { _1[index] > axis }.each do |dot|
dot[index] = axis - (dot[index] - axis)
end
dots.uniq!
# p dots.size or exit
end
xx = Range.new(*dots.map(&:first).minmax)
yy = Range.new(*dots.map(&:last).minmax)
puts yy.map {|y| xx.map {|x| dots.include?([x,y]) ? "" : " " }.join }.join("\n")