AdventOfCode/2022/day1.scala

30 lines
824 B
Scala
Raw Permalink Normal View History

2022-12-01 19:53:03 +10:30
import scala.collection.mutable.ArrayBuffer
import scala.io.Source
2022-12-03 17:22:53 +10:30
val filename = "input/1"
2022-12-01 20:00:08 +10:30
2022-12-01 19:53:03 +10:30
class Elf:
var foodCalories = List[Int]()
var totalCalories = 0
def calculateTotalCalories(): Int =
totalCalories = foodCalories.sum
totalCalories
@main def main() =
val elves = ArrayBuffer[Elf](Elf())
for (line <- Source.fromFile(filename).getLines)
if line.isEmpty then
elves.last.calculateTotalCalories()
elves.addOne(Elf())
else
elves.last.foodCalories = line.toInt :: elves.last.foodCalories
elves.last.calculateTotalCalories()
// Part 1 solution: Total calories held by elf with the most total calories
println(s"${elves.map(elf => elf.totalCalories).max}")
// Part 2 solution: Total calories held by the top 3 elves
println(s"${elves.map(elf => elf.totalCalories).sorted.takeRight(3).sum}")