Skip to content

Commit

Permalink
Merge pull request #257 from scalacenter/setup-2023
Browse files Browse the repository at this point in the history
setup solutions infrastructure for 2023
  • Loading branch information
bishabosha authored Nov 21, 2023
2 parents 6ff9cea + 8adda7b commit b340794
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 10 deletions.
53 changes: 53 additions & 0 deletions 2022/README.md
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.
1 change: 1 addition & 0 deletions 2023/input/day01
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
???
1 change: 1 addition & 0 deletions 2023/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//> using scala 3.3.1
20 changes: 20 additions & 0 deletions 2023/src/day01.scala
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
9 changes: 9 additions & 0 deletions 2023/src/inputs.scala
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)
19 changes: 19 additions & 0 deletions 2023/src/locations.scala
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]
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -38,16 +39,16 @@ 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
```

Or, to run another solution:
```
$ scala-cli 2022 -M <dayX>.<partX>
$ scala-cli 2023 -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.
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.

0 comments on commit b340794

Please sign in to comment.