Day 10
This commit is contained in:
parent
c3027abccc
commit
c3919a1e57
2 changed files with 192 additions and 0 deletions
143
aoc/resources/day10Input.txt
Normal file
143
aoc/resources/day10Input.txt
Normal file
|
|
@ -0,0 +1,143 @@
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -5
|
||||||
|
addx 6
|
||||||
|
addx 3
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -38
|
||||||
|
addx 41
|
||||||
|
addx -22
|
||||||
|
addx -14
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx -12
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -16
|
||||||
|
addx 17
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -30
|
||||||
|
noop
|
||||||
|
addx -6
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -12
|
||||||
|
addx 17
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx 10
|
||||||
|
addx -9
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -5
|
||||||
|
addx 6
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -37
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx -12
|
||||||
|
addx 30
|
||||||
|
addx -23
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -17
|
||||||
|
addx 22
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
addx -10
|
||||||
|
addx 11
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -2
|
||||||
|
noop
|
||||||
|
addx -6
|
||||||
|
addx -29
|
||||||
|
addx 37
|
||||||
|
addx -30
|
||||||
|
addx 27
|
||||||
|
addx -2
|
||||||
|
addx -22
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx -5
|
||||||
|
addx 6
|
||||||
|
addx 2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx -25
|
||||||
|
noop
|
||||||
|
addx -10
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
49
aoc/src/Day10.scala
Normal file
49
aoc/src/Day10.scala
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import scala.io.Source
|
||||||
|
|
||||||
|
object Day10 extends App:
|
||||||
|
|
||||||
|
val input = Source
|
||||||
|
.fromURL(getClass.getResource("day10Input.txt"))
|
||||||
|
.mkString
|
||||||
|
.split('\n')
|
||||||
|
.toList
|
||||||
|
.map(_.split(" ")).map {
|
||||||
|
case Array(inst, value) => (inst, value.toInt)
|
||||||
|
case Array(inst) => (inst, 0)
|
||||||
|
}
|
||||||
|
.flatMap {
|
||||||
|
case inst @ ("noop", _) => List(inst)
|
||||||
|
case inst @ ("addx", _) => List(("noop", 0), inst)
|
||||||
|
}
|
||||||
|
|
||||||
|
val execution = input.scanLeft((1, 1)) {
|
||||||
|
case ((register, cycle), ("noop", _)) => (register, cycle + 1)
|
||||||
|
case ((register, cycle), ("addx", value)) => (register + value, cycle + 1)
|
||||||
|
case ((register, cycle), _) => (register, cycle + 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
val interestingSignals =
|
||||||
|
(20 to execution.last._2).filter(cycle => cycle == 20 || (cycle + 20) % 40 == 0)
|
||||||
|
|
||||||
|
val part1 = execution
|
||||||
|
.filter(state => interestingSignals.contains(state._2))
|
||||||
|
.map(_._1)
|
||||||
|
.zip(interestingSignals)
|
||||||
|
.map(_ * _)
|
||||||
|
.sum
|
||||||
|
println(s"Part 1: $part1")
|
||||||
|
|
||||||
|
def renderRow(rowExecution: List[(Int, Int)]): String =
|
||||||
|
rowExecution.foldLeft("") { case (line, (register, cycle)) =>
|
||||||
|
val spritePosition = register - 1 to register + 1
|
||||||
|
if spritePosition.contains((cycle - 1) % 40) then line + "#"
|
||||||
|
else line + "."
|
||||||
|
}
|
||||||
|
|
||||||
|
val part2 = execution
|
||||||
|
.grouped(40)
|
||||||
|
.map(renderRow)
|
||||||
|
.mkString("\n")
|
||||||
|
println(s"Part 2:")
|
||||||
|
println(part2)
|
||||||
|
end Day10
|
||||||
Loading…
Add table
Add a link
Reference in a new issue