aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2022-12-15 14:09:58 +0000
committerNat Lasseter <user@4574.co.uk>2022-12-15 14:09:58 +0000
commit78dcf1d32fe821c9043148df2de888c415314e17 (patch)
treee61c8423560fafaadac4d68c3661c7fe49e192a5
parent582367707f370678073931c67349474b8b1d5fdd (diff)
Day 14 part 2; hashes are great.
-rw-r--r--day14/part2.rb43
1 files changed, 22 insertions, 21 deletions
diff --git a/day14/part2.rb b/day14/part2.rb
index dd58cb4..cb6f059 100644
--- a/day14/part2.rb
+++ b/day14/part2.rb
@@ -1,4 +1,4 @@
-solids = []
+solids = Hash.new { |h, k| h[k] = Array.new }
$stdin.readlines.map { |line|
line.strip.split(' -> ').map {
@@ -15,7 +15,7 @@ $stdin.readlines.map { |line|
y2 = y1
y1 = t
end
- (y1..y2).each { |y| solids << [x, y] }
+ (y1..y2).each { |y| solids[y] << x }
else
x1 = path[i][0]
x2 = path[i+1][0]
@@ -25,41 +25,42 @@ $stdin.readlines.map { |line|
x1 = t
end
y = path[i][1]
- (x1..x2).each { |x| solids << [x, y] }
+ (x1..x2).each { |x| solids[y] << x }
end
}
}
-solids.uniq!
+solids.keys.each { |k| solids[k].uniq! }
-floor = solids.map{ |_, y| y }.max + 1
+floor = solids.keys.flatten.max + 1
sands = 0
loop do
- puts sands
- sand = [500, 0]
+ sandx = 500
+ sandy = 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
+ break if sandy == floor
+
+ r = solids[sandy + 1]
+ if r.include?(sandx)
+ if r.include?(sandx - 1)
+ if r.include?(sandx + 1)
+ break
else
- sand = [sand[0] - 1, sand[1] + 1]
+ sandx += 1
end
else
- sand = [sand[0], sand[1] + 1]
+ sandx -= 1
end
end
+ sandy += 1
end
- solids << sand
+
+ solids[sandy] << sandx
sands += 1
- break if sand == [500, 0]
+
+ break if sandx == 500 && sandy == 0
end
puts sands