aoc2020/day3/part1/main.d

40 lines
691 B
D
Raw Normal View History

2020-12-04 09:24:06 +01:00
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);
}