2019 day 2 in Scala

This commit is contained in:
aru 2024-04-05 16:54:29 +02:00
parent b8d44e8b20
commit 752b6e67cf
9 changed files with 233 additions and 0 deletions

1
2019/scala/02/.envrc Normal file
View File

@ -0,0 +1 @@
use flake

32
2019/scala/02/day02/.gitignore vendored Normal file
View File

@ -0,0 +1,32 @@
# macOS
.DS_Store
# sbt specific
dist/*
target/
lib_managed/
src_managed/
project/boot/
project/plugins/project/
project/local-plugins.sbt
.history
.ensime
.ensime_cache/
.sbt-scripted/
local.sbt
# Bloop
.bsp
# VS Code
.vscode/
# Metals
.bloop/
.metals/
metals.sbt
# IDEA
.idea
.idea_modules
/.worksheet/

View File

@ -0,0 +1,8 @@
## sbt project compiled with Scala 3
### Usage
This is a normal sbt project. You can compile code with `sbt compile`, run it with `sbt run`, and `sbt console` will start a Scala 3 REPL.
For more information on the sbt-dotty plugin, see the
[scala3-example-project](https://github.com/scala/scala3-example-project/blob/main/README.md).

View File

@ -0,0 +1,12 @@
val scala3Version = "3.4.0"
lazy val root = project
.in(file("."))
.settings(
name := "Day02",
version := "0.1.0-SNAPSHOT",
scalaVersion := scala3Version,
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test
)

View File

@ -0,0 +1 @@
sbt.version=1.9.9

View File

@ -0,0 +1,57 @@
import scala.collection.mutable.ArrayBuffer
import scala.io.Source
case class UnknownInstruction() extends Exception
case class SolutionNotFound() extends Exception
@main def hello(): Unit =
val input: ArrayBuffer[Int] = Source.fromFile("input").getLines().next().split(",").map(_.toInt).to(ArrayBuffer)
printf("Part 1: %d\n", solve1(input.clone()))
printf("Part 2: %d\n", solve2(input))
def step(op: Int, memory : ArrayBuffer[Int]): (Option[Int], ArrayBuffer[Int]) =
memory(op) match
case 1 => (Some(op + 4), add(op, memory))
case 2 => (Some(op + 4), mul(op, memory))
case 99 => (None, memory)
case _ => throw new UnknownInstruction
def run(memory: ArrayBuffer[Int]): ArrayBuffer[Int] =
var op: Option[Int] = Some(0)
var mem: ArrayBuffer[Int] = memory
while(op.nonEmpty) {
val (nop, nmemory) = step(op.get, memory)
op = nop
mem = nmemory
}
mem
def add(op: Int, memory: ArrayBuffer[Int]): ArrayBuffer[Int] =
memory.update(memory(op + 3), indirect(op + 1, memory) + indirect(op + 2, memory))
memory
def mul(op: Int, memory: ArrayBuffer[Int]): ArrayBuffer[Int] =
memory.update(memory(op + 3), indirect(op + 1, memory) * indirect(op + 2, memory))
memory
def indirect(address: Int, memory: ArrayBuffer[Int]): Int =
memory(memory(address))
def run_modified(m1: Int, m2: Int, memory: ArrayBuffer[Int]): Int =
memory(1) = m1
memory(2) = m2
var done = run(memory)
done(0)
def solve1(memory: ArrayBuffer[Int]): Int =
run_modified(12, 2, memory)
def solve2(memory: ArrayBuffer[Int]): Int =
val options = for i <- 0 to 99
j <- 0 to 99
yield (i, j)
options.find((i, j) => run_modified(i, j, memory.clone()) == 19690720) match
case Some(i, j) => 100 * i + j
case None => throw new SolutionNotFound

View File

@ -0,0 +1,42 @@
import scala.collection.mutable.ArrayBuffer
// For more information on writing tests, see
// https://scalameta.org/munit/docs/getting-started.html
class MySuite extends munit.FunSuite {
test("step") {
val in = ArrayBuffer(1,0,0,0,99)
val (next, out) = step(0, in)
assertEquals(next, Some(4))
assertEquals(out, ArrayBuffer(2,0,0,0,99))
}
test("step") {
val in = ArrayBuffer(2,3,0,3,99)
val (next, out) = step(0, in)
assertEquals(next, Some(4))
assertEquals(out, ArrayBuffer(2,3,0,6,99))
}
test("step") {
val in = ArrayBuffer(2,4,4,5,99,0)
val (next, out) = step(0, in)
assertEquals(next, Some(4))
assertEquals(out, ArrayBuffer(2,4,4,5,99,9801))
}
test("step") {
val in = ArrayBuffer(1,1,1,4,99,5,6,0,99)
val (next, out) = step(0, in)
assertEquals(next, Some(4))
val (next2, out2) = step(4, out)
assertEquals(next2, Some(8))
assertEquals(out2, ArrayBuffer(30,1,1,4,2,5,6,0,99))
}
test("run") {
val in = ArrayBuffer(1,1,1,4,99,5,6,0,99)
val out = run(in)
assertEquals(out, ArrayBuffer(30,1,1,4,2,5,6,0,99))
}
}

60
2019/scala/02/flake.lock Normal file
View File

@ -0,0 +1,60 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1712330077,
"narHash": "sha256-Ny1Qm/K2fPUd7bylLEpn3/bfo+WJ0USnHm94z1NdNlA=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "2a7c323a364c12bd079922cdc4cbe3d9b15f2e03",
"type": "github"
},
"original": {
"owner": "nixos",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

20
2019/scala/02/flake.nix Normal file
View File

@ -0,0 +1,20 @@
{
description = "A flake for pulling in dependencies.";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in rec {
devShells.default = with pkgs; mkShell {
buildInputs = [ scala_3 sbt ];
};
}
);
}