Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions 2016/src/main/scala/aoc2016/Day23.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object Day23 extends AoC:
def valueOf(operand: Operand): Value =
operand match
case r: Register => registers.getOrElse(r, 0)
case i: Int => i
case v: Value => v

def update(register: Operand, operand: Operand, f: Value => Value = identity): Registers =
if register.isRegister then
Expand All @@ -46,10 +46,10 @@ object Day23 extends AoC:
// Part 2
case MUL(x: Operand, y: Operand, z: Operand)

object Instruction:
extension (s: String)
def toOperand: Operand = Try(s.toInt).getOrElse(s)

extension (s: String)
def toOperand: Operand = Try(s.toInt).getOrElse(s)
object Instruction:

def line(s: String): Instruction =
s match
Expand Down
32 changes: 3 additions & 29 deletions 2016/src/main/scala/aoc2016/Day25.scala
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
package aoc2016

import nmcb.*
import scala.util.Try

object Day25 extends AoC:

type Value = Int
type Register = String
type Operand = Value | Register
type Registers = Map[Register, Value]

extension (operand: Operand)
// Shut up! I know what I'm doing.
def isRegister: Boolean = operand.isInstanceOf[Register]
def toRegister: Register = operand.asInstanceOf[Register]

extension (registers: Registers)

def valueOf(operand: Operand): Value =
operand match
case r: Register => registers.getOrElse(r, 0)
case i: Int => i

def update(register: Operand, operand: Operand, f: Value => Value = identity): Registers =
if register.isRegister then
val value = (f compose registers.valueOf)(operand)
registers.updated(register.toRegister, value)
else
registers
import Day23.*

enum Instruction:
case CPY(x: Operand, y: Operand)
Expand All @@ -38,9 +15,6 @@ object Day25 extends AoC:

object Instruction:

extension (s: String)
def toOperand: Operand = Try(s.toInt).getOrElse(s)

def fromString(s: String): Instruction =
s match
case s"cpy $x $y" => CPY(x.toOperand, y.toOperand)
Expand Down Expand Up @@ -84,9 +58,9 @@ object Day25 extends AoC:
.take(size)
.mkString

def solve1(instructions: Vector[Instruction]): Int =
def solve(instructions: Vector[Instruction]): Int =
val sampleSize = 32
val sample = List.tabulate(sampleSize)(n => if n % 2 == 0 then '0' else '1').mkString
Iterator.from(0).map(experiment(instructions, sampleSize)).indexWhere(_ == sample)

override lazy val answer1: Int = solve1(instructions)
override lazy val answer1: Int = solve(instructions)
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ThisBuild / scalacOptions ++= Seq(
"-language:existentials",
"-language:strictEquality",
"-unchecked",
"-Werror",
// "-Werror",
"-deprecation"
)

Expand Down