From 2072fc3ed89a544dd4337506453dbce91ce6126b Mon Sep 17 00:00:00 2001 From: Henk Kalkwater Date: Fri, 4 Dec 2020 10:32:34 +0100 Subject: [PATCH] Day 3, part 2 --- in/3.txt | 323 ++++++++++++++++++++++++++++++++++++++++++++++++++ source/day3.d | 70 ++++++++--- 2 files changed, 379 insertions(+), 14 deletions(-) create mode 100644 in/3.txt diff --git a/in/3.txt b/in/3.txt new file mode 100644 index 0000000..13fac46 --- /dev/null +++ b/in/3.txt @@ -0,0 +1,323 @@ +..#..#......#..#.......#...#.#. +...##.....##..#..#....#.##.##.# +...#...#.##...##.....#.....#.#. +..#....#.....#...##.##.###.#... +.#.....#......#..#.##.#...###.. +#..#..#.#......#...........###. +#......####.#....##........##.. +.#.......#.....#......#...#.... +...#...#.....#.......##.....##. +#...##.........#.#..##..#..#.## +#.#.##.........#.#..#.#.....### +.##..##...#....##.....#..#..... +........#.......###.#.#.....#.# +...#.#....#.##..#...##..##..#.. +......#....#........######.#... +.##...#.#...###......#.#.#..#.# +........#.##...##.#...#..#...## +.#..#.#..##....###..#.#.......# +..#..##..#.#...#.##......#..... +##.....#..##.#.#..#......##...# +......................#..#..#.. +..#.##....####.........###.##.. +##..###..#...#....#..#.#...#... +.##.#......#..#....#........#.. +.#.....#..#..#.#.#....#.....##. +..........#..#....#..##...#..## +.#...#.#....#.##..#.....#....#. +#..............#.#.#..#..#....# +...#.#...............##........ +#.#.##...#.##..##.....#........ +...#.......###..###..#...#..#.. +####..#.#..##.....##.#.#......# +.#.#.......#..##.......#....... +#....#...#.##.#.......#..#..... +.#...##..#..#..##.......##...#. +.#..#......#.........#......... +#.##.#.....#....#..##..#.....#. +#.#....#.#....#...#.#..#....#.. +#..#.....#.##..#.....#...##...# +#....#...##.#.........#.#....## +.......##.##......##.......##.. +#.....#..#........#........#... +#....#.#..#.#........##.#...#.. +#.......#.#.#.#....#.......##.# +...#..###..........#...#.#.###. +....#..#....#...#....##.#.....# +.#..##.....#..#....##..##...#.# +#.........#....#.#..###...##... +.#.#.........#.#.......#.#.#..# +..........#........##..#....... +.....#.......#...#.....#..##.## +...#.........#.............#### +##..#...#..#.#......#...#...... +.#..###...#.#.#.#...#...#...... +....#..##.#....#..#.#..##..##.# +..#.......#......#..#.......#.. +....###......#...##...#....#... +..#..#.....#...#..###....#.#..# +.........##..#.##....#..##..#.. +##...#...#.#.........##......#. +###..#.#....#......##..##.#...# +.##...##..#.#.#.#......#..#.... +###......#..#..#.....#..#....#. +.#.#..##....##........##..#.#.. +###...####.#....#.......###.... +..#....###..#.#.#..#.......##.. +.......#.#...#.....#.#....##.#. +......#......#.#....#..##..###. +....####..........#.....#...... +.###.....#...#..#...##.#...###. +...##....##....###....#.#..#.#. +##.#..........##.........#.##.. +..#..#.#.###..##..#....##.....# +..#....##.....#...##....###..## +....#.......##..#..#..........# +............#..#.###..#.#...... +...........##......#.#.#...#..# +...##.##....#...##.##.....#.#.. +.####...#....###...#.....#....# +.##........#..##..#.#.....#.... +..................#.....#..##.. +..###.....#.##..#..#....##...#. +...#.##.#.####.#.###.#....#..## +.#....##..##......####.#####... +#...#.#....##.........##....#.. +..#.##.....##.............#.##. +###.....#.#..#..#......#.##.#.. +...#..##.....#...##...#......#. +.##.#...#......##.#..##....#... +.....##.....#......#.#......... +#.....#.....#........##........ +.#......##...#..#.#....###.#..# +#.####...#....#.........#..#... +#..##.#.....#.##.##.#....#...#. +#########..#....#..#...#......# +..##..##...###.######...##.##.. +##.......#.......#.#....###..#. +.....#...#.######..#.....#..... +.#......#..#.............#.##.# +..###.#.#......##...###........ +.......####.#..##....#........# +..#......#.##....##.##....#.... +....#......#.#....#..#.#.....## +####.....#....#.#......#.#.#.## +#...##....#.#.##.........#....# +....#..###......#......#...#... +.....##.#..#..#...#..#.#.#..... +.##............#.....#......... +##...#..#.....##.#..#.......... +#.....#####.......#..#....#.#.. +.........#..#.....###........#. +#....#..#...###........#..#.#.. +...##...#..#...#.##..#......... +.........#.#.....#.......#...#. +.#.....#..####....#.##.......## +...............##....##.##..##. +............#....#....#...##.#. +..#...#........#.......#..#.... +##....####....#.##...#..##..#.# +.#.#.....#......#.#........#.#. +....#......#.#....##..##....... +.#..#.#..#..##.....#........... +..#........#.##..#......##..#.. +...##.#...#...#..#........#.... +##..##....#......#...#..#.#.#.# +......#.....#..#..#....#....... +.....##......#..#.#.##...#..... +...#.....#.#..........#..##...# +.####.##....#...........#.....# +.....###..##...#....##..#...#.. +..##...#.#...#..........#..#.#. +...#..#..............#.##.#.... +##.#....#...#..#....#.......... +.##..........#..#........#..... +#...#.#......#...#.....##..#... +.##...#.#.#....###.####..#....# +.#......#.#...#.#....#.#...#... +#....##.###.............#.#.... +....#.###..##..##.##...##...... +##....#..###.##.##.....#......# +..#..#..#......#..#..#......... +#.##......#.#....#..#..#....... +....#.#...#..###......##....... +.###.......##.......#....###... +..#..#.##..#.#....#..#.#.....#. +.#..##.##..............#....#.. +#...#.#...#..#.##..##.#.#...... +#...#..#..##..##.###......#.... +.#..#.....#...#....#.....#...#. +.....#....#..#.....###...#.#### +.#.....#......#...##...#..#.... +.#......#............#.#....... +....##....#.#..#..#...#..#.#... +#...#.....###...##...#.##.....# +.......#.....#....#.......#...# +#.......###.......#.#.......... +...#.#.###.#........#.###...#.. +....#............#....#..#..... +#......#.##.#...##.......#..#.# +.....#....#....#.#.#...###..#.. +.....#.#...#...#.#..#....#.#..# +.#.......#.#..#...###.......##. +.......#..#.##.........#....... +.##.#........#.##...##....#.... +.#....#..#...#......####...#..# +...#.....#..##.#..#.#....#....# +...##....#........#.#........#. +.....#....##..#.##..........#.. +#.....#.#.#......##....##.#..#. +.#.#.##..#.#....##.#....##..... +.....#.....#..#.#....#..#....## +...#........#....#......###.#.. +.....##...#.....##.##.#.#.##... +...#.....#####....##.#.#.###.#. +.#..#.#..##...###.........#.#.# +#...#...#.#..#...#...........#. +.##..............#...#..#....#. +....###.........#.#.#....#..... +..#...##.#.#....##.#..#...#..#. +..#.....#.#......#....#......#. +.......##....#.#.##....#...#..# +##.#.#...#..#......#..#..#....# +...#.#......#............###.## +..###..#..##..#...##........#.. +.#...#...##...#....#....##.#..# +..##...####....#....#..#....#.# +...#......##....#.........##.#. +##.#.......#..#..#............. +..#.#.#.#......#...#.#..##..... +.#..##.....###...##.#..#......# +##...#..........#.####....##... +#..........#...#..##....#...... +....##...#....#..####...#.##.## +.#.######...##...#..##......... +....##.........#.......##.##... +.#.....#.#..........##......#.. +...#..#.#.###..#........#.....# +..##..#............##.......#.. +......##....##..#.##..#.......# +.......##....#.......#..#...#.# +#.#......#.###.....#.##........ +.#..##..........#..#.....#.##.. +..#.#...#....#.........#..##..# +.#......#.......#...#..#..###.. +......#.##.....#.#......#....#. +....#....#...#.......#...##.##. +#....#...##...#..##........###. +##......#.#..#.......#.......#. +...##.##..#......#.###..#.#.##. +.............#..#.............# +..#.......##..#..#....##...#... +...............##..##........#. +##...#.##.......#....#.......## +....##.##.#.#.....##.....##.##. +#.#......#.......#..#.#..#..... +....##....#.##........##.##.#.. +......##....#..##..#..##....##. +.............#.....#.......#... +.......###.......#..........#.. +......##.#..#.....#.#...#.#...# +.#...#..#..###.###...#....##... +#......#..#.#...#...#.....#..## +.###.....#..#.#......##..#.##.. +.##.#.....#..#.#..#....##...... +#......#..............#.....#.# +...#..#....#.....#.....##.#...# +......#..##..##.....#...#...... +.....####..#..#.##.......#..#.# +###.#.#........#.......#.....## +..#.#.#.#...#...#........#....# +....##.#.#..#...##.....#......# +#..#.##....#..#.##..####....... +...####.#...#......#......##..# +#....#.#..###......#..#..##..#. +...........#....#...#......#... +......###.#.....#.#....#.#...#. +.......#.##..............#..##. +..##...........#..#.#...#.....# +#..#............##.........#.#. +.......###.#...#.#...#.#.#...#. +..#...##.......#..#......#.#.## +#.#...#.....#...##.#.#.......## +.#.#.##...#..##.#......#....... +#.......#.......#.#....#.....#. +.....#..#..#.......#..#........ +##...##...##......#..##.###.... +..#...#.###.#.###..#.....###... +.....####.......#.#.....##....# +....#....#.#....#...#..#.#..#.. +..##.....#....#.#.#.###...#.... +......#.#....#.#..#....#.#..#.. +#...#...#....#.......#......#.# +#..#.#......#..#...........#.## +...............#....#.....#...# +.#.#.#...#.##...#.#.#..#....#.. +...#.#.####..##.#...##......... +##.........##.##.....##....#... +................#...#.##.#.#..# +.#..#....#...#..#..#..###.#..#. +...#..#.##.#.####..........#..# +........#....##......#..#.#.... +........##.........#..#..#..#.# +#......#.#...#...#...##.....#.. +#...#.....#..#..##.#...#.#.#... +....#..##...##.....#...#.#..... +..#..##....#....#...#....#..... +.#..#...##.......###...#...#... +.#......#......#..##..#..##.... +....##....#..#.#....#.#..##.... +###......#...........#.....###. +.....#...#..##.#..#..#.....#..# +#.#....#...........#.##..#..### +#....#...###.#...#..##..#.....# +.#....#......##.#..#....#.#.... +....#.#....#..#.#....#..#..#... +..#......#..#.#.#....#......... +.#...#.#.....#........#.#...### +....#..##.......#.###....##.... +#.#.......#......#.###........# +#.........#.....####.##..#..#.. +.#.#..##...#.#.....##.#.#..#... +.#..#..#..#.##..#...###.#...#.. +.....##..##..##..#.#.#.....###. +.#..#...#..#......##.#......... +....#..##....#.##.........#...# +........#...#...###.........##. +#.........#..##....#.#...#..... +.......#.......#..#.......#.... +#......##......#.#.##.......... +.#..##..####...#.....####.....# +........#.#....#..##..###.#...# +.#...#...#.........###..#...#.# +#.........#....##...#.......... +.#.#....#..........#........... +.#.#..........#.##.....#.##.... +..#....#...##..###..........##. +.#.#..#.##..#..#.##.##..##..... +........#...#....#...#.#..##... +......#......##..#..#.....#..#. +.##.#....#...#....#...#..##..## +##............#..........###... +....#.......#.#..#.#####.....#. +#......#.....#...#........#.... +..##.....###..#.#.#.#....#....# +#...#...#.#..#..#....#..#...... +......#....#...#..#....#####... +....#.......##....#....##...... +.....##...#.##.#.....##....#... +.#....###.#..##...##.##.......# +....#.#.#.##.............#..##. +...........##......#...#.#.##.. +....##......#....#....##..##.#. +.#.#...#.....##.....#.........# +#.#..........#.......#.##...#.. +....#.##..#.#....#.....#...#... +##.............##.......#.##.#. +....#...#.....##...#..........# +##..#...#...#.#.##...#.......## +..#........#.....###...##..##.# +.....#...##.#.#.##.....#...#... +####.###...##..##...#..#..#..## +......#..#..#.........#...#.#.. +....###.....##.##....#.##.....# diff --git a/source/day3.d b/source/day3.d index f180c29..865293d 100644 --- a/source/day3.d +++ b/source/day3.d @@ -1,31 +1,73 @@ import std.algorithm; +import std.array; import std.format; import std.range; import std.stdio; +import std.traits; import std.uni; +import dayutil; + void run(string[] args) { - ulong count = stdin.byLine.countTrees; + 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)); + writeln(count); } -ulong countTrees(Range)(Range lines) if (isInputRange!Range) { +ulong countTrees1(Range)(Range lines) if (isInputRange!Range && isSomeString!(ElementType!Range)) { return lines.enumerate.filter!(x => (x.value[x.index * 3 % x.value.length] == '#')).count; } unittest { string[] field = [ - "..##.........##.........##.........##.........##.........##.......", - "#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..", - ".#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.", - "..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#", - ".#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.", - "..#.##.......#.##.......#.##.......#.##.......#.##.......#.##.....", - ".#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#", - ".#........#.#........#.#........#.#........#.#........#.#........#", - "#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...", - "#...##....##...##....##...##....##...##....##...##....##...##....#", - ".#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#" + "..##.......", + "#...#...#..", + ".#....#..#.", + "..#.#...#.#", + ".#...##..#.", + "..#.##.....", + ".#.#.#....#", + ".#........#", + "#.##...#...", + "#...##....#", + ".#..#...#.#" ]; - assert(field.countTrees == 7); + 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 = [ + "..##.......", + "#...#...#..", + ".#....#..#.", + "..#.#...#.#", + ".#...##..#.", + "..#.##.....", + ".#.#.#....#", + ".#........#", + "#.##...#...", + "#...##....#", + ".#..#...#.#" + ]; + 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))); }