Created
December 6, 2021 03:38
-
-
Save sarahzrf/f2e0e1840f85a9c440880706ea8a2dca 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
def range(i, j) | |
return (i..j).each if i <= j | |
Enumerator.new do |y| | |
loop do | |
y << i | |
break if i == j | |
i -= 1 | |
end | |
end | |
end | |
lines = $<.map do |l| | |
l =~ /(\d+),(\d+) -> (\d+),(\d+)/ | |
[[$1.to_i, $2.to_i], [$3.to_i, $4.to_i]] | |
end | |
map = Hash.new(0) | |
lines.each do |(x1, y1), (x2, y2)| | |
if x1 == x2 | |
x = x1 | |
range(y1, y2).each do |y| | |
map[[x, y]] += 1 | |
end | |
elsif y1 == y2 | |
y = y1 | |
range(x1, x2).each do |x| | |
map[[x, y]] += 1 | |
end | |
else | |
range(x1, x2).zip(range(y1, y2)).each do |x, y| | |
map[[x, y]] += 1 | |
end | |
end | |
end | |
danger = map.count {|k, v| v > 1} | |
print danger | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment