diff --git a/src/main/scala/Day01.scala b/src/main/scala/Day01.scala index 77756ea..b22f13f 100644 --- a/src/main/scala/Day01.scala +++ b/src/main/scala/Day01.scala @@ -1,5 +1,13 @@ +import scala.util.Failure +import scala.util.Success + object Day01 extends Solution: override def part_01() = + val file = this.read_input("01") match + case Success(f) => f + case Failure(ex) => + return "Error opening file: " + ex.getMessage() + "part 1 :D" override def part_02() = diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala index e6ac06f..0cdb6da 100644 --- a/src/main/scala/Main.scala +++ b/src/main/scala/Main.scala @@ -6,4 +6,3 @@ import scala.util.Random println("Advent of code 2022 with Scala") println(Day01.part_01()) - diff --git a/src/main/scala/Solution.scala b/src/main/scala/Solution.scala index 00f7962..846ea84 100644 --- a/src/main/scala/Solution.scala +++ b/src/main/scala/Solution.scala @@ -1,4 +1,21 @@ +import java.io.BufferedReader +import scala.util.Try +import scala.util.Failure +import java.io.FileReader +import java.io.FileNotFoundException +import scala.util.Success +import scala.io.BufferedSource +import scala.io.Source + + trait Solution: def part_01(): String def part_02(): String + def read_input(day: String): Try[BufferedSource] = + try + Success(Source.fromFile(s"./inputs/day$day.txt")) + catch + case ex: java.io.FileNotFoundException => + Failure(ex) +