diff --git a/source/app.d b/source/app.d index f86d5d9..f5e3c24 100644 --- a/source/app.d +++ b/source/app.d @@ -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) { diff --git a/source/day3.d b/source/day3.d new file mode 100644 index 0000000..f180c29 --- /dev/null +++ b/source/day3.d @@ -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); +}