File reading

This commit is contained in:
Araozu 2023-10-31 06:49:45 -05:00
parent abc35536e4
commit 4652ca7a45
3 changed files with 25 additions and 1 deletions

View File

@ -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() =

View File

@ -6,4 +6,3 @@ import scala.util.Random
println("Advent of code 2022 with Scala")
println(Day01.part_01())

View File

@ -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)