mirror of
https://github.com/HenkKalkwater/aoc-2020
synced 2024-12-22 16:35:18 +00:00
Day 3, part 1
This commit is contained in:
parent
b21ca2df4a
commit
52629702c8
|
@ -7,13 +7,15 @@ import std.getopt;
|
|||
|
||||
import day1;
|
||||
import day2;
|
||||
import day3;
|
||||
import dayutil;
|
||||
|
||||
immutable string progName = "aoc-2020";
|
||||
|
||||
void function(string[])[] programs = [
|
||||
&day1.run,
|
||||
&day2.run
|
||||
&day2.run,
|
||||
&day3.run,
|
||||
];
|
||||
|
||||
void printUsage(string name) {
|
||||
|
|
31
source/day3.d
Normal file
31
source/day3.d
Normal file
|
@ -0,0 +1,31 @@
|
|||
import std.algorithm;
|
||||
import std.format;
|
||||
import std.range;
|
||||
import std.stdio;
|
||||
import std.uni;
|
||||
|
||||
void run(string[] args) {
|
||||
ulong count = stdin.byLine.countTrees;
|
||||
writeln(count);
|
||||
}
|
||||
|
||||
ulong countTrees(Range)(Range lines) if (isInputRange!Range) {
|
||||
return lines.enumerate.filter!(x => (x.value[x.index * 3 % x.value.length] == '#')).count;
|
||||
}
|
||||
|
||||
unittest {
|
||||
string[] field = [
|
||||
"..##.........##.........##.........##.........##.........##.......",
|
||||
"#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..",
|
||||
".#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.",
|
||||
"..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#",
|
||||
".#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.",
|
||||
"..#.##.......#.##.......#.##.......#.##.......#.##.......#.##.....",
|
||||
".#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#",
|
||||
".#........#.#........#.#........#.#........#.#........#.#........#",
|
||||
"#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...",
|
||||
"#...##....##...##....##...##....##...##....##...##....##...##....#",
|
||||
".#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#"
|
||||
];
|
||||
assert(field.countTrees == 7);
|
||||
}
|
Loading…
Reference in a new issue