diff options
author | Nat Lasseter <user@4574.co.uk> | 2022-12-14 20:59:36 +0000 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2022-12-14 20:59:36 +0000 |
commit | 582367707f370678073931c67349474b8b1d5fdd (patch) | |
tree | 8da066f9b9837173c0313b7bba4752d82a6acded /day14/part2.rb | |
parent | 278c2d2acd0b3be7c887f03d89ea7f52a697a204 (diff) |
Day14, part2 exceptionally slow.
Diffstat (limited to 'day14/part2.rb')
-rw-r--r-- | day14/part2.rb | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/day14/part2.rb b/day14/part2.rb new file mode 100644 index 0000000..dd58cb4 --- /dev/null +++ b/day14/part2.rb @@ -0,0 +1,65 @@ +solids = [] + +$stdin.readlines.map { |line| + line.strip.split(' -> ').map { + |coord| coord.split(?,).map(&:to_i) + } +}.each { |path| + (path.length - 1).times { |i| + if path[i][0] == path[i+1][0] + x = path[i][0] + y1 = path[i][1] + y2 = path[i+1][1] + if y1 > y2 + t = y2 + y2 = y1 + y1 = t + end + (y1..y2).each { |y| solids << [x, y] } + else + x1 = path[i][0] + x2 = path[i+1][0] + if x1 > x2 + t = x2 + x2 = x1 + x1 = t + end + y = path[i][1] + (x1..x2).each { |x| solids << [x, y] } + end + } +} + +solids.uniq! + +floor = solids.map{ |_, y| y }.max + 1 + +sands = 0 +loop do + puts sands + sand = [500, 0] + loop do + if sand[1] == floor + break + else + if solids.include?([sand[0], sand[1] + 1]) + if solids.include?([sand[0] - 1, sand[1] + 1]) + if solids.include?([sand[0] + 1, sand[1] + 1]) + break + else + sand = [sand[0] + 1, sand[1] + 1] + end + else + sand = [sand[0] - 1, sand[1] + 1] + end + else + sand = [sand[0], sand[1] + 1] + end + end + end + solids << sand + sands += 1 + break if sand == [500, 0] +end + +puts sands |