This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added fast compiling day 17 part 2 in Scala
- Loading branch information
1 parent
c1c1827
commit 0518a90
Showing
8 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
aoc17/src/main/scala/de/havox_design/aoc2022/day17/Day17.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package de.havox_design.aoc2022.day17 | ||
|
||
object Day17 { | ||
def solvePart2(filename: String, runs: Int = 10000): Long = { | ||
val guess = 1000 | ||
val height = simulate(readData(filename).next()) | ||
.slice(1, 5 * guess) | ||
.toSeq | ||
val delta = height.sliding(2).map(s => s.last - s.head).toSeq | ||
val end = delta.size - guess | ||
val start = delta.lastIndexOfSlice(delta.takeRight(guess), end - 1) | ||
val cycleHeight = height(end) - height(start) | ||
val cycleWidth = end - start | ||
val offset = 1000000000000L - 1 - start | ||
val quotient = offset / cycleWidth | ||
val remainder = offset % cycleWidth | ||
|
||
(quotient * cycleHeight) + height(start + remainder.toInt) | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
println("Solution for part2: " + solvePart2("input.txt")) | ||
} | ||
|
||
private def simulate(jets: String): Iterator[Int] = | ||
val initial = State(jets, Set.tabulate(8)(Point(_, 0)), 0, 0, 0) | ||
|
||
Iterator | ||
.iterate(initial)(_.step) | ||
.map(_.height) | ||
|
||
private def readData(filename: String): Iterator[String] = | ||
scala | ||
.io | ||
.Source | ||
.fromResource(filename) | ||
.getLines() | ||
} |
7 changes: 7 additions & 0 deletions
7
aoc17/src/main/scala/de/havox_design/aoc2022/day17/Point.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.havox_design.aoc2022.day17 | ||
|
||
case class Point(x: Int, y: Int) | ||
|
||
extension (shape: Set[Point]) | ||
def move(dx: Int, dy: Int): Set[Point] = shape.map(p => Point(p.x + dx, p.y + dy)) | ||
def canMove(grid: Set[Point]): Boolean = shape.forall(p => p.x > 0 && p.x < 8 && !grid.contains(p)) |
37 changes: 37 additions & 0 deletions
37
aoc17/src/main/scala/de/havox_design/aoc2022/day17/State.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package de.havox_design.aoc2022.day17 | ||
|
||
val shapes = Seq( | ||
Set(Point(0, 0), Point(1, 0), Point(2, 0), Point(3, 0)), | ||
Set(Point(1, 0), Point(0, 1), Point(1, 1), Point(2, 1), Point(1, 2)), | ||
Set(Point(0, 0), Point(1, 0), Point(2, 0), Point(2, 1), Point(2, 2)), | ||
Set(Point(0, 0), Point(0, 1), Point(0, 2), Point(0, 3)), | ||
Set(Point(0, 0), Point(1, 0), Point(0, 1), Point(1, 1))) | ||
|
||
case class State(jets: String, grid: Set[Point], shapeIndex: Int, jetIndex: Int, height: Int): | ||
def step: State = | ||
val initialShape = shapes(shapeIndex % shapes.size) | ||
.move(3, height + 4) | ||
val (nextShape, nextJetIndex) = fall(initialShape, jetIndex) | ||
val nextHeight = height | ||
.max(nextShape.map(_.y).max) | ||
|
||
State(jets, grid ++ nextShape, shapeIndex + 1, nextJetIndex, nextHeight) | ||
|
||
private def fall(shape: Set[Point], jetIndex: Int): (Set[Point], Int) = | ||
val jet = jets(jetIndex % jets.length) | ||
val first = if jet != '>' then | ||
shape.move(-1, 0) | ||
else | ||
shape.move(1, 0) | ||
val second = if !first.canMove(grid) then | ||
shape | ||
else | ||
first | ||
val third = second | ||
.move(0, -1) | ||
|
||
if !third.canMove(grid) then | ||
(second, jetIndex + 1) | ||
else | ||
fall(third, jetIndex + 1) | ||
end State |
14 changes: 14 additions & 0 deletions
14
aoc17/src/test/scala/de/havox_design/aoc2022/day17/Day17ScalaTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.havox_design.aoc2022.day17 | ||
|
||
import Day17._ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class Day17ScalaTest extends AnyFunSuite { | ||
test("Day 17 - Part 2") { | ||
println("Solution for part2: " + solvePart2("input.txt")) | ||
} | ||
|
||
test("Day 17 - Part 2 - Sample") { | ||
assert(solvePart2("sample.txt") == 1514285714288L) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters