-
-
Save waynejo/4cb04586444bb2816bd588fc547d7b54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream | |
import scala.io.StdIn | |
@main def solve3() = | |
case class Vector2(x: Int, y: Int) | |
def solve(trees: Vector[Vector[Char]], delta: Vector2, x: Int = 0, y: Int = 0, count: Int = 0): BigInt = { | |
if trees.size - 1 <= y then | |
count | |
else | |
val nextX = x + delta.x | |
val nextY = y + delta.y | |
val nextCount = if '#' == trees(nextY)(nextX % trees(nextY).size) then | |
count + 1 | |
else | |
count | |
solve(trees, delta, nextX, nextY, nextCount) | |
} | |
val in = new FileInputStream("example3-1.in") | |
System.setIn(in) | |
val inputs = Iterator.continually(StdIn.readLine()).takeWhile(_ != null).map(_.toVector).toVector | |
val slopes = Vector(Vector2(1, 1), Vector2(3, 1), Vector2(5, 1), Vector2(7, 1), Vector2(1, 2)) | |
val answers = slopes.map { solve(inputs, _) } | |
println(answers.reduce(_ * _)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment