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;
}
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;
}

View file

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