Skip to content

Instantly share code, notes, and snippets.

@SleeplessByte
Last active December 5, 2020 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SleeplessByte/e0bf49737705bfa3366128c48c9edfef to your computer and use it in GitHub Desktop.
Save SleeplessByte/e0bf49737705bfa3366128c48c9edfef to your computer and use it in GitHub Desktop.
Advent of Code 2020: Day 3 - Toboggan Trajectory
require 'benchmark'
class TreeGrid
def self.from(file)
rows = File.readlines(file)
TreeGrid.new(rows)
end
def initialize(rows)
self.rows = rows
self.width = rows.first.scan(/[\.\#]/).length
end
def at(y, x)
self.rows[y][x % width]
end
def tree?(y, x)
at(y, x) == '#'
end
def height
rows.length
end
private
attr_accessor :width, :rows
end
grid = TreeGrid.from('input.txt')
def count_trees(grid, slope_x, slope_y)
trees = 0
x = 0
y = 0
while y < grid.height
trees += 1 if grid.tree?(y, x)
y += slope_y
x += slope_x
end
trees
end
Benchmark.bmbm do |b|
b.report do
puts [
count_trees(grid, 1, 1),
count_trees(grid, 3, 1),
count_trees(grid, 5, 1),
count_trees(grid, 7, 1),
count_trees(grid, 1, 2)
].inject(&:*)
end
end
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
....#..#.................#..#..
#..#.#.#..#.###.#..#...#..#....
.#....#......#.#.#..##...#...#.
.............#.#..#........#.#.
............##.#..#...##.###...
.....#..#......#......##.......
........##........#...........#
..................#..#.........
......#..#...#..#......###..#..
.#....#...........#.....#.##...
..#..#.#........###..#....#...#
..#..##..#..#....#..#......#...
......#....................#...
.........#..#..................
..#.#.....#......#.#....#...#..
..#..........##.......##.##....
#.......#.##.....#...#....#....
####..............###.#....#...
....#........#.#..###..#...#..#
#.#......#...#.##....#.....#...
.......#......#.....#........#.
.##.........#...#.........#....
............#....#.#........#..
#..##..#....#...#.#....#....#..
.....#....##..#................
.#...........#....#..#.....#...
......#.#...#..###.............
#...#...........#..###.#..#..#.
...................#..#....#..#
....#...#...#.#.....#....#.##..
.......#.......#.#.........#...
#..#.......#...#..#.#......#...
..##...........#....#..#.......
.#...............#...##........
.....#..........#............#.
..#.......#.#.#...........#..#.
........#..#.#.#........#.#.##.
.###..............#.#..........
.#....###.....#......#....#....
............#.#......#..#....#.
.#.#.............#........##...
.....#..###....##.....#....#..#
.##....#...#.#.........#......#
....##.......#...............##
..........##.##.......#........
......#....##.........##.#.#.#.
..............#....#..#......#.
......##.........#.........#...
.#...##...##....##..#..#.....#.
.#......#.###.#....#...#......#
.##.......................#..#.
....#......#.##..........#.###.
.#.....##..........#.#.........
....#.#.........#...#..........
.#..##.#....##.......#.......#.
.........#.......#............#
###..........#.....##.#....#...
.......###.#....#........#...##
..#..#....#.......#.#..........
.#..#..........#......#...#....
.....###.#..#.....#...#..#.....
........#.#.#..........#.#..#..
........#...##.................
...#.............#.#..#......##
......#......##......#...#.#..#
.#..#...##..........#...##.....
..#.#....####..#...#....##....#
.#..##.........#..##......#....
#....#.#.........#.............
....###..............#....#....
....#..#..#...###..#.#..#.#....
....#.....#...........###..#...
....#.#.....#...#.....#.......#
..#.......#..................#.
#...................##.........
....#.#.#.#.#.....#.....#......
...............##..#..##.#...##
..#.....#.....##.........#..#..
...#...#.....#..##..##....#..##
..#.................#....#...#.
#....##...............##..#....
..#.....#.....##.........##...#
..#.###..............#...#.....
.......................#.......
#...#..#.....##...#...#........
..........#......#.###....#...#
..#.....#.##.#..#.#.......#....
#.##...#............#..........
#........#.#.#..#...#..#.....##
#............#.#...............
.#..#.......#.#.....#.#......##
.#.#....#........#..##.........
..#....#......#.#...##...#.....
##.....#......................#
...#.......#..##.....#........#
......##..#...............##...
.....#...#......##....#.#......
.#...#....#.#.#........#...#.#.
.......#...#...##...#..........
.##..#..##........##....###.#..
..##........#........##........
............#....#......#......
.......#...........#.......#...
#.#......##.#...#....#.#.....#.
..#.#.#......##........#....#..
#.#.####.#..#..........#.......
......................#.#......
...#.......#.....#......#..#.#.
...#....#....##..........#..##.
......#......#....#.........#..
.........#....#...#.#.........#
.....#...##.#.#.#......#.....#.
........#...#......#.#....#....
.....#.........#.............#.
.#...........#.#....##.......#.
.#..#......#....#....#....#....
#.......#.#.#.#..#.......#.....
..#...#...#......#.............
.....#.......#..#.........##..#
#..##...........#.#.........#..
#..#..........#....#......#...#
#...#............#....##..#.##.
....#.#..#....#.........##..##.
.........#..........#.......##.
#...#........................#.
....#....#..#...........##...#.
.....###.###.#....#.....#.....#
.#..###..#...##..........#.....
#..#.....#...#........#........
.#..##..#......#.....#......##.
#.....##........#.#..#....#.#..
.#....#...#..#...............#.
..........#.#........#.....#...
..#.#.....#....#........#.###..
...#..#...#.##.....#..........#
..#......##....................
.....#...#....#..#....#.......#
......#............#....#...#..
.#..#....#.....#........##.....
...#..#.......#...............#
##.#..#...............#.#..##..
..#.........#.####.####........
.........#...#.#........#..###.
..###.....#.........##........#
#..##.....##.#..........#....##
.#..#....###..#.....##..#......
#...#..#........#.............#
#.#....#........#.........#.###
.....#....#.###.......#........
...........#............#..#...
..........#.#..##......###....#
..##....#...........#....#....#
..#...##.#.......#.##.......#..
.......#......#..........#....#
.........#..#..............####
#.#...#...#......#...#..#...#.#
.#.#...#.....#.......#.#..##.#.
......##..##...#...............
....#....#...##.......#.#......
.............................#.
..##...#.......#......#.......#
.#.##.##....#......##.......#..
.......##..#....#.##.#.#.......
....#.............#......#..#..
...#.........#....#..#.....#.#.
......#......#......#.........#
........#..#.#.....#.....#...#.
.#.......#.........#...#.......
#.#.##.....#...#...............
..#.......##.....#............#
.................##.#..#.....##
........#.###....#.......##.#..
....#.#..#..#.......#.#....#..#
.#..#..#..#...##....#..#.....#.
...#..#...#..........#........#
...........##....#...##.#...###
.##..#.......##.....##....#....
...#.#..#..#..##..#.....##.....
.#.....#..........#...#........
.....#..#.#..#.................
..#.......#..#.....##.......#..
............##.##.....#...#....
#......#......................#
...#..........#...#...#..#.....
......##..............##.##....
................##......#.###..
.###...#..........#...........#
#....#...#.........##......#...
.............#...............#.
.###.....#......#...#.......#..
......##..#.#.#.....#........#.
..#.#..........#.#......##.....
.#.#...#.#.....#.#..#.....#....
.......#....#.#....##..........
#.........#........####.#......
...#..#.....#..............#.#.
...#..........................#
..##....#..#..........#....#...
..##.##.#.#.#......#..#........
...#..#.#.#..#........#.......#
.....#.....#..#...#........##..
..#..#...........#...##..##..#.
#.....#.....#..#..##........#..
...#..............#......#..#..
...#.#...........###...#.#.....
..........................#.#..
....#........#..#..........#.#.
.#..#..........#.#..........#..
....#.........#......#.#....#..
..#........#.................#.
...#......#............#.......
...#.#...#..##....#........#...
......##.#....#.#......#.......
........#..........#........#..
###..#.....#...#......#...###..
..##...#..###............#...#.
#.......#..#..#............##..
#........####......#...........
#..#..#...........#.......####.
......#..##................#...
.....#..##......#.#...#..#.....
......#.....##.....#.###.......
.#.....#.........#.......#.#..#
.##...........###...#....#...#.
.#....#..#....#.##...#.........
.................##............
......#......#.............#...
.........##.#........#....#...#
..##....#.......#....##.#......
.......#.#.#.....##..#.#.......
......#.#.#.#......#...#.......
....##...#.....#..#......#.....
..............#......#.##......
#.##..###........#.##........##
#..#.........#.#......#.#......
.#..###.......#................
..............#...#..##.#.#....
.....#..#........#...##.#...#..
.#...##.....#........#..###.#..
....#.....#...#........#.......
....#.##.....#....#............
#.#..#....#....#............#..
....#....#...#.#...#...######..
.##.........#..#.....#.....#...
..##...............#...........
........##...#........#..#.....
...#................###.##..##.
.#..#..#..#...#.............#..
#.....#..##.#....#.#......#..#.
...#...#...#.....#...#...#.....
..##.###..#..#...##...........#
.#.......##........#.#..#..#...
..#.......#.###................
..#...#........##...#..#......#
...#...#............#.#......#.
..#...#.....#.#.#.#........#...
.#........#......##....##...#..
...#..##....#.........#....#.#.
....#........##...............#
.###.....#...#..#.#.....#.....#
..#...#..................#...##
#..#....###....................
...........#...#...........#...
........#.....#................
#........#...........#........#
........#..#....#...#....#..#..
#.#....#......#.......##.......
.#.....##..#...#......#.#.#....
..#..##.#.....#...#.#......#.#.
.##....#...#....#......##......
.#..#..#...##.##..#.#..#.......
.....#.#...........#.#........#
.#...#....#..................#.
...........#...#.........#.....
.#..###........##......#..#...#
.............###..##.....#.#..#
.#..#..........#......#........
..#........#.#...#.......##.#..
....#...........##......#...#..
...........#....#.....#...#.#..
...#...........#.....#.#..#..#.
......#.......#................
##.......#.....#............#.#
.##.....#.#.#..................
..........#.....##..#.#.#......
.###.#.....##...#..#.#........#
#....#........##..#..#.........
.............#.........##..#...
......##.#...#.#.#....##.......
.#.......###.#.###..##........#
..##.....#..#.............#..#.
.#...#......#.#.............#..
..##.#...#.........##....#...#.
...........#......#.#..........
.....#..#...##.....#....#..#...
#...................##...#.....
..#.................#.....#....
..............#..#.#...###.....
.......#........#...#.....#....
.............#.......#...##..#.
.#...#..#...#..#.....#......#.#
....#..#..#...#...........#....
.....#..#......##.##....#..#...
...#......#..#.#...#.....#.....
.......##..#.#.......#..#....##
#.#..#....##.##.#.#..........#.
..........#..........#....##.#.
....#.................#...#..#.
...#.....#..#...#.#...#..#.....
....#.#..###....#.............#
#....#..#.#..........#..#..#...
...#..#......#...#...#...#...#.
##....#.......#..........#.....
#......#.........#...#.........
##...##.#....#....#..#..#.#....
....#..#.....#.##.#.......#.#..
..##....##....#...#..####...#.#
..##..........#.............#..
..#......#..............#......
...#......#..#.#...#.......#...
.#............#....#...##.##..#
..##..........#...........#..#.
..#..##..#....#..#.#..#..#..#..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment