day 1 part 2
This commit is contained in:
parent
a6f758277f
commit
a7e0e74fee
@ -22,7 +22,32 @@ object Day01 extends Solution:
|
||||
current_sum = 0
|
||||
|
||||
|
||||
max_calories_amount.toString()
|
||||
"part 1: " + max_calories_amount.toString()
|
||||
|
||||
override def part_02() =
|
||||
"part 2 :O"
|
||||
val file = this.read_input("01") match
|
||||
case Success(f) => f
|
||||
case Failure(ex) =>
|
||||
return "Error opening file: " + ex.getMessage()
|
||||
|
||||
var (max1, max2, max3) = (0, 0, 0)
|
||||
var current_sum = 0
|
||||
|
||||
for line <- file.getLines() do
|
||||
Try(line.toInt).toOption match
|
||||
case Some(value) => current_sum += value
|
||||
case None =>
|
||||
if current_sum > max1 then
|
||||
max3 = max2
|
||||
max2 = max1
|
||||
max1 = current_sum
|
||||
else if current_sum > max2 then
|
||||
max3 = max2
|
||||
max2 = current_sum
|
||||
else if current_sum > max3 then
|
||||
max3 = current_sum
|
||||
|
||||
current_sum = 0
|
||||
|
||||
"part 2: " + (max1 + max2 + max3).toString()
|
||||
|
||||
|
@ -5,4 +5,6 @@ import scala.util.Random
|
||||
@main def hello: Unit =
|
||||
println("Advent of code 2022 with Scala")
|
||||
println(Day01.part_01())
|
||||
println(Day01.part_02())
|
||||
|
||||
|
||||
|
@ -19,3 +19,10 @@ trait Solution:
|
||||
case ex: java.io.FileNotFoundException =>
|
||||
Failure(ex)
|
||||
|
||||
def read_test_input(day: String): Try[BufferedSource] =
|
||||
try
|
||||
Success(Source.fromFile(s"./inputs/day$day.test.txt"))
|
||||
catch
|
||||
case ex: java.io.FileNotFoundException =>
|
||||
Failure(ex)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user