Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created December 25, 2020 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynejo/4cb04586444bb2816bd588fc547d7b54 to your computer and use it in GitHub Desktop.
Save waynejo/4cb04586444bb2816bd588fc547d7b54 to your computer and use it in GitHub Desktop.
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