-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_14_2.rb
71 lines (62 loc) · 1.24 KB
/
day_14_2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "set"
def print_grid(bottom)
(0..bottom).each do |y|
print "#{y.to_s.rjust(3)} "
(450..600).each do |x|
print ($image.include? [x,y]) ? "x" : "."
end
puts
end
end
#print_grid
def tokenize(t0,t1)
x = []
y = []
r = []
x[0],y[0] = t0
x[1],y[1] = t1
(x.min .. x.max).each { |m|
r << [m,y.min]
} if y.min == y.max
(y.min .. y.max).each { |m|
r << [x.min,m]
} if x.min == x.max
r
end
data = '''498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9'''
#data = File.open("inp_14").read()
$image = Set.new
data.split("\n").each do |l|
tokens = l.split(" -> ").map{|i| i.split(",").map{|o| o.to_i }}
(1..tokens.size-1).each do |i|
$image += tokenize(tokens[i-1],tokens[i])
end
end
bottom = $image.map { |i| i[1] }.max+2
grains = 0
loop do
s = [500,0]
grains += 1
breaker = false
loop do
unless $image.include? [s[0],s[1]+1] or s[1]+1 >= bottom
s = [s[0],s[1]+1]
break if s[1]+1 == bottom
next
end
unless $image.include? [s[0]-1,s[1]+1] or s[1]+1 >= bottom
s = [s[0]-1,s[1]+1]
next
end
unless $image.include? [s[0]+1,s[1]+1] or s[1]+1 >= bottom
s = [s[0]+1,s[1]+1]
next
end
breaker = true if s == [500,0]
break
end
$image << s
break if breaker
end
puts "part 2: #{grains}"