-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay16.scala
137 lines (111 loc) · 6.08 KB
/
Day16.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.lmat.adventofcode.year2018
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.adventofcode.year2018.Day16Definitions._
import com.lmat.util.Files.readResource
import scala.annotation.tailrec
import scala.util.Try
object Day16Definitions {
case class InstructionTemplate(opcode: Int, inputL: Int, inputR: Int, output: Int)
case class CpuSample(before: Seq[Int], instruction: InstructionTemplate, after: Seq[Int])
case class Input(cpuSamples: Seq[CpuSample], instructions: Seq[InstructionTemplate])
case class Instruction(name: String, a: Int, b: Int, c: Int)
}
object Day16 extends SimpleCommonPuzzle[Input, Int, Int] {
override def parse(resource: String): Input = parseInput(readResource(resource))
def parseInput(rows: Seq[String]): Input = {
val (cpuSamples, instructions) = rows.sliding(4, 4).span { case h +: _ => h.nonEmpty }
Input(cpuSamples.flatMap(parseCpuSample).toSeq, instructions.flatten.flatMap(parseInstruction).toSeq)
}
def parseCpuSample(rows: Seq[String]): Option[CpuSample] = rows match {
case beforeS +: instructionS +: afterS +: _ => for {
before <- parseState(beforeS, "Before")
instruction <- parseInstruction(instructionS)
after <- parseState(afterS, "After")
} yield CpuSample(before, instruction, after)
}
def parseState(row: String, time: String): Option[Seq[Int]] = {
val pattern = s"$time:.*?\\[(.*?)\\]".r
row match {
case pattern(valuesS) => Some(valuesS.split(", ").toIndexedSeq.flatMap(s => Try(s.toInt).toOption))
case _ => None
}
}
def parseInstruction(row: String): Option[InstructionTemplate] = row.split(" ").toSeq match {
case oS +: aS +: bS +: cS +: Seq() => (for {
o <- Try(oS.toInt)
a <- Try(aS.toInt)
b <- Try(bS.toInt)
c <- Try(cS.toInt)
} yield InstructionTemplate(o, a, b, c)).toOption
case _ => None
}
lazy val instructionNames: Set[String] =
Set(
"addr", "addi",
"mulr", "muli",
"banr", "bani",
"borr", "bori",
"setr", "seti",
"gtir", "gtri", "gtrr",
"eqir", "eqri", "eqrr")
override def part1(input: Input): Int =
input.cpuSamples.count(matchingInstructions(instructionNames)(_).size >= 3)
def matchingInstructions(instructionNames: Set[String])(cpuSample: CpuSample): Set[Instruction] = {
val instructions = toInstructions(instructionNames)(cpuSample.instruction)
instructions.filter(instruction => applyInstruction(instruction, cpuSample.before) == cpuSample.after)
}
def toInstructions(instructions: Set[String])(instructionTemplate: InstructionTemplate): Set[Instruction] = {
val InstructionTemplate(_, inputL, inputR, output) = instructionTemplate
instructions.map(name => Instruction(name, inputL, inputR, output))
}
def applyInstruction(instruction: Instruction, state: Seq[Int]): Seq[Int] = {
val stateMap: Map[Int, Int] = state.zipWithIndex.map{ case (v, i) => (i, v)}.toMap
applyInstruction(instruction, stateMap).toSeq.sortBy(_._1).map(_._2)
}
def applyInstruction(instruction: Instruction, state: Map[Int, Int]): Map[Int, Int] = {
def register(name: Int): Int = state.getOrElse(name, 0)
instruction match {
case Instruction("addr", aR, bR, cR) => state.updated(cR, register(aR) + register(bR))
case Instruction("addi", aR, b, cR) => state.updated(cR, register(aR) + b)
case Instruction("mulr", aR, bR, cR) => state.updated(cR, register(aR) * register(bR))
case Instruction("muli", aR, b, cR) => state.updated(cR, register(aR) * b)
case Instruction("banr", aR, bR, cR) => state.updated(cR, register(aR) & register(bR))
case Instruction("bani", aR, b, cR) => state.updated(cR, register(aR) & b)
case Instruction("borr", aR, bR, cR) => state.updated(cR, register(aR) | register(bR))
case Instruction("bori", aR, b, cR) => state.updated(cR, register(aR) | b)
case Instruction("setr", aR, _, cR) => state.updated(cR, register(aR))
case Instruction("seti", a, _, cR) => state.updated(cR, a)
case Instruction("gtir", a, bR, cR) => state.updated(cR, if(a > register(bR)) 1 else 0)
case Instruction("gtri", aR, b, cR) => state.updated(cR, if(register(aR) > b) 1 else 0)
case Instruction("gtrr", aR, bR, cR) => state.updated(cR, if(register(aR) > register(bR)) 1 else 0)
case Instruction("eqir", a, bR, cR) => state.updated(cR, if(a == register(bR)) 1 else 0)
case Instruction("eqri", aR, b, cR) => state.updated(cR, if(register(aR) == b) 1 else 0)
case Instruction("eqrr", aR, bR, cR) => state.updated(cR, if(register(aR) == register(bR)) 1 else 0)
case _ => throw new IllegalArgumentException(s"Illegal instruction: $instruction")
}
}
override def part2(input: Input): Int = {
val opCodeMap = buildOpCodeMap(input.cpuSamples)
val instructions = input.instructions.map(ins => Instruction(opCodeMap(ins.opcode), ins.inputL, ins.inputR, ins.output))
val result = instructions.foldLeft(Map[Int, Int]())((state, ins) => applyInstruction(ins, state))
result.getOrElse(0, 0)
}
/**
* Luckily given the input we can build the opcode to instruction name map recursively
* By finding a Cpu sample that can only be satisfied by one instruction
* And then removing that instruction and all relevant samples from the pool
*/
def buildOpCodeMap(cpuSamples: Seq[CpuSample]): Map[Int, String] = {
@tailrec
def iterate(remainingSamples: Set[CpuSample], remainingInstructions: Set[String], builtMap: Map[Int, String]): Map[Int, String] =
if(remainingInstructions.isEmpty) builtMap
else {
remainingSamples.map(sample => (sample, matchingInstructions(remainingInstructions)(sample).map(_.name))).find(_._2.size == 1) match {
case Some((CpuSample(_, InstructionTemplate(opcode, _, _, _), _), instructions)) =>
iterate(remainingSamples.filterNot(_.instruction.opcode == opcode), remainingInstructions -- instructions, builtMap.updated(opcode, instructions.head))
case None => builtMap
}
}
iterate(cpuSamples.toSet, instructionNames, Map())
}
}