From 4d1dc6ee2fe7cbab0fe4a46c5d4a2a37c76ba6cd Mon Sep 17 00:00:00 2001 From: Florian Cassayre Date: Sat, 27 Oct 2018 22:23:16 +0200 Subject: [PATCH] Initial commit. --- .gitignore | 2 + LICENSE | 21 ++++++ README.md | 54 ++++++++++++++++ build.sbt | 17 +++++ project/build.properties | 1 + src/main/scala/adventofcode/Day.scala | 64 +++++++++++++++++++ .../scala/adventofcode/solutions/Day01.scala | 12 ++++ 7 files changed, 171 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.sbt create mode 100644 project/build.properties create mode 100644 src/main/scala/adventofcode/Day.scala create mode 100644 src/main/scala/adventofcode/solutions/Day01.scala diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee44a96 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +target diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f18bc3c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Florian Cassayre + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f7c8020 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +_My solutions to the 2018 edition of [Advent Of Code](https://adventofcode.com)._ + +Check out [my 2017 participation](https://github.com/FlorianCassayre/AdventOfCode-2017)! + + +## Problems & Solutions + +* [Day 1](https://adventofcode.com/day/1): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day01.scala) +* [Day 2](https://adventofcode.com/day/2): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day02.scala) +* [Day 3](https://adventofcode.com/day/3): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day03.scala) +* [Day 4](https://adventofcode.com/day/4): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day04.scala) +* [Day 5](https://adventofcode.com/day/5): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day05.scala) +* [Day 6](https://adventofcode.com/day/6): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day06.scala) +* [Day 7](https://adventofcode.com/day/7): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day07.scala) +* [Day 8](https://adventofcode.com/day/8): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day08.scala) +* [Day 9](https://adventofcode.com/day/9): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day09.scala) +* [Day 10](https://adventofcode.com/day/10): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day10.scala) +* [Day 11](https://adventofcode.com/day/11): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day11.scala) +* [Day 12](https://adventofcode.com/day/12): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day12.scala) +* [Day 13](https://adventofcode.com/day/13): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day13.scala) +* [Day 14](https://adventofcode.com/day/14): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day14.scala) +* [Day 15](https://adventofcode.com/day/15): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day15.scala) +* [Day 16](https://adventofcode.com/day/16): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day16.scala) +* [Day 17](https://adventofcode.com/day/17): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day17.scala) +* [Day 18](https://adventofcode.com/day/18): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day18.scala) +* [Day 19](https://adventofcode.com/day/19): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day19.scala) +* [Day 20](https://adventofcode.com/day/20): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day20.scala) +* [Day 21](https://adventofcode.com/day/21): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day21.scala) +* [Day 22](https://adventofcode.com/day/22): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day22.scala) +* [Day 23](https://adventofcode.com/day/23): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day23.scala) +* [Day 24](https://adventofcode.com/day/24): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day24.scala) +* [Day 25](https://adventofcode.com/day/25): –[](https://github.com/FlorianCassayre/AdventOfCode-2018/blob/master/src/main/scala/adventofcode/solutions/Day25.scala) + + +## Testing + +You'll need Scala `2.12.7` and sbt `1.2.6`. + +Start by entering the sbt shell: +``` +$ sbt +``` + +To run a solution - for instance **day 1** - you can conveniently use the following command: +``` +> day 1 +``` + +The output will be printed to the console and stored to the corresponding file in `output/`. + + +## License + +This repository is licensed under the MIT License, please refer to the `LICENSE` file. \ No newline at end of file diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..064771e --- /dev/null +++ b/build.sbt @@ -0,0 +1,17 @@ +lazy val root = project + .in(file(".")) + .settings( + name := "AdventOfCode-2018", + description := "Advent of Code 2018", + version := "0.1.0", + + scalaVersion := "2.12.7" + ) + +commands += Command("day") { _ => + import complete.DefaultParsers._ + (' ' ~ charClass(_.isDigit, "digit").+.map(_.mkString.toInt)).map(_._2) +} { case (previousState, i: Int) => + val formatted = "%02d".format(i) + Command.process(s"runMain adventofcode.solutions.Day$formatted", previousState) +} diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..e71780d --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version = 1.2.6 \ No newline at end of file diff --git a/src/main/scala/adventofcode/Day.scala b/src/main/scala/adventofcode/Day.scala new file mode 100644 index 0000000..40cba1f --- /dev/null +++ b/src/main/scala/adventofcode/Day.scala @@ -0,0 +1,64 @@ +package adventofcode + +import java.io.PrintWriter + +import scala.io.Source +import scala.util.{Failure, Success, Try} + +abstract class Day(day: Int) extends App { + + sealed abstract class SubProblem(val name: String) + case object A extends SubProblem("A") + case object B extends SubProblem("B") + + require(day >= 1 && day <= 25, "Invalid day number") + + protected val lineSeparator: String = "\n" + + private val formatted = "%02d".format(day) + + private def readInput(): String = Source.fromFile(s"input/$formatted.txt").getLines().mkString(lineSeparator) + + private def writeOutput(output: String, subProblem: SubProblem): Unit = { + val sub = subProblem.name + new PrintWriter(s"output/$formatted$sub.txt") { + write(output) + close() + } + } + + protected val input: String = readInput() + protected val lines: IndexedSeq[String] = input.split(lineSeparator).toIndexedSeq + + + def solutionA: Any + def solutionB: Any + + + // To be called at the end + + var submitted: Boolean = false + + protected def submit(): Unit = { + assert(!submitted, "Solution has already been submitted") + submitted = true + + submitSolution(solutionA, A) + submitSolution(solutionB, B) + } + + private def submitSolution(function: => Any, subProblem: SubProblem): Unit = { + Try(function.toString) match { + case Success(string) => + writeOutput(string, subProblem) + println(string) + println() + case Failure(_: NotImplementedError) => + println(s"(ignored solution ${subProblem.name})") + println() + case Failure(exception) => + throw new RuntimeException(exception) + } + } + +} diff --git a/src/main/scala/adventofcode/solutions/Day01.scala b/src/main/scala/adventofcode/solutions/Day01.scala new file mode 100644 index 0000000..6bbd6a2 --- /dev/null +++ b/src/main/scala/adventofcode/solutions/Day01.scala @@ -0,0 +1,12 @@ +package adventofcode.solutions + +import adventofcode.Day + +object Day01 extends Day(1) { + + override def solutionA = ??? + + override def solutionB = ??? + + submit() +}