From 396cc056acec3c244bea49a84ce44d2ef986c4fc Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Fri, 4 Dec 2020 09:36:02 +0100 Subject: [PATCH] day3: simplify countTrees --- day3/part1/main.d | 10 +++------- day3/part2/main.d | 8 ++------ 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/day3/part1/main.d b/day3/part1/main.d index df48eda..4e3dc00 100644 --- a/day3/part1/main.d +++ b/day3/part1/main.d @@ -5,16 +5,12 @@ void main() File("input", "r").byLineCopy.array.countTrees(3, 1).writeln; } -ulong countTrees(const char[][] input, int angleX, int angleY) +auto countTrees(const char[][] input, int angleX, int angleY) in(input.length > 0) { immutable width = input[0].length; - size_t x = 0; - return input.stride(angleY).map!((row) { - immutable result = row[x]; - x = (x + angleX) % width; - return result; - }) + return input.stride(angleY).enumerate + .map!(row => row.value[(row.index * angleX) % width]) .filter!(pos => pos == '#') .count; } diff --git a/day3/part2/main.d b/day3/part2/main.d index 088c826..1d793d7 100644 --- a/day3/part2/main.d +++ b/day3/part2/main.d @@ -17,12 +17,8 @@ auto countTrees(const char[][] input, int angleX, int angleY) in(input.length > 0) { immutable width = input[0].length; - size_t x = 0; - return input.stride(angleY).map!((row) { - immutable result = row[x]; - x = (x + angleX) % width; - return result; - }) + return input.stride(angleY).enumerate + .map!(row => row.value[(row.index * angleX) % width]) .filter!(pos => pos == '#') .count; }