Compare commits

..

No commits in common. "0f8f8c3f19bc3b0e86e3cfa372562d5c77a2e231" and "3f2f2fd74192013fe069541c0a7fad012c02039c" have entirely different histories.

10 changed files with 2 additions and 283 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: 18 / 50
#### Stars: 15 / 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::star: | 9<br>:star::star: | 10 |
| 4<br>:star::star: | 5<br>:star::star: | 6<br>:star::star: | 7<br>:star::star: | 8<br>:star: | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | | | | | | |

View File

@ -1,80 +0,0 @@
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
}
}
}

View File

@ -1,56 +0,0 @@
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)
}
}

View File

@ -1,38 +0,0 @@
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)
}
}

View File

@ -1,38 +0,0 @@
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)
}
}

View File

@ -1,39 +0,0 @@
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

@ -1,9 +0,0 @@
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

@ -1,9 +0,0 @@
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

@ -1,9 +0,0 @@
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

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