1
0
Fork 0
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:
Chris Josten 2020-12-03 10:50:55 +01:00
parent b21ca2df4a
commit 52629702c8
Signed by: chris
GPG key ID: 1795A594046530AB
2 changed files with 34 additions and 1 deletions

View file

@ -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
View 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);
}