-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.rb
75 lines (57 loc) · 1.44 KB
/
day17.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
72
73
74
75
file = File.open('input17.txt')
WIDTH = 7
FALL_HEIGHT = 4
ROCKS = 2022
moves = file.read
patterns = [
# horizontal bar
[{y: 0, h: 1}, {y: 0, h: 1}, {y: 0, h: 1}, {y: 0, h: 1}],
# plus sign
[{y: 1, h: 1}, {y: 0, h: 3}, {y: 1, h: 1}],
# inverted L
[{y: 0, h: 1}, {y: 0, h: 1}, {y: 0, h: 3}],
# vertical bar
[{y: 0, h: 4}],
# square
[{y: 0, h: 2}, {y: 0, h: 2}]
]
pattern_index = 0
input_index = 0
height_map = Array.new(WIDTH, 0)
ROCKS.times do |i|
current_height = height_map.max
rock = patterns[i % patterns.length].map(&:clone)
rock.each { |r| r[:y] += current_height + 4 }
hit_bottom = false
offset = 2
loop do
input = moves[input_index]
input_index = (input_index + 1) % moves.length
new_rock = rock.clone.map(&:clone)
new_offset = offset
case input
when '>'
new_offset += 1 if offset < WIDTH - rock.length
when '<'
new_offset -= 1 if offset > 0
end
hit_wall = rock.map.with_index { |r, j| r[:y] <= height_map[j + new_offset] }.any?
offset = new_offset unless hit_wall
rock.each_index do |j|
if rock[j][:y] - 1 <= height_map[j + offset]
hit_bottom = true
break
end
new_rock[j][:y] -= 1
end
break if hit_bottom
rock = new_rock
end
rock.each_index do |j|
height_map[j + offset] = rock[j][:y] + rock[j][:h] - 1
end
pattern_index = (pattern_index + 1) % patterns.length
end
# < 3080
puts height_map.max
file.close