Skip to content

Commit

Permalink
2023-24 - refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 26, 2023
1 parent dded175 commit 2b31e67
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
16 changes: 2 additions & 14 deletions scala2/src/main/scala/jurisk/adventofcode/y2023/Advent24.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import jurisk.utils.Parsing.StringOps
import org.scalatest.matchers.should.Matchers._

object Advent24 {
private type InputPart2 = List[PositionAndVelocity3D]

final case class PositionAndVelocity2D(
position: Coordinates2D[Long],
velocity: Coordinates2D[Long],
Expand All @@ -30,8 +32,6 @@ object Advent24 {
def v: Coords3D[Long] = velocity
}

type InputPart2 = List[PositionAndVelocity3D]

def parse(input: String): InputPart2 = {
def parse3D(input: String): PositionAndVelocity3D =
input match {
Expand All @@ -46,18 +46,6 @@ object Advent24 {
input.parseLines(parse3D)
}

def areVectorsParallel(a: Coords3D[Long], b: Coords3D[Long]): Boolean = {
val ax = BigDecimal(a.x)
val ay = BigDecimal(a.y)
val az = BigDecimal(a.z)

val bx = BigDecimal(b.x)
val by = BigDecimal(b.y)
val bz = BigDecimal(b.z)

List(ax / bx, ay / by, az / bz).distinct.size == 1
}

// https://en.wikipedia.org/wiki/Chinese_remainder_theorem
// From https://github.com/ellentari/aoc2023/blob/main/src/main/scala/aoc/Day24.scala
// and https://github.com/ellentari/aoc2023/blob/main/src/main/scala/aoc/util/ChineseRemainderTheorem.scala
Expand Down
12 changes: 12 additions & 0 deletions scala2/src/main/scala/jurisk/geometry/Coords3D.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,16 @@ object Coords3D {
val maxZ = points.map(_.z).max
Area3D(Coords3D(minX, minY, minZ), Coords3D(maxX, maxY, maxZ))
}

def areVectorsParallel(a: Coords3D[Long], b: Coords3D[Long]): Boolean = {
val ax = BigDecimal(a.x)
val ay = BigDecimal(a.y)
val az = BigDecimal(a.z)

val bx = BigDecimal(b.x)
val by = BigDecimal(b.y)
val bz = BigDecimal(b.z)

List(ax / bx, ay / by, az / bz).distinct.size == 1
}
}
20 changes: 0 additions & 20 deletions scala2/src/test/scala/jurisk/adventofcode/y2023/Advent24Spec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ class Advent24Spec extends AnyFreeSpec {
}
}

// https://www.mathsisfun.com/algebra/vectors-cross-product.html
"crossProduct" - {
(Coords3D[Long](2, 3, 4) crossProduct Coords3D[Long](
5,
6,
7,
)) shouldEqual Coords3D[Long](
-3,
6,
-3,
)
}

"areVectorsParallel" in {
val a = Coords3D[Long](5, 2, -1)
val b = Coords3D[Long](-10, -4, 2)
areVectorsParallel(a, b) shouldEqual true
areVectorsParallel(a, expectedTestAnswer.velocity) shouldEqual false
}

"linesIntersect" in {
testData foreach { rock =>
linesIntersect(rock, expectedTestAnswer) shouldEqual true
Expand Down
27 changes: 27 additions & 0 deletions scala2/src/test/scala/jurisk/geometry/Coords3DSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jurisk.geometry

import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers._
import jurisk.geometry.Coords3D.areVectorsParallel

class Coords3DSpec extends AnyFreeSpec {
// https://www.mathsisfun.com/algebra/vectors-cross-product.html
"crossProduct" - {
(Coords3D[Long](2, 3, 4) crossProduct Coords3D[Long](
5,
6,
7,
)) shouldEqual Coords3D[Long](
-3,
6,
-3,
)
}

"areVectorsParallel" in {
val a = Coords3D[Long](5, 2, -1)
val b = Coords3D[Long](-10, -4, 2)
areVectorsParallel(a, b) shouldEqual true
areVectorsParallel(a, Coords3D[Long](-3, 1, 2)) shouldEqual false
}
}

0 comments on commit 2b31e67

Please sign in to comment.