From 4652ca7a45f37feaad4a87d0ee49e635df69a8b0 Mon Sep 17 00:00:00 2001 From: Araozu Date: Tue, 31 Oct 2023 06:49:45 -0500 Subject: [PATCH] File reading --- src/main/scala/Day01.scala | 8 ++++++++ src/main/scala/Main.scala | 1 - src/main/scala/Solution.scala | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) 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) +