day3: simplify countTrees
This commit is contained in:
parent
825cf44894
commit
396cc056ac
2 changed files with 5 additions and 13 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue