Commit 099dabd3 authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

Refactor input source for easier switching between sample and real input

Long term: add all samples and build test cases from this
parent 9786d300
package aoc2020
@main def runDay(inputDir: String, day: Int): Unit =
given Location(inputDir, day)
//given Fixed(sample)
val out = day match {
case 1 => input(_.toInt)(day1)
case 2 => input()(day2)
......@@ -15,3 +16,5 @@ package aoc2020
case _ => "No such day implemented"
}
println(out)
//val sample: List[String] = List()
......@@ -2,23 +2,29 @@ package aoc2020
import scala.io.Source
case class Location(inputDir: String, day: Int)
sealed trait InputSource
case class Location(inputDir: String, day: Int) extends InputSource
case class Fixed(lines: List[String]) extends InputSource
def inputLines(using l: Location) = scala.io.Source.fromFile(s"${l.inputDir}/day${l.day}.txt").getLines
def inputLines(using src: InputSource): Iterator[String] = src match {
case Location(inputDir, day) => scala.io.Source.fromFile(s"${inputDir}/day${day}.txt").getLines
case Fixed(input) => input.iterator
}
def simpleInput(solver: String => String)(using l: Location): String =
def simpleInput(solver: String => String)(using l: InputSource): String =
solver(inputLines.mkString)
def input[A](format: String => A = identity)(solver: List[A] => String)(using l: Location): String =
def input[A](format: String => A = identity)(solver: List[A] => String)(using l: InputSource): String =
inputF(format)(List)(solver)
def inputF[A, C[_]](format: String => A = identity)(coll: collection.Factory[A, C[A]])(solver: C[A] => String)(using l: Location): String =
def inputF[A, C[_]](format: String => A = identity)(coll: collection.Factory[A, C[A]])(solver: C[A] => String)(using l: InputSource): String =
solver(inputLines.map(format).to(coll))
def flatInput[A](format: String => List[A] = List.apply)(solver: List[A] => String)(using l: Location): String =
def flatInput[A](format: String => List[A] = List.apply)(solver: List[A] => String)(using l: InputSource): String =
flatInputF(format)(List)(solver)
def flatInputF[A, C[_]](format: String => IterableOnce[A] = identity)(coll: collection.Factory[A, C[A]])(solver: C[A] => String)(using l: Location): String =
def flatInputF[A, C[_]](format: String => IterableOnce[A] = identity)(coll: collection.Factory[A, C[A]])(solver: C[A] => String)(using l: InputSource): String =
solver(inputLines.flatMap(format).to(coll))
def boolChar(trueChar: Char): String => Vector[Boolean] = _.map(_ == trueChar).toVector
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment