-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.rb
More file actions
130 lines (115 loc) · 3.86 KB
/
day8.rb
File metadata and controls
130 lines (115 loc) · 3.86 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class Forest
def initialize(input)
@forest = input_file(input)
@viewable_trees = find_viewable_trees(@forest)
end
attr_reader :viewable_trees
def input_file(input)
forest = Array.new
i = 0
File.foreach(input) do |row|
row = row.gsub("\n", "")
forest.push(row.split(//).map{|char| char.to_i})
i += 1
end
return forest
end
def find_viewable_trees(forest)
viewable_trees = Array.new
# x, y = 0, 0
forest.each_index do |row|
forest[row].each_index do |column|
puts "[#{row}, #{column}] = #{forest[row][column]}"
viewable = viewable_tree(forest, row, column)
# puts viewable
if viewable
viewable_trees.push("#{row}#{column}")
end
end
end
return viewable_trees
end
def viewable_tree(forest, row, column)
east = viewable_from_east?(forest, row, column)
west = viewable_from_west?(forest, row, column)
north = viewable_from_north?(forest, row, column)
south = viewable_from_south?(forest, row, column)
p "[#{row},#{column}}] north = #{north}"
p "[#{row},#{column}}] south = #{south}"
p "[#{row},#{column}}] east = #{east}"
p "[#{row},#{column}}] west = #{west}"
p "#{east || west || north || south}"
return east || west || north || south
end
def viewable_from_east?(forest, row, column)
#get height of tree
height = forest[row][column]
height_to_east = -1
p "[#{row}][#{column}] east -_---_-_-_-_-_-_-_"
for a in 0..column-1
p "forest[#{row}][#{a}] = #{forest[row][a]}"
if forest[row][a] > height_to_east #&& column != a
height_to_east = forest[row][a]
end
end
# p "forest[#{x}][#{y}]"
p height
p height_to_east
p height > height_to_east
return height > height_to_east
end
def viewable_from_west?(forest, row, column)
#get height of tree
height = forest[row][column]
height_to_west = -1
p "[#{row}][#{column}] west -_---_-_-_-_-_-_-_"
for a in column+1..forest[0].length-1
p "forest[#{row}][#{a}] = #{forest[row][a]}"
if forest[row][a] > height_to_west #&& a != forest[0].length
height_to_west = forest[row][a]
end
end
# p "forest[#{x}][#{y}]"
p height
p height_to_west
p height > height_to_west
return height > height_to_west
end
def viewable_from_north?(forest, row, column)
#get height of tree
height = forest[row][column]
height_to_north = -1
p "[#{row}][#{column}] north -_---_-_-_-_-_-_-_"
for a in 0..row-1
p "forest[#{a}][#{column}] = #{forest[a][column]}"
if forest[a][column] > height_to_north #&& a != forest.length
height_to_north = forest[a][column]
end
end
# p "forest[#{x}][#{y}]"
p height
p height_to_north
p height > height_to_north
return height > height_to_north
end
end
def viewable_from_south?(forest, row, column)
#get height of tree
height = forest[row][column]
height_to_south = -1
p "[#{row}][#{column}] south -_---_-_-_-_-_-_-_"
for a in row+1..forest.length-1
p "forest[#{a}][#{column}] = #{forest[a][column]}"
if forest[a][column] > height_to_south #&& a != forest.length
height_to_south = forest[a][column]
end
end
# p "forest[#{x}][#{y}]"
p height
p height_to_south
p height > height_to_south
return height > height_to_south
end
today = Forest.new("day8i.txt")
p today.viewable_trees
p today.viewable_trees.length