Compare commits

...

3 Commits

Author SHA1 Message Date
0f8f8c3f19 day 9 puzzle 2 2023-12-09 11:59:01 +01:00
c3c9a8a4e4 day 9 puzzle 1 2023-12-09 11:53:51 +01:00
76427545d6 day 8 puzzle 2 (naive and not optimized) 2023-12-09 11:26:07 +01:00
10 changed files with 283 additions and 2 deletions

View File

@ -5,11 +5,11 @@ This repo contains my attempt at this year's Advent of Code (2023)
I will try and do some problems (probably not all of them) in Scala
## Progress:
#### Stars: 15 / 50
#### Stars: 18 / 50
| Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|:-----------------:|
| | | | | 1<br>:star::star: | 2<br>:star::star: | 3<br>:star::star: |
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8<br>:star: | 9 | 10 |
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8<br>:star::star: | 9<br>:star::star: | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | | | | | | |

80
src/day8/Ghost.scala Normal file
View File

@ -0,0 +1,80 @@
package day8
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
class Ghost(val startId: String) extends Iterator[Long] {
var preLoopOffsets: ArrayBuffer[Int] = new ArrayBuffer()
var loopOffsets: ArrayBuffer[Int] = new ArrayBuffer()
var visited: ArrayBuffer[(String, Int)] = new ArrayBuffer()
private var distIterator: Iterator[Long] = Iterator.empty
def computePath(nodes: mutable.Map[String, Node], rule: Array[Int]): Unit = {
var id: String = startId
preLoopOffsets.addOne(0)
var ruleI: Int = 0
// While not looped
while (!visited.contains((id, ruleI))) {
//println(s" Visited $id (ruleI = $ruleI)")
visited.addOne((id, ruleI))
id = nodes(id).nextNodes(rule(ruleI))
preLoopOffsets(preLoopOffsets.length-1) += 1
ruleI = (ruleI + 1) % rule.length
if (id.last == 'Z') {
preLoopOffsets.addOne(0)
}
}
val loopStartDist: Int = visited.indexOf((id, ruleI))
var dist: Int = 0
var loopStartI: Int = 0
while (dist < loopStartDist) {
dist += preLoopOffsets(loopStartI)
loopStartI += 1
}
loopStartI -= 1
preLoopOffsets(preLoopOffsets.length-1) += preLoopOffsets(loopStartI) - loopStartDist
println(s" Loop from ($id, $ruleI) (index=$loopStartI)")
println(s" # of offsets: ${preLoopOffsets.length}")
loopOffsets.addAll(preLoopOffsets.drop(loopStartI+1))
preLoopOffsets = preLoopOffsets.dropRight(preLoopOffsets.length - loopStartI - 1)
println(preLoopOffsets.length)
println(loopOffsets.length)
distIterator = new GhostIterator(preLoopOffsets.toArray, loopOffsets.toArray)
}
override def hasNext: Boolean = true
override def next(): Long = distIterator.next()
private class GhostIterator(val preLoopOffsets: Array[Int], val loopOffsets: Array[Int]) extends Iterator[Long] {
private var inLoop: Boolean = false
private var offsetI: Int = 0
private var dist: Long = 0
override def hasNext: Boolean = true
override def next(): Long = {
var offset: Int = 0
if (inLoop) {
offset = loopOffsets(offsetI)
} else {
offset = preLoopOffsets(offsetI)
}
dist += offset
offsetI += 1
if (!inLoop) {
if (offsetI == preLoopOffsets.length) {
offsetI = 0
inLoop = true
}
} else {
offsetI %= loopOffsets.length
}
return dist
}
}
}

56
src/day8/Puzzle2.scala Normal file
View File

@ -0,0 +1,56 @@
package day8
import scala.collection.mutable
import scala.io.{BufferedSource, Source}
import scala.util.matching.Regex
import scala.util.matching.Regex.Match
object Puzzle2 {
var rule: Array[Int] = Array.empty
val nodes: mutable.Map[String, Node] = new mutable.HashMap()
var ghosts: Array[Ghost] = Array.empty
def loadInput(path: String): Unit = {
val source: BufferedSource = Source.fromFile(path)
val lines: String = source.getLines().mkString("\n")
val parts: Array[String] = lines.split("\n\n")
rule = parts(0).map(c => "LR".indexOf(c)).toArray
val regexp: Regex = new Regex("([A-Z]{3}) = \\(([A-Z]{3}), ([A-Z]{3})\\)")
for (line: String <- parts(1).split("\n")) {
val matches: Match = regexp.findFirstMatchIn(line).get
val id: String = matches.group(1)
val left: String = matches.group(2)
val right: String = matches.group(3)
nodes(id) = new Node(id, Array(left, right))
}
source.close()
}
def solve(path: String): Long = {
loadInput(path)
ghosts = nodes.keys.filter(k => k.last == 'A').map(k => new Ghost(k)).toArray
val ghostDists: mutable.Map[Ghost, Long] = new mutable.HashMap()
for (ghost: Ghost <- ghosts) {
println(s"Ghost from ${ghost.startId}")
ghost.computePath(nodes, rule)
ghostDists(ghost) = ghost.next()
}
while (ghostDists.values.toArray.distinct.length != 1) {
val minimum: (Ghost, Long) = ghostDists.toArray.minBy(_._2)
ghostDists(minimum._1) = minimum._1.next()
}
val dists: Array[Long] = ghostDists.values.toArray
return dists(0)
}
def main(args: Array[String]): Unit = {
val solution: Long = solve("./res/day8/input1.txt")
println(solution)
}
}

