adamratzman
12/08/2020, 4:41 AMDavid Whittaker
12/08/2020, 5:14 AMwhen
to the rescue yet again!!adamratzman
12/08/2020, 5:15 AMadamratzman
12/08/2020, 5:16 AMprivate val input = readInput("input8.txt")
data class Instruction(val name: String, val argument: Int)
fun main() {
val instructions = input.split("\n").map { it.split(" ") }.map { Instruction(it[0], it[1].toInt()) }
fun part1(): Int {
var accumulator = 0
var currentLine = 0
val linesRun = mutableListOf<Int>()
while (currentLine < instructions.size && currentLine !in linesRun) {
linesRun += currentLine
val instruction = instructions[currentLine]
when (instruction.name) {
"acc" -> {
accumulator += instruction.argument
currentLine++
}
"nop" -> currentLine++
"jmp" -> currentLine += instruction.argument
}
}
return accumulator
}
println("Part 1: ${part1()}")
fun part2() {
instructions
.mapIndexed { index, instruction -> index to instruction }
.filter { it.second.name in listOf("jmp", "nop") }
.map { it.first }.forEach { a ->
var accumulator = 0
val testInstructions = instructions.toMutableList().apply {
val oldInstruction = this[a]
this[a] = oldInstruction.copy(name = if (oldInstruction.name == "jmp") "nop" else "jmp")
}
var currentLine = 0
val linesRun = mutableListOf<Int>()
while (currentLine < testInstructions.size && currentLine !in linesRun) {
val instruction = testInstructions[currentLine]
linesRun += currentLine
when (instruction.name) {
"acc" -> {
accumulator += instruction.argument
currentLine++
}
"nop" -> currentLine++
"jmp" -> currentLine += instruction.argument
}
}
if (currentLine == testInstructions.size) {
println("Part 2: $accumulator")
return@forEach
}
}
}
part2()
}
adamratzman
12/08/2020, 5:17 AMDavid Whittaker
12/08/2020, 5:22 AMadamratzman
12/08/2020, 5:23 AMDavid Whittaker
12/08/2020, 5:26 AMinput
shadowing your outer input
? lol -- naming is hard, naming fast is super hardNir
12/08/2020, 5:38 AMadamratzman
12/08/2020, 5:40 AMEdgars
12/08/2020, 7:55 AMJoris PZ
12/08/2020, 8:22 AM| Platform | Average (ms) | Measurements (ms) |
| -----------------| ------------:|------------------:|
| GraalVM | 8.7±9.5 | `47, 15, 16, 7, 5, 5, 3, 4, 5, 6, 5, 5, 7, 5, 5, 5, 5, 6, 6, 4` |
| Node JS | 23.3±14.1 | `81, 37, 20, 22, 24, 17, 20, 17, 18, 23, 15, 18, 17, 18, 15, 16, 16, 20, 19, 23` |
| Native | 124±17 | `106, 134, 141, 112, 136, 105, 139, 106, 134, 107, 153, 108, 128, 135, 117, 119, 154, 90, 118, 124` |
ephemient
12/08/2020, 10:44 AMNir
12/08/2020, 1:26 PMEdgars
12/08/2020, 1:29 PMNir
12/08/2020, 1:44 PMNir
12/08/2020, 1:45 PMEdgars
12/08/2020, 1:45 PMEdgars
12/08/2020, 1:47 PMdetect loop
pointer--
replace instruction if necessary // decrement accumulator it's an acc
run code
if detects loop, put the original instruction back, pointer--, try again
else hooray!
Edgars
12/08/2020, 1:47 PMEdgars
12/08/2020, 1:49 PMNir
12/08/2020, 2:01 PMNir
12/08/2020, 2:03 PMEdgars
12/08/2020, 2:06 PMjmp
will jump you into a loop that you otherwise wouldn't have got to.todd.ginsberg
12/08/2020, 2:10 PMephemient
12/08/2020, 2:11 PMtodd.ginsberg
12/08/2020, 2:17 PMfun solvePart2(): Int =
instructions
.indices
.asSequence()
.mapNotNull { index -> instructions.flipIndexOrNull(index) }
.mapNotNull { inst ->
Computer(inst).run {
if (runUntilTerminate() == Computer.ExecutionState.TERMINATED) accumulator
else null
}
}.first()
Nir
12/08/2020, 4:20 PMNir
12/08/2020, 4:20 PMEdgars
12/08/2020, 4:25 PMNir
12/08/2020, 4:35 PMNir
12/08/2020, 4:36 PMEdgars
12/08/2020, 4:39 PMephemient
12/08/2020, 4:41 PMNir
12/08/2020, 5:01 PMNir
12/08/2020, 5:01 PMNir
12/08/2020, 5:01 PMephemient
12/08/2020, 5:02 PMNir
12/08/2020, 5:05 PMephemient
12/08/2020, 5:31 PMNir
12/08/2020, 5:53 PMtodd.ginsberg
12/08/2020, 6:02 PMbjonnh
12/08/2020, 6:14 PMbjonnh
12/08/2020, 6:15 PMbjonnh
12/08/2020, 6:15 PMbjonnh
12/08/2020, 6:15 PMMarcin Wisniowski
12/08/2020, 6:16 PMbjonnh
12/08/2020, 6:21 PMbjonnh
12/08/2020, 6:21 PMbjonnh
12/08/2020, 6:21 PMNir
12/08/2020, 6:22 PMNir
12/08/2020, 6:23 PMbjonnh
12/08/2020, 6:23 PMbjonnh
12/08/2020, 6:23 PMNir
12/08/2020, 6:24 PMbjonnh
12/08/2020, 6:24 PMNir
12/08/2020, 6:24 PMbjonnh
12/08/2020, 6:25 PMbjonnh
12/08/2020, 6:25 PMbjonnh
12/08/2020, 6:26 PMbjonnh
12/08/2020, 6:26 PMEdgars
12/08/2020, 6:26 PMbjonnh
12/08/2020, 6:26 PMbjonnh
12/08/2020, 6:27 PMbjonnh
12/08/2020, 6:27 PMbjonnh
12/08/2020, 6:27 PMEdgars
12/08/2020, 6:27 PMbjonnh
12/08/2020, 6:28 PMbjonnh
12/08/2020, 6:28 PMbjonnh
12/08/2020, 6:29 PMbjonnh
12/08/2020, 6:29 PMEdgars
12/08/2020, 6:29 PMbjonnh
12/08/2020, 6:30 PM% time ./gradlew run
> Task :run
(1859, 1235)
BUILD SUCCESSFUL in 528ms
2 actionable tasks: 1 executed, 1 up-to-date
./gradlew run 0.84s user 0.05s system 98% cpu 0.902 total
bjonnh
12/08/2020, 6:30 PMbjonnh
12/08/2020, 6:32 PMtime ./bin/advent-of-code
(1859, 1235)
./bin/advent-of-code 0.17s user 0.04s system 127% cpu 0.170 total
bjonnh
12/08/2020, 6:34 PMtime ./bin/advent-of-code
(1859, 1235)
./bin/advent-of-code 0.15s user 0.03s system 105% cpu 0.179 total
bjonnh
12/08/2020, 6:36 PMbjonnh
12/08/2020, 6:39 PMbjonnh
12/08/2020, 6:39 PMbjonnh
12/08/2020, 6:39 PMbjonnh
12/08/2020, 6:49 PMbjonnh
12/08/2020, 6:49 PMbjonnh
12/08/2020, 6:50 PMNir
12/08/2020, 6:51 PMNir
12/08/2020, 6:51 PMbjonnh
12/08/2020, 6:52 PMbjonnh
12/08/2020, 6:52 PMbjonnh
12/08/2020, 6:52 PMNir
12/08/2020, 6:52 PMNir
12/08/2020, 6:53 PMbjonnh
12/08/2020, 6:53 PMbjonnh
12/08/2020, 6:53 PMbjonnh
12/08/2020, 6:53 PMbjonnh
12/08/2020, 6:54 PMbjonnh
12/08/2020, 6:54 PMexport JAVA_OPTS="-Xshare:on -XX:SharedArchiveFile=classes.jsa -XX:+UnlockExperimentalVMOptions -XX:AOTLibrary=./java_base.so " ; time ./bin/advent-of-code
Helloworld
./bin/advent-of-code 0.03s user 0.02s system 106% cpu 0.049 total
bjonnh
12/08/2020, 6:54 PMbjonnh
12/08/2020, 6:55 PMbjonnh
12/08/2020, 6:57 PM/tmp/o 0.00s user 0.00s system 77% cpu 0.002 total
bjonnh
12/08/2020, 6:57 PMbjonnh
12/08/2020, 6:58 PMpython /tmp/o.py 0.02s user 0.01s system 96% cpu 0.021 total
bjonnh
12/08/2020, 6:58 PMNir
12/08/2020, 6:58 PMbjonnh
12/08/2020, 6:58 PMbjonnh
12/08/2020, 6:58 PMbjonnh
12/08/2020, 6:58 PMbjonnh
12/08/2020, 6:59 PMNir
12/08/2020, 6:59 PMNir
12/08/2020, 6:59 PMbjonnh
12/08/2020, 6:59 PMbjonnh
12/08/2020, 6:59 PMbjonnh
12/08/2020, 7:16 PMbjonnh
12/08/2020, 7:16 PMbjonnh
12/08/2020, 7:16 PMbjonnh
12/08/2020, 7:16 PMbjonnh
12/08/2020, 7:21 PMbjonnh
12/08/2020, 7:22 PMbjonnh
12/08/2020, 7:22 PMNir
12/08/2020, 7:22 PMNir
12/08/2020, 7:22 PMephemient
12/08/2020, 7:22 PM$ cc -nostdlib -ohello hello.S
$ time ./hello
Hello, world!
0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 344maxresident)k
0inputs+0outputs (0major+27minor)pagefaults 0swaps
bjonnh
12/08/2020, 7:23 PMbjonnh
12/08/2020, 7:27 PMbjonnh
12/08/2020, 7:28 PM114.79 msec task-clock # 1.517 CPUs utilized ( +- 2.38% )
430 context-switches # 0.004 M/sec ( +- 0.73% )
12 cpu-migrations # 0.104 K/sec ( +- 3.51% )
6,018 page-faults # 0.052 M/sec ( +- 0.29% )
457,879,005 cycles # 3.989 GHz ( +- 0.51% )
499,379,616 instructions # 1.09 insn per cycle ( +- 0.17% )
96,479,627 branches # 840.454 M/sec ( +- 0.17% )
4,003,302 branch-misses # 4.15% of all branches ( +- 0.24% )
0.07567 +- 0.00199 seconds time elapsed ( +- 2.63% )
bjonnh
12/08/2020, 7:28 PM