Skip to content

Commit 2b41ef1

Browse files
committed
Cleanup
1 parent 4451297 commit 2b41ef1

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

aoc2024/ruby/10.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
DIRS = [ [0,-1], [1,0], [0,1], [-1,0] ]
1+
DIRS = [[0,-1], [1,0], [0,1], [-1,0]]
22

3-
def in_bounds(grid, x, y) = y >= 0 && y < grid.length && x >= 0 && x < grid[y].length
3+
def in_bounds(grid, x, y) =
4+
y >= 0 &&
5+
y < grid.length &&
6+
x >= 0 &&
7+
x < grid[y].length
48

5-
def find_trails(grid, v, x, y)
9+
def find_trails(grid, x, y, v=0, steps=[[x,y]])
610
return [] unless in_bounds(grid, x, y) && grid[y][x] == v
7-
return [[x,y]] if v == 9
11+
return [steps] if v == 9
812

9-
DIRS
13+
return DIRS
1014
.map { |dx,dy| [x+dx, y+dy] }
11-
.flat_map { |nx, ny| find_trails(grid, v+1, nx, ny) }
15+
.flat_map { |nx, ny| find_trails(grid, nx, ny, v+1, [*steps, [nx,ny]]) }
1216
end
1317

1418

1519
input = File
1620
.readlines('../input/10-sample.txt', chomp: true)
1721
.map { _1.chars.map &:to_i }
1822

19-
p input
20-
.each_with_index
21-
.sum { |row,y|
22-
row.each_index.sum { |x| find_trails(input, 0, x, y).size }
23-
}
23+
trails = input.flat_map.with_index { |row,y|
24+
row.each_index.flat_map { |x| find_trails(input, x, y) }
25+
}
26+
27+
p trails.group_by(&:first).sum { |_,trail| trail.map(&:last).uniq.size }
28+
p trails.size

0 commit comments

Comments
 (0)