From 52629702c8ae1166e235b9fadfa9d62816151d70 Mon Sep 17 00:00:00 2001 From: Henk Kalkwater Date: Thu, 3 Dec 2020 10:50:55 +0100 Subject: [PATCH] Day 3, part 1 --- source/app.d | 4 +++- source/day3.d | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 source/day3.d 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); +}