-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.rb
38 lines (31 loc) · 952 Bytes
/
day03.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
def readArea(lines)
area = []
for i in 0..lines.length-1
line = lines[i]
area[i] = line.chars[0..line.length-2]
end
return area
end
file = File.open("input/day03.txt", "r")
area = readArea(file.readlines)
def treeCountForSlope(area, right, down)
currentPosition = [0, 0] # [y, x]
treeCount = 0
while currentPosition[0] < area.length
tile = area[currentPosition[0]][currentPosition[1]]
# puts "There is #{tile} at position #{currentPosition}"
if tile == "#"
treeCount += 1
end
currentPosition = [currentPosition[0] + down, (currentPosition[1] + right) % (area[0].length-1)]
end
return treeCount
end
treeCounts = []
for slope in [[1, 1], [3, 1], [5, 1], [7, 1], [1, 2]]
right = slope[0]
down = slope[1]
treeCounts.push(treeCountForSlope(area, right, down))
end
puts treeCounts[1] # part 1
puts (treeCounts.reduce { |p, n| p * n } )