diff --git a/2022/README.md b/2022/README.md new file mode 100644 index 000000000..d61b2d783 --- /dev/null +++ b/2022/README.md @@ -0,0 +1,53 @@ +# Scala Advent of Code 2022 + +> See earlier editions: +> - [2021](/2021/README.md) + +## Website + +The [Scala Advent of Code](https://scalacenter.github.io/scala-advent-of-code/) website contains: +- some explanation of our solutions +- more solutions from the community + +## Setup + +We use Visual Studio Code with Metals to write Scala code, and scala-cli to compile and run it. + +You can follow these [steps](https://scalacenter.github.io/scala-advent-of-code/setup) to set up your environement. + +### How to open in Visual Studio Code + +After you clone the repository, open a terminal and run: +``` +$ cd scala-advent-of-code +$ scala-cli setup-ide 2022 +$ code 2022 +``` + +`code 2022` will open Visual Studio Code and start Metals. If not you may have to go to the Metals pane and click +the button labelled "Start Metals". + +When you navigate to a file, e.g. `2022/src/day01.scala` metals should index the project, and then display code lenses +above each of the main methods `part1` and `part2`, as shown in this image: +![](img/code-lenses.png) + +You can click `run` to see the results of the program run in VS Code. Or `debug`, +which will let you pause on breakpoints, and execute expressions in the debug console. + +### How to run a solution + +In a terminal you can run: +``` +$ scala-cli 2022 -M day01.part01 +Compiling project (Scala 3.x.y, JVM) +Compiled project (Scala 3.x.y, JVM) +The solution is 64929 +``` + +Or, to run another solution: +``` +$ scala-cli 2022 -M . +``` + +By default the solution programs run on our input files which are stored in the `2022/input` folder. +To get your solutions you can change the content of those files in the `2022/input` folder. diff --git a/2023/input/day01 b/2023/input/day01 new file mode 100644 index 000000000..be108529a --- /dev/null +++ b/2023/input/day01 @@ -0,0 +1 @@ +??? diff --git a/2023/project.scala b/2023/project.scala new file mode 100644 index 000000000..00c059a29 --- /dev/null +++ b/2023/project.scala @@ -0,0 +1 @@ +//> using scala 3.3.1 diff --git a/2023/src/day01.scala b/2023/src/day01.scala new file mode 100644 index 000000000..f6cb9c73c --- /dev/null +++ b/2023/src/day01.scala @@ -0,0 +1,20 @@ +package day01 + +import locations.Directory.currentDir +import inputs.Input.loadFileSync + +@main def part1: Unit = + println(s"The solution is ${part1(loadInput())}") + +@main def part2: Unit = + println(s"The solution is ${part2(loadInput())}") + +def loadInput(): String = loadFileSync(s"$currentDir/../input/day01") + +def part1(input: String): String = + val text = input.linesIterator.mkString + text.ensuring(text == "???") // TODO: fill in when day 1 is released + +def part2(input: String): String = + val text = input.linesIterator.mkString + text.ensuring(text == "???") // TODO: fill in when day 1 is released diff --git a/2023/src/inputs.scala b/2023/src/inputs.scala new file mode 100644 index 000000000..1d41e4738 --- /dev/null +++ b/2023/src/inputs.scala @@ -0,0 +1,9 @@ +package inputs + +import scala.util.Using +import scala.io.Source + +object Input: + + def loadFileSync(path: String): String = + Using.resource(Source.fromFile(path))(_.mkString) diff --git a/2023/src/locations.scala b/2023/src/locations.scala new file mode 100644 index 000000000..986d2a3bb --- /dev/null +++ b/2023/src/locations.scala @@ -0,0 +1,19 @@ +package locations + +import scala.quoted.* + +object Directory: + + /** The absolute path of the parent directory of the file that calls this method + * This is stable no matter which directory runs the program. + */ + inline def currentDir: String = ${ parentDirImpl } + + private def parentDirImpl(using Quotes): Expr[String] = + // position of the call to `currentDir` in the source code + val position = quotes.reflect.Position.ofMacroExpansion + // get the path of the file calling this macro + val srcFilePath = position.sourceFile.getJPath.get + // get the parent of the path, which is the directory containing the file + val parentDir = srcFilePath.getParent().toAbsolutePath + Expr(parentDir.toString) // convert the String to Expr[String] diff --git a/README.md b/README.md index d61b2d783..a7e0168b6 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,13 @@ -# Scala Advent of Code 2022 +# Scala Advent of Code 2023 > See earlier editions: > - [2021](/2021/README.md) +> - [2022](/2022/README.md) ## Website The [Scala Advent of Code](https://scalacenter.github.io/scala-advent-of-code/) website contains: -- some explanation of our solutions +- some explanation of our solutions to Advent of Code (adventofcode.com) - more solutions from the community ## Setup @@ -20,14 +21,14 @@ You can follow these [steps](https://scalacenter.github.io/scala-advent-of-code/ After you clone the repository, open a terminal and run: ``` $ cd scala-advent-of-code -$ scala-cli setup-ide 2022 -$ code 2022 +$ scala-cli setup-ide 2023 +$ code 2023 ``` -`code 2022` will open Visual Studio Code and start Metals. If not you may have to go to the Metals pane and click +`code 2023` will open Visual Studio Code and start Metals. If not you may have to go to the Metals pane and click the button labelled "Start Metals". -When you navigate to a file, e.g. `2022/src/day01.scala` metals should index the project, and then display code lenses +When you navigate to a file, e.g. `2023/src/day01.scala` metals should index the project, and then display code lenses above each of the main methods `part1` and `part2`, as shown in this image: ![](img/code-lenses.png) @@ -38,7 +39,7 @@ which will let you pause on breakpoints, and execute expressions in the debug co In a terminal you can run: ``` -$ scala-cli 2022 -M day01.part01 +$ scala-cli 2023 -M day01.part01 Compiling project (Scala 3.x.y, JVM) Compiled project (Scala 3.x.y, JVM) The solution is 64929 @@ -46,8 +47,8 @@ The solution is 64929 Or, to run another solution: ``` -$ scala-cli 2022 -M . +$ scala-cli 2023 -M . ``` -By default the solution programs run on our input files which are stored in the `2022/input` folder. -To get your solutions you can change the content of those files in the `2022/input` folder. +By default the solution programs run on our input files which are stored in the `2023/input` folder. +To get your solutions you can change the content of those files in the `2023/input` folder.