Last active
December 5, 2020 06:31
-
-
Save SleeplessByte/e0bf49737705bfa3366128c48c9edfef to your computer and use it in GitHub Desktop.
Advent of Code 2020: Day 3 - Toboggan Trajectory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
..##....... | |
#...#...#.. | |
.#....#..#. | |
..#.#...#.# | |
.#...##..#. | |
..#.##..... | |
.#.#.#....# | |
.#........# | |
#.##...#... | |
#...##....# | |
.#..#...#.# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
....#..#.................#..#.. | |
#..#.#.#..#.###.#..#...#..#.... | |
.#....#......#.#.#..##...#...#. | |
.............#.#..#........#.#. | |
............##.#..#...##.###... | |
.....#..#......#......##....... | |
........##........#...........# | |
..................#..#......... | |
......#..#...#..#......###..#.. | |
.#....#...........#.....#.##... | |
..#..#.#........###..#....#...# | |
..#..##..#..#....#..#......#... | |
......#....................#... | |
.........#..#.................. | |
..#.#.....#......#.#....#...#.. | |
..#..........##.......##.##.... | |
#.......#.##.....#...#....#.... | |
####..............###.#....#... | |
....#........#.#..###..#...#..# | |
#.#......#...#.##....#.....#... | |
.......#......#.....#........#. | |
.##.........#...#.........#.... | |
............#....#.#........#.. | |
#..##..#....#...#.#....#....#.. | |
.....#....##..#................ | |
.#...........#....#..#.....#... | |
......#.#...#..###............. | |
#...#...........#..###.#..#..#. | |
...................#..#....#..# | |
....#...#...#.#.....#....#.##.. | |
.......#.......#.#.........#... | |
#..#.......#...#..#.#......#... | |
..##...........#....#..#....... | |
.#...............#...##........ | |
.....#..........#............#. | |
..#.......#.#.#...........#..#. | |
........#..#.#.#........#.#.##. | |
.###..............#.#.......... | |
.#....###.....#......#....#.... | |
............#.#......#..#....#. | |
.#.#.............#........##... | |
.....#..###....##.....#....#..# | |
.##....#...#.#.........#......# | |
....##.......#...............## | |
..........##.##.......#........ | |
......#....##.........##.#.#.#. | |
..............#....#..#......#. | |
......##.........#.........#... | |
.#...##...##....##..#..#.....#. | |
.#......#.###.#....#...#......# | |
.##.......................#..#. | |
....#......#.##..........#.###. | |
.#.....##..........#.#......... | |
....#.#.........#...#.......... | |
.#..##.#....##.......#.......#. | |
.........#.......#............# | |
###..........#.....##.#....#... | |
.......###.#....#........#...## | |
..#..#....#.......#.#.......... | |
.#..#..........#......#...#.... | |
.....###.#..#.....#...#..#..... | |
........#.#.#..........#.#..#.. | |
........#...##................. | |
...#.............#.#..#......## | |
......#......##......#...#.#..# | |
.#..#...##..........#...##..... | |
..#.#....####..#...#....##....# | |
.#..##.........#..##......#.... | |
#....#.#.........#............. | |
....###..............#....#.... | |
....#..#..#...###..#.#..#.#.... | |
....#.....#...........###..#... | |
....#.#.....#...#.....#.......# | |
..#.......#..................#. | |
#...................##......... | |
....#.#.#.#.#.....#.....#...... | |
...............##..#..##.#...## | |
..#.....#.....##.........#..#.. | |
...#...#.....#..##..##....#..## | |
..#.................#....#...#. | |
#....##...............##..#.... | |
..#.....#.....##.........##...# | |
..#.###..............#...#..... | |
.......................#....... | |
#...#..#.....##...#...#........ | |
..........#......#.###....#...# | |
..#.....#.##.#..#.#.......#.... | |
#.##...#............#.......... | |
#........#.#.#..#...#..#.....## | |
#............#.#............... | |
.#..#.......#.#.....#.#......## | |
.#.#....#........#..##......... | |
..#....#......#.#...##...#..... | |
##.....#......................# | |
...#.......#..##.....#........# | |
......##..#...............##... | |
.....#...#......##....#.#...... | |
.#...#....#.#.#........#...#.#. | |
.......#...#...##...#.......... | |
.##..#..##........##....###.#.. | |
..##........#........##........ | |
............#....#......#...... | |
.......#...........#.......#... | |
#.#......##.#...#....#.#.....#. | |
..#.#.#......##........#....#.. | |
#.#.####.#..#..........#....... | |
......................#.#...... | |
...#.......#.....#......#..#.#. | |
...#....#....##..........#..##. | |
......#......#....#.........#.. | |
.........#....#...#.#.........# | |
.....#...##.#.#.#......#.....#. | |
........#...#......#.#....#.... | |
.....#.........#.............#. | |
.#...........#.#....##.......#. | |
.#..#......#....#....#....#.... | |
#.......#.#.#.#..#.......#..... | |
..#...#...#......#............. | |
.....#.......#..#.........##..# | |
#..##...........#.#.........#.. | |
#..#..........#....#......#...# | |
#...#............#....##..#.##. | |
....#.#..#....#.........##..##. | |
.........#..........#.......##. | |
#...#........................#. | |
....#....#..#...........##...#. | |
.....###.###.#....#.....#.....# | |
.#..###..#...##..........#..... | |
#..#.....#...#........#........ | |
.#..##..#......#.....#......##. | |
#.....##........#.#..#....#.#.. | |
.#....#...#..#...............#. | |
..........#.#........#.....#... | |
..#.#.....#....#........#.###.. | |
...#..#...#.##.....#..........# | |
..#......##.................... | |
.....#...#....#..#....#.......# | |
......#............#....#...#.. | |
.#..#....#.....#........##..... | |
...#..#.......#...............# | |
##.#..#...............#.#..##.. | |
..#.........#.####.####........ | |
.........#...#.#........#..###. | |
..###.....#.........##........# | |
#..##.....##.#..........#....## | |
.#..#....###..#.....##..#...... | |
#...#..#........#.............# | |
#.#....#........#.........#.### | |
.....#....#.###.......#........ | |
...........#............#..#... | |
..........#.#..##......###....# | |
..##....#...........#....#....# | |
..#...##.#.......#.##.......#.. | |
.......#......#..........#....# | |
.........#..#..............#### | |
#.#...#...#......#...#..#...#.# | |
.#.#...#.....#.......#.#..##.#. | |
......##..##...#............... | |
....#....#...##.......#.#...... | |
.............................#. | |
..##...#.......#......#.......# | |
.#.##.##....#......##.......#.. | |
.......##..#....#.##.#.#....... | |
....#.............#......#..#.. | |
...#.........#....#..#.....#.#. | |
......#......#......#.........# | |
........#..#.#.....#.....#...#. | |
.#.......#.........#...#....... | |
#.#.##.....#...#............... | |
..#.......##.....#............# | |
.................##.#..#.....## | |
........#.###....#.......##.#.. | |
....#.#..#..#.......#.#....#..# | |
.#..#..#..#...##....#..#.....#. | |
...#..#...#..........#........# | |
...........##....#...##.#...### | |
.##..#.......##.....##....#.... | |
...#.#..#..#..##..#.....##..... | |
.#.....#..........#...#........ | |
.....#..#.#..#................. | |
..#.......#..#.....##.......#.. | |
............##.##.....#...#.... | |
#......#......................# | |
...#..........#...#...#..#..... | |
......##..............##.##.... | |
................##......#.###.. | |
.###...#..........#...........# | |
#....#...#.........##......#... | |
.............#...............#. | |
.###.....#......#...#.......#.. | |
......##..#.#.#.....#........#. | |
..#.#..........#.#......##..... | |
.#.#...#.#.....#.#..#.....#.... | |
.......#....#.#....##.......... | |
#.........#........####.#...... | |
...#..#.....#..............#.#. | |
...#..........................# | |
..##....#..#..........#....#... | |
..##.##.#.#.#......#..#........ | |
...#..#.#.#..#........#.......# | |
.....#.....#..#...#........##.. | |
..#..#...........#...##..##..#. | |
#.....#.....#..#..##........#.. | |
...#..............#......#..#.. | |
...#.#...........###...#.#..... | |
..........................#.#.. | |
....#........#..#..........#.#. | |
.#..#..........#.#..........#.. | |
....#.........#......#.#....#.. | |
..#........#.................#. | |
...#......#............#....... | |
...#.#...#..##....#........#... | |
......##.#....#.#......#....... | |
........#..........#........#.. | |
###..#.....#...#......#...###.. | |
..##...#..###............#...#. | |
#.......#..#..#............##.. | |
#........####......#........... | |
#..#..#...........#.......####. | |
......#..##................#... | |
.....#..##......#.#...#..#..... | |
......#.....##.....#.###....... | |
.#.....#.........#.......#.#..# | |
.##...........###...#....#...#. | |
.#....#..#....#.##...#......... | |
.................##............ | |
......#......#.............#... | |
.........##.#........#....#...# | |
..##....#.......#....##.#...... | |
.......#.#.#.....##..#.#....... | |
......#.#.#.#......#...#....... | |
....##...#.....#..#......#..... | |
..............#......#.##...... | |
#.##..###........#.##........## | |
#..#.........#.#......#.#...... | |
.#..###.......#................ | |
..............#...#..##.#.#.... | |
.....#..#........#...##.#...#.. | |
.#...##.....#........#..###.#.. | |
....#.....#...#........#....... | |
....#.##.....#....#............ | |
#.#..#....#....#............#.. | |
....#....#...#.#...#...######.. | |
.##.........#..#.....#.....#... | |
..##...............#........... | |
........##...#........#..#..... | |
...#................###.##..##. | |
.#..#..#..#...#.............#.. | |
#.....#..##.#....#.#......#..#. | |
...#...#...#.....#...#...#..... | |
..##.###..#..#...##...........# | |
.#.......##........#.#..#..#... | |
..#.......#.###................ | |
..#...#........##...#..#......# | |
...#...#............#.#......#. | |
..#...#.....#.#.#.#........#... | |
.#........#......##....##...#.. | |
...#..##....#.........#....#.#. | |
....#........##...............# | |
.###.....#...#..#.#.....#.....# | |
..#...#..................#...## | |
#..#....###.................... | |
...........#...#...........#... | |
........#.....#................ | |
#........#...........#........# | |
........#..#....#...#....#..#.. | |
#.#....#......#.......##....... | |
.#.....##..#...#......#.#.#.... | |
..#..##.#.....#...#.#......#.#. | |
.##....#...#....#......##...... | |
.#..#..#...##.##..#.#..#....... | |
.....#.#...........#.#........# | |
.#...#....#..................#. | |
...........#...#.........#..... | |
.#..###........##......#..#...# | |
.............###..##.....#.#..# | |
.#..#..........#......#........ | |
..#........#.#...#.......##.#.. | |
....#...........##......#...#.. | |
...........#....#.....#...#.#.. | |
...#...........#.....#.#..#..#. | |
......#.......#................ | |
##.......#.....#............#.# | |
.##.....#.#.#.................. | |
..........#.....##..#.#.#...... | |
.###.#.....##...#..#.#........# | |
#....#........##..#..#......... | |
.............#.........##..#... | |
......##.#...#.#.#....##....... | |
.#.......###.#.###..##........# | |
..##.....#..#.............#..#. | |
.#...#......#.#.............#.. | |
..##.#...#.........##....#...#. | |
...........#......#.#.......... | |
.....#..#...##.....#....#..#... | |
#...................##...#..... | |
..#.................#.....#.... | |
..............#..#.#...###..... | |
.......#........#...#.....#.... | |
.............#.......#...##..#. | |
.#...#..#...#..#.....#......#.# | |
....#..#..#...#...........#.... | |
.....#..#......##.##....#..#... | |
...#......#..#.#...#.....#..... | |
.......##..#.#.......#..#....## | |
#.#..#....##.##.#.#..........#. | |
..........#..........#....##.#. | |
....#.................#...#..#. | |
...#.....#..#...#.#...#..#..... | |
....#.#..###....#.............# | |
#....#..#.#..........#..#..#... | |
...#..#......#...#...#...#...#. | |
##....#.......#..........#..... | |
#......#.........#...#......... | |
##...##.#....#....#..#..#.#.... | |
....#..#.....#.##.#.......#.#.. | |
..##....##....#...#..####...#.# | |
..##..........#.............#.. | |
..#......#..............#...... | |
...#......#..#.#...#.......#... | |
.#............#....#...##.##..# | |
..##..........#...........#..#. | |
..#..##..#....#..#.#..#..#..#.. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment