day3: solve part 1 & 2

This commit is contained in:
Johannes Loher 2020-12-04 09:24:06 +01:00
parent 6c90570d52
commit 3dd39bac7a
4 changed files with 798 additions and 0 deletions

323
day3/part1/input Normal file
View file

@ -0,0 +1,323 @@
.......#..#....#...#...#......#
..##..#...##.###.#..#.....#.#..
#..#.#....#......#..#.........#
.#..##...#........#....#..#..#.
#.#.#....###...#........#.....#
.#...#.#.##.#.##...#.#.........
####......#.......###.##.#.....
..#...........#...#.#.#........
.#.......#....###.####..#......
...##........#....##.......##..
.###......##.#......##....#.#.#
........#.#......##...#......#.
#....##.#..#...#.......#.......
.#..##........##.........#....#
.#..#..#...#....#.#......#.#...
..#.#......##.#.......#....##..
......##......#.#..##.#..#...#.
.....##.......#.#....#.#.......
........#.....#.....#..###.#...
#........#..#.....#...#.#.#..#.
.#..#.....#...#........#.....#.
.#.#.....#.....#...#...........
.....#.#..#..#...#..#..#..##..#
##.#...#....#..#.##..#.....#.#.
#.......####......#..#.#....#..
......#.#...####.........#.#..#
.#.........#..#.#...#..........
...#####.#....#.#..#......#.#.#
##....#.###....##...##..#.....#
...........####.##.#....##.##..
#.#.#..........#.#..##.#.######
##...#..#...........###..#....#
.#.#.#...##..........##.#...#..
...#.#........#..##...#....#...
......#..#...#..##....#.......#
.#..#.......#..#......##....##.
.......#.......#........#..##..
...#...#...#.##......#.##.#....
.........#.........#.#.#.##....
..#...................#....#..#
.........#..#.....#.#...#....#.
#.#.#...#........#..###.#......
#.#.#.####......##...#...#....#
#...........##..#.#.#....#..#..
........#..#.#...........##.#.#
.#.........#...........#..#....
#............##.#..#....##...##
.#....##..#.#....#.......#..#..
..#.#...#.#......####.......#..
...#.#.......###......#.....#..
#......#.......#.#...#.#..##...
...#.....#...##.#.....#.#......
#.#.#............#..#......#..#
....#...#...##.##.##...##.#....
..##........#..#........#...##.
.......#..#...#.........#.....#
...........#.#......#...#......
...##..##..##..###..#..#..#..#.
#..##.......##..#....#....#.#..
#.#.##.#..##.....#....#.#......
....#..##......#.#..#....#....#
.#.#.........##...#......##.##.
##...........#..#.....#.###....
.#.###........#...#....##..#...
......##.....#.................
.#.##..#.#.......#......#.#.#..
.#...#....#.##..........##.##..
#...##......####.#....#....#...
.#...#.##.#.#.....#...#........
.#................#.##.#.###...
...#.#..#.#.....##.....##....#.
..##.#..#..##.....#....#...#.##
........###.##..#..###.....#..#
..##.....#.......#.#...##......
#.#..###...##.###.##.#..#...#..
#..#..#.#...#....#...##.....#.#
#..................#........#..
#.....#.......#.##....##....#..
...#.............#.....#...#...
...#...#.##..##.....#........#.
.......#........##....###..##..
.#....#....#.#..#......#....#.#
..........#..#.#.....##...#.##.
.#...##.#...........#.#.......#
..#.##.....#.###.#.............
..#....###..........#.#.#......
#.....#.####..#.#......#..#.#.#
...#........#..#...............
.###.#.##.....#.#...........#..
..#....#..#....#..##....#......
......#..#.....#.#.##.......#.#
###..#...#.#..#....#..##.###..#
.#....##.###........##...##.#.#
........##..##.#....##..#....#.
...#..#....#.#....#...#...##...
#.....#......#.##........#....#
....#....###.##...#.#.##....#..
......#.##..#.#..........#...#.
...........#...#....##...#....#
......#.#.........#....#.#.#...
.###..........#.###.##....#...#
...##.......#......#....#....#.
#..#...#.#..####...#......#..#.
....##..#.#.........#..........
.##.###.##....##.####....#...#.
..##.......#........#...#..#...
....#####..........###....#....
.#.#..#.#.#....#..#............
........#.....#....#.......##..
...........##....##..##.....##.
..###........#.#.#..#....##...#
.....#...........##......#..#..
...##........#.##.#......##..#.
##..#....#............##..#..#.
.#.....#...##.##..............#
#..##........#...#...#......##.
......##.....#.......####.##..#
...#.#....#...#..#.............
..#...#..##.###..#..#.......##.
##....###.......#...#..#.......
#..#.....###.....#.#.........#.
#.#....#.............#...#.....
..#.#.##..........#.....##.#...
.....##......#..#..#.....#..#..
##.#..#..#.##......###....#..#.
...#............##...#..##.....
.#..#....#.........#......#.##.
.##.##...#..............#..#.##
...#....#...###...#...#....#..#
..#...#..####..#....#.#...##..#
..............##.##.......##...
..##.#..##...........#.#.#...#.
..................##.####.###..
.#...........#.......#......#..
.#.#.#...#....#.........##...##
....#..........#.#....#.#.....#
..........#.#..........#.#.....
...........#.....#.#......#....
........#..#.#.#.#.............
...###...##...##..####.##......
.#..#......###.....#...#.....#.
.........##............#.#.....
#.#..#.#.#....###.#.#..#..#..##
..........#...#.##.#..#..#....#
#..#.......##....#..##........#
##.#...#....##.............#...
....#........#......##..#..#.##
.................#.#.#.#.#.....
...........#.#.....#.......#...
#.......#.......#............#.
....#...........#.#.##.....#..#
#...#.....#....#..##...#.......
..#.....#.....#.##.##....#.....
.#.#..#...#..#..##.....##..#...
.#.#....#.........####.........
#...#..####.....#...#..##......
..#...##.#.....#...#.....##....
.#...#.....#.#.#......#.......#
..#.....##.#..#.#...##.........
##.#...#..#....#....#.##.##...#
.#..#....#..##.#.......#..#....
...##.#......#...###.......#...
...#..#.........##.####........
#.#..#..##...........#..#......
.#...#.#......#.#..........#...
...###...#.......#.....#.#...##
..#....#.#.##..........##...#..
.....###.........#.....#..##..#
.......##.....#.#.....#.#..##..
.#.#.###..##.......##...#......
......#.....#................##
.#......##..##.#.#...#...#...##
.#...#......#.......#.#........
.#..........###...#..#...#.....
.........##.....#.#..#..#.#...#
#...#...#.........#..#..#....#.
###.......#.#.....#....##......
.#..#......#..#...........#..#.
..##....##..##...#......#......
.#........#....#...#....#.....#
.#.......#...#...#..##.#.#..#..
#...#........#.##.....#.....#..
#..##.....#..........#...#...##
............#...............#..
.#.##...#.....#.#..#..#..#.....
.#.#.#...#........#....#...##..
##......#.....#.###.#...#.#..#.
.........##..#..#.#...#...#...#
#...#.#....#..#..#.....#.......
.......#.###...#.............#.
..#.....#.#.#..###.#....#.....#
....#...#.#....#.#..........#..
..#......#.###.#.#..#.....#...#
#............#..##...##......#.
#...........#..#....#.###..###.
.#.##.#.#.......#.............#
..............#................
..#.#.....#.....#...#......#...
.#.#.#..#..#.#...........##....
.....##.#......#..#.##....#....
.......##..#.#.#..#............
..#.....#.....#.###..#.....#.#.
......##.....#..##.#...#.....#.
...#...#....#..#..#........#...
..#.##..#....#.........#.#..#..
#....#.....###.....#......#....
##.....#..#..##.........#.##.##
.#.#....#.#..........#.........
.##.#...#..#.......#.##...#....
...#...#.....#....#...#.#..#...
.....#....#.....#.....#.#......
...........#.#.......#.......#.
.........##.###.##........#....
#..##.....#...#.#..............
.#...#....##........#.#..#....#
..#...#........#...#..#.##.#..#
........#...#.....##.#.#....#.#
#..#.......###.#....#.#.#......
.......#...##....#...#..##..#..
.....##........#.#.#..#....##..
.#....#..#.#...........#......#
...##....#.##.....##.......#...
.##..#..#....#.#....#..#....##.
..#....#.....###.......#..##..#
....#.......#....##..#....#..##
....#......##..#....#.#...#.#..
.##.#......##..................
##.#....#........#..#..#...##.#
.......#..#.#...##.....#.#.....
..##.#...........#.#.#..#.#.#..
.....#....#......#..#.......#..
#.#...#.####..##.......#..##...
...#....#.....#.##.#..#.##..#..
.#.......#......##........##.#.
.......#.#...#..#...#..##.#....
.#....#........#.#.....##..#..#
#..#.....#..#.............#...#
#...#....#..#...###..#...#.#...
.#..#.....#..........#..##.####
#.#.#.#.##.#.#.....##.#........
...#....##....#...#..##.......#
..##.##.#.#........#..........#
..###........###..#..........#.
...#......#..##.#........#..#..
#.#.#..#........#..#..........#
...#........#..##.#...#.###....
##......#.####.#....#......#...
.#..#......#................#..
#.#........#.#.....##.....##...
#...............#..#.......#.#.
.##..#...........##..#..#.#....
#......#.#.......#.#.#.##..#.##
.....##.#..###.............##..
....##.........#..#...#........
.....#.....#.#.#..#.#..........
#.........#....##.#.##.....#..#
.#.........#......#.#.##.#.#...
##.........#.....#..#.#..#.##.#
....#......##...#.....#..#..###
..#..............#...#..####...
#....#...##.#.......#...#..#...
#.......###.#.#.......#.......#
...##....#.#...........#...###.
...........#..#.#.....#..##..#.
..#.........#..###..#.....#...#
..#.#.....#.#.#...#.#.#......#.
........#.....#.#......##....##
##.#.#...#.#........#.....#...#
........#....#...............#.
##.###......####...#####..#....
...##...#..#....#........#...#.
...###.#..................##.#.
##.#.......###.......#...#.#...
....#..#.#...#...#....#.#.#..##
....#...........#..#...........
#..#.#..#...#...#..#...........
...#...#.#....#..#....#........
#....#.......#.##........#..#..
.....#...#..#................#.
#......#.......#..........##..#
.#....#.#......#.#...#....##..#
...#.##...#......#.#...##...##.
..#...#..##...#...#....#.......
.....#....#.#.#..........#.#...
...#...#..#....#..#.#..........
......#.#..........##.......#..
.#...##.#.#...#..##..#...#.....
..#..#.........#........#.#.#..
#.#..##..#.....##......#.....#.
#..#.....#.#....#...#.#....#.#.
......#........##.#..#...#.....
...#.##.#.#......#.#..##...#..#
....#..###..#..#.....###....##.
.....#...#.#.....#..........#.#
.#...##..##.....#..#...#.#.#...
.##.#......##...##..#...#.....#
.#.##....#...#.##.#.#...#.#...#
....#.#...#....###.#.....#.....
#.....####................#..#.
....#.....#...#.#.......##.#...
.#...##.#...#..#...........#.#.
..#####..#.#...#...##........#.
...#...##........#...#.#....###
........#.#.#..#.....#.......#.
...#...#..##............##.....
#.#..###....###.#...#.#...##.##
..#.##...#......#..#.........##
.##..#..#.....#..#.........#.#.
.#..#.#....#.##...#..#.##....##
..#...#.#...##.#.#...#...#....#
#..........#.......##..##....#.
#...###.#......#....#.........#
#.....#...##.......##....##....
.##.#..#.##......#.##....#..#..
............#.#....##.#..#....#
.#.........##.##...#....#.....#
##....##..#..#....##...#.....##
...#.....#...........#.....##..
......#...#.........#.......#..
............#...##.#.....#.#.#.
.#........##..........#.....#.#
.###.........#.....#.##...#....
.##..#...##...#..#..#.##.......

39
day3/part1/main.d Normal file
View file

@ -0,0 +1,39 @@
import std;
void main()
{
File("input", "r").byLineCopy.array.countTrees(3, 1).writeln;
}
ulong countTrees(T)(T input, int angleX, int angleY)
in(input.length > 0)
{
immutable width = input[0].length;
size_t count = 0;
size_t x = 0;
return input.stride(angleY).map!((row) {
auto result = row[x];
x = (x + angleX) % width;
return result;
})
.filter!(pos => pos == '#')
.count;
}
unittest
{
auto input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`.byChar.splitter("\n").map!array.array;
assert(input.countTrees(3, 1) == 7);
}

323
day3/part2/input Normal file
View file

@ -0,0 +1,323 @@
.......#..#....#...#...#......#
..##..#...##.###.#..#.....#.#..
#..#.#....#......#..#.........#
.#..##...#........#....#..#..#.
#.#.#....###...#........#.....#
.#...#.#.##.#.##...#.#.........
####......#.......###.##.#.....
..#...........#...#.#.#........
.#.......#....###.####..#......
...##........#....##.......##..
.###......##.#......##....#.#.#
........#.#......##...#......#.
#....##.#..#...#.......#.......
.#..##........##.........#....#
.#..#..#...#....#.#......#.#...
..#.#......##.#.......#....##..
......##......#.#..##.#..#...#.
.....##.......#.#....#.#.......
........#.....#.....#..###.#...
#........#..#.....#...#.#.#..#.
.#..#.....#...#........#.....#.
.#.#.....#.....#...#...........
.....#.#..#..#...#..#..#..##..#
##.#...#....#..#.##..#.....#.#.
#.......####......#..#.#....#..
......#.#...####.........#.#..#
.#.........#..#.#...#..........
...#####.#....#.#..#......#.#.#
##....#.###....##...##..#.....#
...........####.##.#....##.##..
#.#.#..........#.#..##.#.######
##...#..#...........###..#....#
.#.#.#...##..........##.#...#..
...#.#........#..##...#....#...
......#..#...#..##....#.......#
.#..#.......#..#......##....##.
.......#.......#........#..##..
...#...#...#.##......#.##.#....
.........#.........#.#.#.##....
..#...................#....#..#
.........#..#.....#.#...#....#.
#.#.#...#........#..###.#......
#.#.#.####......##...#...#....#
#...........##..#.#.#....#..#..
........#..#.#...........##.#.#
.#.........#...........#..#....
#............##.#..#....##...##
.#....##..#.#....#.......#..#..
..#.#...#.#......####.......#..
...#.#.......###......#.....#..
#......#.......#.#...#.#..##...
...#.....#...##.#.....#.#......
#.#.#............#..#......#..#
....#...#...##.##.##...##.#....
..##........#..#........#...##.
.......#..#...#.........#.....#
...........#.#......#...#......
...##..##..##..###..#..#..#..#.
#..##.......##..#....#....#.#..
#.#.##.#..##.....#....#.#......
....#..##......#.#..#....#....#
.#.#.........##...#......##.##.
##...........#..#.....#.###....
.#.###........#...#....##..#...
......##.....#.................
.#.##..#.#.......#......#.#.#..
.#...#....#.##..........##.##..
#...##......####.#....#....#...
.#...#.##.#.#.....#...#........
.#................#.##.#.###...
...#.#..#.#.....##.....##....#.
..##.#..#..##.....#....#...#.##
........###.##..#..###.....#..#
..##.....#.......#.#...##......
#.#..###...##.###.##.#..#...#..
#..#..#.#...#....#...##.....#.#
#..................#........#..
#.....#.......#.##....##....#..
...#.............#.....#...#...
...#...#.##..##.....#........#.
.......#........##....###..##..
.#....#....#.#..#......#....#.#
..........#..#.#.....##...#.##.
.#...##.#...........#.#.......#
..#.##.....#.###.#.............
..#....###..........#.#.#......
#.....#.####..#.#......#..#.#.#
...#........#..#...............
.###.#.##.....#.#...........#..
..#....#..#....#..##....#......
......#..#.....#.#.##.......#.#
###..#...#.#..#....#..##.###..#
.#....##.###........##...##.#.#
........##..##.#....##..#....#.
...#..#....#.#....#...#...##...
#.....#......#.##........#....#
....#....###.##...#.#.##....#..
......#.##..#.#..........#...#.
...........#...#....##...#....#
......#.#.........#....#.#.#...
.###..........#.###.##....#...#
...##.......#......#....#....#.
#..#...#.#..####...#......#..#.
....##..#.#.........#..........
.##.###.##....##.####....#...#.
..##.......#........#...#..#...
....#####..........###....#....
.#.#..#.#.#....#..#............
........#.....#....#.......##..
...........##....##..##.....##.
..###........#.#.#..#....##...#
.....#...........##......#..#..
...##........#.##.#......##..#.
##..#....#............##..#..#.
.#.....#...##.##..............#
#..##........#...#...#......##.
......##.....#.......####.##..#
...#.#....#...#..#.............
..#...#..##.###..#..#.......##.
##....###.......#...#..#.......
#..#.....###.....#.#.........#.
#.#....#.............#...#.....
..#.#.##..........#.....##.#...
.....##......#..#..#.....#..#..
##.#..#..#.##......###....#..#.
...#............##...#..##.....
.#..#....#.........#......#.##.
.##.##...#..............#..#.##
...#....#...###...#...#....#..#
..#...#..####..#....#.#...##..#
..............##.##.......##...
..##.#..##...........#.#.#...#.
..................##.####.###..
.#...........#.......#......#..
.#.#.#...#....#.........##...##
....#..........#.#....#.#.....#
..........#.#..........#.#.....
...........#.....#.#......#....
........#..#.#.#.#.............
...###...##...##..####.##......
.#..#......###.....#...#.....#.
.........##............#.#.....
#.#..#.#.#....###.#.#..#..#..##
..........#...#.##.#..#..#....#
#..#.......##....#..##........#
##.#...#....##.............#...
....#........#......##..#..#.##
.................#.#.#.#.#.....
...........#.#.....#.......#...
#.......#.......#............#.
....#...........#.#.##.....#..#
#...#.....#....#..##...#.......
..#.....#.....#.##.##....#.....
.#.#..#...#..#..##.....##..#...
.#.#....#.........####.........
#...#..####.....#...#..##......
..#...##.#.....#...#.....##....
.#...#.....#.#.#......#.......#
..#.....##.#..#.#...##.........
##.#...#..#....#....#.##.##...#
.#..#....#..##.#.......#..#....
...##.#......#...###.......#...
...#..#.........##.####........
#.#..#..##...........#..#......
.#...#.#......#.#..........#...
...###...#.......#.....#.#...##
..#....#.#.##..........##...#..
.....###.........#.....#..##..#
.......##.....#.#.....#.#..##..
.#.#.###..##.......##...#......
......#.....#................##
.#......##..##.#.#...#...#...##
.#...#......#.......#.#........
.#..........###...#..#...#.....
.........##.....#.#..#..#.#...#
#...#...#.........#..#..#....#.
###.......#.#.....#....##......
.#..#......#..#...........#..#.
..##....##..##...#......#......
.#........#....#...#....#.....#
.#.......#...#...#..##.#.#..#..
#...#........#.##.....#.....#..
#..##.....#..........#...#...##
............#...............#..
.#.##...#.....#.#..#..#..#.....
.#.#.#...#........#....#...##..
##......#.....#.###.#...#.#..#.
.........##..#..#.#...#...#...#
#...#.#....#..#..#.....#.......
.......#.###...#.............#.
..#.....#.#.#..###.#....#.....#
....#...#.#....#.#..........#..
..#......#.###.#.#..#.....#...#
#............#..##...##......#.
#...........#..#....#.###..###.
.#.##.#.#.......#.............#
..............#................
..#.#.....#.....#...#......#...
.#.#.#..#..#.#...........##....
.....##.#......#..#.##....#....
.......##..#.#.#..#............
..#.....#.....#.###..#.....#.#.
......##.....#..##.#...#.....#.
...#...#....#..#..#........#...
..#.##..#....#.........#.#..#..
#....#.....###.....#......#....
##.....#..#..##.........#.##.##
.#.#....#.#..........#.........
.##.#...#..#.......#.##...#....
...#...#.....#....#...#.#..#...
.....#....#.....#.....#.#......
...........#.#.......#.......#.
.........##.###.##........#....
#..##.....#...#.#..............
.#...#....##........#.#..#....#
..#...#........#...#..#.##.#..#
........#...#.....##.#.#....#.#
#..#.......###.#....#.#.#......
.......#...##....#...#..##..#..
.....##........#.#.#..#....##..
.#....#..#.#...........#......#
...##....#.##.....##.......#...
.##..#..#....#.#....#..#....##.
..#....#.....###.......#..##..#
....#.......#....##..#....#..##
....#......##..#....#.#...#.#..
.##.#......##..................
##.#....#........#..#..#...##.#
.......#..#.#...##.....#.#.....
..##.#...........#.#.#..#.#.#..
.....#....#......#..#.......#..
#.#...#.####..##.......#..##...
...#....#.....#.##.#..#.##..#..
.#.......#......##........##.#.
.......#.#...#..#...#..##.#....
.#....#........#.#.....##..#..#
#..#.....#..#.............#...#
#...#....#..#...###..#...#.#...
.#..#.....#..........#..##.####
#.#.#.#.##.#.#.....##.#........
...#....##....#...#..##.......#
..##.##.#.#........#..........#
..###........###..#..........#.
...#......#..##.#........#..#..
#.#.#..#........#..#..........#
...#........#..##.#...#.###....
##......#.####.#....#......#...
.#..#......#................#..
#.#........#.#.....##.....##...
#...............#..#.......#.#.
.##..#...........##..#..#.#....
#......#.#.......#.#.#.##..#.##
.....##.#..###.............##..
....##.........#..#...#........
.....#.....#.#.#..#.#..........
#.........#....##.#.##.....#..#
.#.........#......#.#.##.#.#...
##.........#.....#..#.#..#.##.#
....#......##...#.....#..#..###
..#..............#...#..####...
#....#...##.#.......#...#..#...
#.......###.#.#.......#.......#
...##....#.#...........#...###.
...........#..#.#.....#..##..#.
..#.........#..###..#.....#...#
..#.#.....#.#.#...#.#.#......#.
........#.....#.#......##....##
##.#.#...#.#........#.....#...#
........#....#...............#.
##.###......####...#####..#....
...##...#..#....#........#...#.
...###.#..................##.#.
##.#.......###.......#...#.#...
....#..#.#...#...#....#.#.#..##
....#...........#..#...........
#..#.#..#...#...#..#...........
...#...#.#....#..#....#........
#....#.......#.##........#..#..
.....#...#..#................#.
#......#.......#..........##..#
.#....#.#......#.#...#....##..#
...#.##...#......#.#...##...##.
..#...#..##...#...#....#.......
.....#....#.#.#..........#.#...
...#...#..#....#..#.#..........
......#.#..........##.......#..
.#...##.#.#...#..##..#...#.....
..#..#.........#........#.#.#..
#.#..##..#.....##......#.....#.
#..#.....#.#....#...#.#....#.#.
......#........##.#..#...#.....
...#.##.#.#......#.#..##...#..#
....#..###..#..#.....###....##.
.....#...#.#.....#..........#.#
.#...##..##.....#..#...#.#.#...
.##.#......##...##..#...#.....#
.#.##....#...#.##.#.#...#.#...#
....#.#...#....###.#.....#.....
#.....####................#..#.
....#.....#...#.#.......##.#...
.#...##.#...#..#...........#.#.
..#####..#.#...#...##........#.
...#...##........#...#.#....###
........#.#.#..#.....#.......#.
...#...#..##............##.....
#.#..###....###.#...#.#...##.##
..#.##...#......#..#.........##
.##..#..#.....#..#.........#.#.
.#..#.#....#.##...#..#.##....##
..#...#.#...##.#.#...#...#....#
#..........#.......##..##....#.
#...###.#......#....#.........#
#.....#...##.......##....##....
.##.#..#.##......#.##....#..#..
............#.#....##.#..#....#
.#.........##.##...#....#.....#
##....##..#..#....##...#.....##
...#.....#...........#.....##..
......#...#.........#.......#..
............#...##.#.....#.#.#.
.#........##..........#.....#.#
.###.........#.....#.##...#....
.##..#...##...#..#..#.##.......

113
day3/part2/main.d Normal file
View file

@ -0,0 +1,113 @@
import std;
void main()
{
File("input", "r").byLineCopy.array.productOfTreesForDefaultSlopes.writeln;
}
alias product = partial!(reverseArgs!(fold!((a, b) => a * b)), 1UL);
auto productOfTreesForDefaultSlopes(const char[][] input)
{
return [tuple(1, 1), tuple(3, 1), tuple(5, 1), tuple(7, 1), tuple(1, 2)].map!(
slope => input.dup.countTrees(slope[0], slope[1])).product;
}
auto countTrees(const char[][] input, int angleX, int angleY)
in(input.length > 0)
{
immutable width = input[0].length;
size_t x = 0;
return input.stride(angleY).map!((row) {
auto result = row[x];
x = (x + angleX) % width;
return result;
})
.filter!(pos => pos == '#')
.count;
}
unittest
{
auto input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`.byChar.splitter("\n").map!array.array;
assert(input.countTrees(1, 1) == 2);
}
unittest
{
auto input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`.byChar.splitter("\n").map!array.array;
assert(input.countTrees(3, 1) == 7);
}
unittest
{
auto input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`.byChar.splitter("\n").map!array.array;
assert(input.countTrees(5, 1) == 3);
}
unittest
{
auto input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`.byChar.splitter("\n").map!array.array;
assert(input.countTrees(7, 1) == 4);
}
unittest
{
auto input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`.byChar.splitter("\n").map!array.array;
assert(input.countTrees(1, 2) == 2);
}