-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from scalacenter/setup-2023
setup solutions infrastructure for 2023
- Loading branch information
Showing
7 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <dayX>.<partX> | ||
``` | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
//> using scala 3.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters