aoc2020/day3/part2/main.d

110 lines
1.9 KiB
D
Raw Permalink Normal View History

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