Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created December 30, 2022 12:56
Show Gist options
  • Save waynejo/3113e91cce51f9b9d5934171d180ea22 to your computer and use it in GitHub Desktop.
Save waynejo/3113e91cce51f9b9d5934171d180ea22 to your computer and use it in GitHub Desktop.
import java.io.FileInputStream
import scala.annotation.tailrec
import scala.io.StdIn
def calories(values: Vector[String]): Vector[Int] =
values.foldLeft(Vector[Int](0)) { (acc, v) =>
if v.isEmpty then
acc :+ 0
else
acc.init :+ (acc.last + v.toInt)
}
def solve1_1(values: Vector[String]): Int =
val cals = calories(values)
cals.max
def solve1_2(values: Vector[String]): Int =
val cals = calories(values)
cals.sortBy(c => -c).take(3).sum
@main def solve1(): Unit =
val in = new FileInputStream("example1-2.in")
System.setIn(in)
val inputs = Iterator.continually(StdIn.readLine())
.takeWhile(line => null != line)
.toVector
println(solve1_1(inputs))
println(solve1_2(inputs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment