aoc2020/day3/part1/main.d

34 lines
624 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;
}
2020-12-04 09:36:02 +01:00
auto countTrees(const char[][] input, int angleX, int angleY)
2020-12-04 09:24:06 +01:00
in(input.length > 0)
{
immutable width = input[0].length;
2020-12-04 09:36:02 +01:00
return input.stride(angleY).enumerate
.map!(row => row.value[(row.index * angleX) % width])
2020-12-04 09:24:06 +01:00
.filter!(pos => pos == '#')
.count;
}
unittest
{
auto input = `..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#`.byChar.splitter("\n").map!array.array;
assert(input.countTrees(3, 1) == 7);
}