Compare commits
2 Commits
b8d44e8b20
...
fa7e76ae40
Author | SHA1 | Date |
---|---|---|
aru | fa7e76ae40 | |
aru | 752b6e67cf |
|
@ -0,0 +1 @@
|
|||
use flake
|
|
@ -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/
|
|
@ -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).
|
|
@ -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
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
sbt.version=1.9.9
|
|
@ -0,0 +1,55 @@
|
|||
import scala.collection.immutable.Vector
|
||||
import scala.io.Source
|
||||
import scala.annotation.tailrec
|
||||
|
||||
case class UnknownInstruction() extends Exception
|
||||
case class SolutionNotFound() extends Exception
|
||||
|
||||
@main def hello(): Unit =
|
||||
val input: Vector[Int] = Source.fromFile("input").getLines().next().split(",").map(_.toInt).to(Vector)
|
||||
|
||||
printf("Part 1: %d\n", solve1(input))
|
||||
printf("Part 2: %d\n", solve2(input))
|
||||
|
||||
def step(op: Int, memory : Vector[Int]): (Option[Int], Vector[Int]) =
|
||||
memory(op) match
|
||||
case 1 | 2 => (Some(op + 4), binary_op(op, memory))
|
||||
case 99 => (None, memory)
|
||||
case _ => throw new UnknownInstruction
|
||||
|
||||
def run(memory: Vector[Int]): Vector[Int] =
|
||||
@tailrec
|
||||
def inner_run(state: (Option[Int], Vector[Int])): Vector[Int] = state match {
|
||||
case (None, memory) => memory
|
||||
case (Some(ip), memory) => inner_run(step(ip, memory))
|
||||
}
|
||||
|
||||
inner_run(Some(0), memory)
|
||||
|
||||
def binary_op(op: Int, memory: Vector[Int]): Vector[Int] =
|
||||
val o1 = indirect(op + 1, memory)
|
||||
val o2 = indirect(op + 2, memory)
|
||||
val res = memory(op) match {
|
||||
case 1 => o1 + o2
|
||||
case 2 => o1 * o2
|
||||
case _ => throw new UnknownInstruction
|
||||
}
|
||||
memory.updated(memory(op + 3), res)
|
||||
|
||||
def indirect(address: Int, memory: Vector[Int]): Int =
|
||||
memory(memory(address))
|
||||
|
||||
def run_modified(m1: Int, m2: Int, memory: Vector[Int]): Int =
|
||||
run(memory.updated(1, m1).updated(2, m2))(0)
|
||||
|
||||
def solve1(memory: Vector[Int]): Int =
|
||||
run_modified(12, 2, memory)
|
||||
|
||||
def solve2(memory: Vector[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) == 19690720) match
|
||||
case Some(i, j) => 100 * i + j
|
||||
case None => throw new SolutionNotFound
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
import scala.collection.immutable.Vector
|
||||
|
||||
// For more information on writing tests, see
|
||||
// https://scalameta.org/munit/docs/getting-started.html
|
||||
class MySuite extends munit.FunSuite {
|
||||
test("step") {
|
||||
val in = Vector(1,0,0,0,99)
|
||||
val (next, out) = step(0, in)
|
||||
assertEquals(next, Some(4))
|
||||
assertEquals(out, Vector(2,0,0,0,99))
|
||||
}
|
||||
|
||||
test("step") {
|
||||
val in = Vector(2,3,0,3,99)
|
||||
val (next, out) = step(0, in)
|
||||
assertEquals(next, Some(4))
|
||||
assertEquals(out, Vector(2,3,0,6,99))
|
||||
}
|
||||
|
||||
test("step") {
|
||||
val in = Vector(2,4,4,5,99,0)
|
||||
val (next, out) = step(0, in)
|
||||
assertEquals(next, Some(4))
|
||||
assertEquals(out, Vector(2,4,4,5,99,9801))
|
||||
}
|
||||
|
||||
test("step") {
|
||||
val in = Vector(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, Vector(30,1,1,4,2,5,6,0,99))
|
||||
}
|
||||
|
||||
test("run") {
|
||||
val in = Vector(1,1,1,4,99,5,6,0,99)
|
||||
val out = run(in)
|
||||
assertEquals(out, Vector(30,1,1,4,2,5,6,0,99))
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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 ];
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue