2020-12-03 09:50:55 +00:00
|
|
|
import std.algorithm;
|
2020-12-04 09:32:34 +00:00
|
|
|
import std.array;
|
2020-12-03 09:50:55 +00:00
|
|
|
import std.format;
|
|
|
|
import std.range;
|
|
|
|
import std.stdio;
|
2020-12-04 09:32:34 +00:00
|
|
|
import std.traits;
|
2020-12-03 09:50:55 +00:00
|
|
|
import std.uni;
|
|
|
|
|
2020-12-04 09:32:34 +00:00
|
|
|
import dayutil;
|
|
|
|
|
2020-12-03 09:50:55 +00:00
|
|
|
void run(string[] args) {
|
2020-12-04 09:32:34 +00:00
|
|
|
auto input = stdin.byLineCopy.array;
|
|
|
|
ulong count = parts!ulong(args,
|
|
|
|
() => input.countTrees1,
|
|
|
|
() => [[1,1], [3,1], [5,1], [7,1], [1,2]]
|
|
|
|
.map!(x => input.save.enumerate.countTrees2(x[0], x[1]))
|
|
|
|
.fold!((x, y) => x * y));
|
|
|
|
|
2020-12-03 09:50:55 +00:00
|
|
|
writeln(count);
|
|
|
|
}
|
|
|
|
|
2020-12-04 09:32:34 +00:00
|
|
|
ulong countTrees1(Range)(Range lines) if (isInputRange!Range && isSomeString!(ElementType!Range)) {
|
2020-12-03 09:50:55 +00:00
|
|
|
return lines.enumerate.filter!(x => (x.value[x.index * 3 % x.value.length] == '#')).count;
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest {
|
|
|
|
string[] field = [
|
2020-12-04 09:32:34 +00:00
|
|
|
"..##.......",
|
|
|
|
"#...#...#..",
|
|
|
|
".#....#..#.",
|
|
|
|
"..#.#...#.#",
|
|
|
|
".#...##..#.",
|
|
|
|
"..#.##.....",
|
|
|
|
".#.#.#....#",
|
|
|
|
".#........#",
|
|
|
|
"#.##...#...",
|
|
|
|
"#...##....#",
|
|
|
|
".#..#...#.#"
|
|
|
|
];
|
|
|
|
assert(field.countTrees1 == 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Screw proper template type checking, ain't got time for that.
|
|
|
|
ulong countTrees2(Range)(Range lines, int advanceX, int advanceY) if (isInputRange!Range)
|
|
|
|
in (advanceX > 0)
|
|
|
|
in (advanceY > 0)
|
|
|
|
out (r; r >= 0) {
|
|
|
|
return lines.enumerate.filter!(x => {
|
|
|
|
char c = x.value.value[(x.value.index * advanceX) / advanceY % x.value.value.length];
|
|
|
|
return (x.index % advanceY) == 0 && c == '#';
|
|
|
|
}()).count;
|
|
|
|
}
|
|
|
|
|
|
|
|
unittest {
|
|
|
|
string[] field = [
|
|
|
|
"..##.......",
|
|
|
|
"#...#...#..",
|
|
|
|
".#....#..#.",
|
|
|
|
"..#.#...#.#",
|
|
|
|
".#...##..#.",
|
|
|
|
"..#.##.....",
|
|
|
|
".#.#.#....#",
|
|
|
|
".#........#",
|
|
|
|
"#.##...#...",
|
|
|
|
"#...##....#",
|
|
|
|
".#..#...#.#"
|
2020-12-03 09:50:55 +00:00
|
|
|
];
|
2020-12-04 09:32:34 +00:00
|
|
|
assert(field.enumerate.countTrees2(1, 1) == 2);
|
|
|
|
assert(field.enumerate.countTrees2(3, 1) == 7);
|
|
|
|
assert(field.enumerate.countTrees2(5, 1) == 3);
|
|
|
|
assert(field.enumerate.countTrees2(7, 1) == 4);
|
|
|
|
assert(field.enumerate.countTrees2(1, 2) == 2, "Expected 2, got %d".format(field.enumerate.countTrees2(1,2)));
|
2020-12-03 09:50:55 +00:00
|
|
|
}
|