38
src/day9/Puzzle1.scala Normal file
View File

@ -0,0 +1,38 @@
package day9
import scala.io.{BufferedSource, Source}
object Puzzle1 {
var series: Array[Series] = Array.empty
def loadInput(path: String): Unit = {
val source: BufferedSource = Source.fromFile(path)
val lines: Array[String] = source.getLines().toArray
series = new Array(lines.length)
for ((line: String, i: Int) <- lines.zipWithIndex) {
series(i) = new Series(line.split(" ").map(_.toInt))
}
source.close()
}
def solve(path: String): Int = {
loadInput(path)
var solution: Int = 0
for (s: Series <- series) {
s.computeDiffs()
solution += s.extrapolate()
}
return solution
}
def main(args: Array[String]): Unit = {
val solution: Int = solve("./res/day9/input1.txt")
println(solution)
}
}

38
src/day9/Puzzle2.scala Normal file
View File

@ -0,0 +1,38 @@
package day9
import scala.io.{BufferedSource, Source}
object Puzzle2 {
var series: Array[Series] = Array.empty
def loadInput(path: String): Unit = {
val source: BufferedSource = Source.fromFile(path)
val lines: Array[String] = source.getLines().toArray
series = new Array(lines.length)
for ((line: String, i: Int) <- lines.zipWithIndex) {
series(i) = new Series(line.split(" ").map(_.toInt))
}
source.close()
}
def solve(path: String): Int = {
loadInput(path)
var solution: Int = 0
for (s: Series <- series) {
s.computeDiffs()
solution += s.reverseExtrapolate()
}
return solution
}
def main(args: Array[String]): Unit = {
val solution: Int = solve("./res/day9/input1.txt")
println(solution)
}
}

39
src/day9/Series.scala Normal file
View File

@ -0,0 +1,39 @@
package day9
import scala.collection.mutable.ArrayBuffer
class Series {
private val values: ArrayBuffer[Array[Int]] = new ArrayBuffer()
def this(initialValues: Array[Int]) = {
this()
values.addOne(initialValues.concat(Array(0)))
}
def computeDiffs(): Unit = {
while (!(values.last.dropRight(1).distinct.length == 1 && values.last(0) == 0)) {
val diffs: Array[Int] = new Array(values.last.length-1)
for (i: Int <- 0 until diffs.length-1) {
diffs(i) = values.last(i+1) - values.last(i)
}
values.addOne(diffs)
}
}
def extrapolate(): Int = {
val len: Int = values(0).length
values.last(values.last.length-1) = 0
for (i: Int <- values.length-2 to 0 by -1) {
values(i)(len-i-1) = values(i)(len-i-2) + values(i+1)(len-i-2)
}
return values(0).last
}
def reverseExtrapolate(): Int = {
val len: Int = values(0).length
values.last(values.last.length-1) = 0
for (i: Int <- values.length-2 to 0 by -1) {
values(i)(len-i-1) = values(i)(0) - values(i+1)(len-i-2)
}
return values(0).last
}
}

View File

@ -0,0 +1,9 @@
package day8
import org.scalatest.funsuite.AnyFunSuite
class Puzzle2Test extends AnyFunSuite {
test("Puzzle2.solve") {
assert(Puzzle2.solve("tests_res/day8/input1.txt") == 6)
}
}

View File

@ -0,0 +1,9 @@
package day9
import org.scalatest.funsuite.AnyFunSuite
class Puzzle1Test extends AnyFunSuite {
test("Puzzle1.solve") {
assert(Puzzle1.solve("tests_res/day9/input1.txt") == 114)
}
}

View File

@ -0,0 +1,9 @@
package day9
import org.scalatest.funsuite.AnyFunSuite
class Puzzle2Test extends AnyFunSuite {
test("Puzzle2.solve") {
assert(Puzzle2.solve("tests_res/day9/input1.txt") == 2)
}
}

View File

@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45