Compare commits
No commits in common. "a7e0e74feeda2aa4c1873314b28fae2bff6aace2" and "4652ca7a45f37feaad4a87d0ee49e635df69a8b0" have entirely different histories.
a7e0e74fee
...
4652ca7a45
@ -1,6 +1,5 @@
|
|||||||
import scala.util.Failure
|
import scala.util.Failure
|
||||||
import scala.util.Success
|
import scala.util.Success
|
||||||
import scala.util.Try
|
|
||||||
|
|
||||||
object Day01 extends Solution:
|
object Day01 extends Solution:
|
||||||
override def part_01() =
|
override def part_01() =
|
||||||
@ -9,45 +8,7 @@ object Day01 extends Solution:
|
|||||||
case Failure(ex) =>
|
case Failure(ex) =>
|
||||||
return "Error opening file: " + ex.getMessage()
|
return "Error opening file: " + ex.getMessage()
|
||||||
|
|
||||||
var max_calories_amount = 0
|
"part 1 :D"
|
||||||
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 > max_calories_amount then
|
|
||||||
max_calories_amount = current_sum
|
|
||||||
|
|
||||||
current_sum = 0
|
|
||||||
|
|
||||||
|
|
||||||
"part 1: " + max_calories_amount.toString()
|
|
||||||
|
|
||||||
override def part_02() =
|
override def part_02() =
|
||||||
val file = this.read_input("01") match
|
"part 2 :O"
|
||||||
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,6 +5,4 @@ import scala.util.Random
|
|||||||
@main def hello: Unit =
|
@main def hello: Unit =
|
||||||
println("Advent of code 2022 with Scala")
|
println("Advent of code 2022 with Scala")
|
||||||
println(Day01.part_01())
|
println(Day01.part_01())
|
||||||
println(Day01.part_02())
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,10 +19,3 @@ trait Solution:
|
|||||||
case ex: java.io.FileNotFoundException =>
|
case ex: java.io.FileNotFoundException =>
|
||||||
Failure(ex)
|
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