day3: simplify countTrees

This commit is contained in:
Johannes Loher 2020-12-04 09:36:02 +01:00
parent 825cf44894
commit 396cc056ac
2 changed files with 5 additions and 13 deletions

View file

@ -5,16 +5,12 @@ void main()
File("input", "r").byLineCopy.array.countTrees(3, 1).writeln; 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) in(input.length > 0)
{ {
immutable width = input[0].length; immutable width = input[0].length;
size_t x = 0; return input.stride(angleY).enumerate
return input.stride(angleY).map!((row) { .map!(row => row.value[(row.index * angleX) % width])
immutable result = row[x];
x = (x + angleX) % width;
return result;
})
.filter!(pos => pos == '#') .filter!(pos => pos == '#')
.count; .count;
} }

View file

@ -17,12 +17,8 @@ auto countTrees(const char[][] input, int angleX, int angleY)
in(input.length > 0) in(input.length > 0)
{ {
immutable width = input[0].length; immutable width = input[0].length;
size_t x = 0; return input.stride(angleY).enumerate
return input.stride(angleY).map!((row) { .map!(row => row.value[(row.index * angleX) % width])
immutable result = row[x];
x = (x + angleX) % width;
return result;
})
.filter!(pos => pos == '#') .filter!(pos => pos == '#')
.count; .count;
} }