Unfinished Day 19 Part 1. Nope. Skipping this.
This commit is contained in:
parent
25bc83d011
commit
c51316a773
39
input/2021/day19-sample1.txt
Normal file
39
input/2021/day19-sample1.txt
Normal file
|
@ -0,0 +1,39 @@
|
|||
--- scanner 0 ---
|
||||
-1,-1,1
|
||||
-2,-2,2
|
||||
-3,-3,3
|
||||
-2,-3,1
|
||||
5,6,-4
|
||||
8,0,7
|
||||
|
||||
--- scanner 1 ---
|
||||
1,-1,1
|
||||
2,-2,2
|
||||
3,-3,3
|
||||
2,-1,3
|
||||
-5,4,-6
|
||||
-8,-7,0
|
||||
|
||||
--- scanner 2 ---
|
||||
-1,-1,-1
|
||||
-2,-2,-2
|
||||
-3,-3,-3
|
||||
-1,-3,-2
|
||||
4,6,5
|
||||
-7,0,8
|
||||
|
||||
--- scanner 3 ---
|
||||
1,1,-1
|
||||
2,2,-2
|
||||
3,3,-3
|
||||
1,3,-2
|
||||
-4,-6,5
|
||||
7,0,8
|
||||
|
||||
--- scanner 4 ---
|
||||
1,1,1
|
||||
2,2,2
|
||||
3,3,3
|
||||
3,1,2
|
||||
-6,-4,-5
|
||||
0,7,-8
|
136
input/2021/day19-sample2.txt
Normal file
136
input/2021/day19-sample2.txt
Normal file
|
@ -0,0 +1,136 @@
|
|||
--- scanner 0 ---
|
||||
404,-588,-901
|
||||
528,-643,409
|
||||
-838,591,734
|
||||
390,-675,-793
|
||||
-537,-823,-458
|
||||
-485,-357,347
|
||||
-345,-311,381
|
||||
-661,-816,-575
|
||||
-876,649,763
|
||||
-618,-824,-621
|
||||
553,345,-567
|
||||
474,580,667
|
||||
-447,-329,318
|
||||
-584,868,-557
|
||||
544,-627,-890
|
||||
564,392,-477
|
||||
455,729,728
|
||||
-892,524,684
|
||||
-689,845,-530
|
||||
423,-701,434
|
||||
7,-33,-71
|
||||
630,319,-379
|
||||
443,580,662
|
||||
-789,900,-551
|
||||
459,-707,401
|
||||
|
||||
--- scanner 1 ---
|
||||
686,422,578
|
||||
605,423,415
|
||||
515,917,-361
|
||||
-336,658,858
|
||||
95,138,22
|
||||
-476,619,847
|
||||
-340,-569,-846
|
||||
567,-361,727
|
||||
-460,603,-452
|
||||
669,-402,600
|
||||
729,430,532
|
||||
-500,-761,534
|
||||
-322,571,750
|
||||
-466,-666,-811
|
||||
-429,-592,574
|
||||
-355,545,-477
|
||||
703,-491,-529
|
||||
-328,-685,520
|
||||
413,935,-424
|
||||
-391,539,-444
|
||||
586,-435,557
|
||||
-364,-763,-893
|
||||
807,-499,-711
|
||||
755,-354,-619
|
||||
553,889,-390
|
||||
|
||||
--- scanner 2 ---
|
||||
649,640,665
|
||||
682,-795,504
|
||||
-784,533,-524
|
||||
-644,584,-595
|
||||
-588,-843,648
|
||||
-30,6,44
|
||||
-674,560,763
|
||||
500,723,-460
|
||||
609,671,-379
|
||||
-555,-800,653
|
||||
-675,-892,-343
|
||||
697,-426,-610
|
||||
578,704,681
|
||||
493,664,-388
|
||||
-671,-858,530
|
||||
-667,343,800
|
||||
571,-461,-707
|
||||
-138,-166,112
|
||||
-889,563,-600
|
||||
646,-828,498
|
||||
640,759,510
|
||||
-630,509,768
|
||||
-681,-892,-333
|
||||
673,-379,-804
|
||||
-742,-814,-386
|
||||
577,-820,562
|
||||
|
||||
--- scanner 3 ---
|
||||
-589,542,597
|
||||
605,-692,669
|
||||
-500,565,-823
|
||||
-660,373,557
|
||||
-458,-679,-417
|
||||
-488,449,543
|
||||
-626,468,-788
|
||||
338,-750,-386
|
||||
528,-832,-391
|
||||
562,-778,733
|
||||
-938,-730,414
|
||||
543,643,-506
|
||||
-524,371,-870
|
||||
407,773,750
|
||||
-104,29,83
|
||||
378,-903,-323
|
||||
-778,-728,485
|
||||
426,699,580
|
||||
-438,-605,-362
|
||||
-469,-447,-387
|
||||
509,732,623
|
||||
647,635,-688
|
||||
-868,-804,481
|
||||
614,-800,639
|
||||
595,780,-596
|
||||
|
||||
--- scanner 4 ---
|
||||
727,592,562
|
||||
-293,-554,779
|
||||
441,611,-461
|
||||
-714,465,-776
|
||||
-743,427,-804
|
||||
-660,-479,-426
|
||||
832,-632,460
|
||||
927,-485,-438
|
||||
408,393,-506
|
||||
466,436,-512
|
||||
110,16,151
|
||||
-258,-428,682
|
||||
-393,719,612
|
||||
-211,-452,876
|
||||
808,-476,-593
|
||||
-575,615,604
|
||||
-485,667,467
|
||||
-680,325,-822
|
||||
-627,-443,-432
|
||||
872,-547,-609
|
||||
833,512,582
|
||||
807,604,487
|
||||
839,-516,451
|
||||
891,-625,532
|
||||
-652,-548,-490
|
||||
30,-46,-14
|
1105
input/2021/day19.txt
Normal file
1105
input/2021/day19.txt
Normal file
File diff suppressed because it is too large
Load diff
|
@ -2,5 +2,50 @@ package de.qwertyuiop.aoc.`2021`
|
|||
|
||||
import de.qwertyuiop.aoc.lib.*
|
||||
import cats.*, cats.implicits.given
|
||||
import Vectors.{*, given}
|
||||
import BeaconScanners.*
|
||||
|
||||
def day19(using InputSource): Unit = ???
|
||||
def day19(using InputSource): Unit =
|
||||
val scanners = input().split("").collect{
|
||||
case s"--- scanner $index ---" :: coords => index.toInt -> coords.collect((Vec3D.parse[Int] _ ).unlift)
|
||||
}.toMap
|
||||
|
||||
val normalizedScanners =
|
||||
for (id, coords) <- scanners
|
||||
yield
|
||||
val dists = coords.zipWithIndex.combinations(2)
|
||||
.collect{ case List((a, ai),(b, bi)) => normalize(a - b) -> Set(ai, bi) }
|
||||
id -> dists.toMap
|
||||
|
||||
val overlapping = normalizedScanners.toVector.combinations(2).flatMap {
|
||||
case Vector(id1 -> dists1, id2 -> dists2) =>
|
||||
val overlapDists = (dists1.keySet & dists2.keySet)
|
||||
val overlaps = overlapDists.flatMap(dists1).size
|
||||
val beaconMapping = overlapDists.toVector.flatMap { dist =>
|
||||
val List(a1, b1) = dists1(dist).toList
|
||||
val List(a2, b2) = dists2(dist).toList
|
||||
Vector(a1 -> a2, a1 -> b2, b1 -> a2, b1 -> b2)
|
||||
}.occurrences.filter(_._2 > 1).map(_._1).toMap
|
||||
Option.when(overlaps >= 12)((id1, id2, beaconMapping))
|
||||
case _ => sys.error("combinations(2) is broken")
|
||||
}.toVector
|
||||
|
||||
val translations = overlapping.map { (a, b, beacons) =>
|
||||
val overlapOrig = beacons.map((k,v) => scanners(a)(k) -> scanners(b)(v))
|
||||
val overlapIndexed = overlapOrig.toVector
|
||||
val Vector(a0 -> b0, a1 -> b1) = overlapOrig.take(2).toVector
|
||||
val aDist = a0 - a1
|
||||
val bDist = b0 - b1
|
||||
val sign = aDist.combine(bDist)(_ / _)
|
||||
val (origin, neighbour) = overlapOrig.head
|
||||
Map(a -> List(b -> (origin - (neighbour ⊙ sign))))
|
||||
}.combineAll
|
||||
translations.foreach(println)
|
||||
val cleaned = translations.mapValuesS(_.filter(translations contains _._1))
|
||||
|
||||
//cleaned.foreach((k, v) => println(s"from $k: ${v.size} translations: ${v.map(_._1)}"))
|
||||
|
||||
object BeaconScanners:
|
||||
def normalize(v: Vec3D[Int]): Vec3D[Int] =
|
||||
val Seq(x, y, z) = Seq(v.x, v.y, v.z).map(_.abs).sorted
|
||||
Vec3D(x, y, z)
|
||||
|
|
|
@ -22,16 +22,50 @@ object Vectors:
|
|||
yield (x, y)
|
||||
)
|
||||
|
||||
def parse[T: Numeric](in: String): Option[Vec2D[T]] =
|
||||
in match
|
||||
case s"$x,$y" =>
|
||||
for xt <- Numeric[T].parseString(x)
|
||||
yt <- Numeric[T].parseString(y)
|
||||
yield Vec2D(xt,yt)
|
||||
case _ => None
|
||||
|
||||
object Vec3D:
|
||||
def apply[T](x: T, y: T, z: T): Vec3D[T] = (x,y,z)
|
||||
|
||||
def parse[T: Numeric](in: String): Option[Vec3D[T]] =
|
||||
in match
|
||||
case s"$x,$y,$z" =>
|
||||
for xt <- Numeric[T].parseString(x)
|
||||
yt <- Numeric[T].parseString(y)
|
||||
zt <- Numeric[T].parseString(z)
|
||||
yield Vec3D(xt,yt,zt)
|
||||
case _ => None
|
||||
|
||||
object Vec4D:
|
||||
def apply[T](x: T, y: T, z: T, w: T): Vec4D[T] = (x,y,z,w)
|
||||
|
||||
trait Vec[T]:
|
||||
extension (v: T)
|
||||
def +(w: T): T
|
||||
def neighbours: Vector[T]
|
||||
def parse[T: Numeric](in: String): Option[Vec4D[T]] =
|
||||
in match
|
||||
case s"$x,$y,$z,$w" =>
|
||||
for xt <- Numeric[T].parseString(x)
|
||||
yt <- Numeric[T].parseString(y)
|
||||
zt <- Numeric[T].parseString(z)
|
||||
wt <- Numeric[T].parseString(w)
|
||||
yield Vec4D(xt,yt,zt,wt)
|
||||
case _ => None
|
||||
|
||||
trait Vec[V, E]:
|
||||
def parse(in: String): Option[V]
|
||||
|
||||
extension (a: V)
|
||||
def +(b: V): V
|
||||
def -(b: V): V
|
||||
def *(scale: E): V
|
||||
|
||||
def combine(b: V)(f: (E, E) => E): V
|
||||
|
||||
def neighbours: Vector[V]
|
||||
|
||||
given [T : Show]: Show[Vec2D[T]] with
|
||||
def show(v: Vec2D[T]): String = show"Vec2D(${v._1}, ${v._2})"
|
||||
|
@ -40,36 +74,49 @@ object Vectors:
|
|||
given [T : Show]: Show[Vec4D[T]] with
|
||||
def show(v: Vec4D[T]): String = show"Vec4D(${v._1}, ${v._2}, ${v._3}, ${v._4})"
|
||||
|
||||
given [T: Numeric]: Vec[Vec2D[T]] with
|
||||
extension (v: Vec2D[T])
|
||||
def +(w: Vec2D[T]): Vec2D[T] = (v._1 + w._1, v._2 + w._2)
|
||||
def -(w: Vec2D[T]): Vec2D[T] = (v._1 - w._1, v._2 - w._2)
|
||||
def *(x: T): Vec2D[T] = (v._1 * x, v._2 * x)
|
||||
given Functor[Vec2D] with
|
||||
def map[A, B](fa: Vec2D[A])(f: A => B): Vec2D[B] = Vec2D(f(fa._1), f(fa._2))
|
||||
|
||||
def x: T = v._1
|
||||
def y: T = v._2
|
||||
given Functor[Vec3D] with
|
||||
def map[A, B](fa: Vec3D[A])(f: A => B): Vec3D[B] = Vec3D(f(fa._1), f(fa._2), f(fa._3))
|
||||
|
||||
given Functor[Vec4D] with
|
||||
def map[A, B](fa: Vec4D[A])(f: A => B): Vec4D[B] = Vec4D(f(fa._1), f(fa._2), f(fa._3), f(fa._4))
|
||||
|
||||
given [T: Numeric]: Vec[Vec2D[T], T] with
|
||||
def parse(in: String): Option[Vec2D[T]] = Vec2D.parse(in)
|
||||
|
||||
extension (a: Vec2D[T])
|
||||
def +(b: Vec2D[T]): Vec2D[T] = (a.x + b.x, a.y + b.y)
|
||||
def -(b: Vec2D[T]): Vec2D[T] = (a.x - b.x, a.y - b.y)
|
||||
def *(s: T): Vec2D[T] = (a.x * s, a.y * s)
|
||||
|
||||
def combine(b: Vec2D[T])(f: (T, T) => T): Vec2D[T] = Vec2D(f(a.x, b.x), f(a.y, b.y))
|
||||
|
||||
def x: T = a._1
|
||||
def y: T = a._2
|
||||
|
||||
def rot(deg: Dir): Vec2D[T] = deg match
|
||||
case North => (-v._2, v._1)
|
||||
case West => (-v._1, -v._2)
|
||||
case South => (v._2, -v._1)
|
||||
case East => v
|
||||
case North => (-a.y, a.x)
|
||||
case West => (-a.x, -a.y)
|
||||
case South => (a.y, -a.x)
|
||||
case East => a
|
||||
|
||||
def left(deg: Int): Vec2D[T] = repeat[Vec2D[T]](deg / 90)(_.rotLeft)(v)
|
||||
def right(deg: Int): Vec2D[T] = repeat[Vec2D[T]](deg / 90)(_.rotRight)(v)
|
||||
def left(deg: Int): Vec2D[T] = repeat[Vec2D[T]](deg / 90)(_.rotLeft)(a)
|
||||
def right(deg: Int): Vec2D[T] = repeat[Vec2D[T]](deg / 90)(_.rotRight)(a)
|
||||
|
||||
def rotLeft: Vec2D[T] = (-v._2, v._1)
|
||||
def rotRight: Vec2D[T] = (v._2, -v._1)
|
||||
def rotLeft: Vec2D[T] = (-a.y, a.x)
|
||||
def rotRight: Vec2D[T] = (a.y, -a.x)
|
||||
|
||||
def move(dir: Dir, dist: T): Vec2D[T] = dir match
|
||||
case North => (v._1, v._2 + dist)
|
||||
case South => (v._1, v._2 - dist)
|
||||
case East => (v._1 + dist, v._2)
|
||||
case West => (v._1 - dist, v._2)
|
||||
case North => (a.x, a.y + dist)
|
||||
case South => (a.x, a.y - dist)
|
||||
case East => (a.x + dist, a.y)
|
||||
case West => (a.x - dist, a.y)
|
||||
|
||||
def manhattan: T = v._1.abs + v._2.abs
|
||||
def manhattan: T = a.x.abs + a._2.abs
|
||||
def neighbours: Vector[Vec2D[T]] =
|
||||
neighbourCoords(2).map(n => v + (n(0), n(1)))
|
||||
neighbourCoords(2).map(n => a + (n(0), n(1)))
|
||||
|
||||
def orthoNeighbours(sizeX: T, sizeY: T)(using Ordering[T]): Vector[Vec2D[T]] =
|
||||
val n = summon[Numeric[T]]
|
||||
|
@ -89,26 +136,43 @@ object Vectors:
|
|||
given [T: Monoid]: Monoid[Vec3D[T]] = semiauto.monoid
|
||||
given [T: Monoid]: Monoid[Vec4D[T]] = semiauto.monoid
|
||||
|
||||
given [T: Numeric]: Vec[Vec3D[T]] with
|
||||
extension (v: Vec3D[T])
|
||||
def +(w: Vec3D[T]): Vec3D[T] = (v._1 + w._1, v._2 + w._2, v._3 + w._3)
|
||||
given [T: Numeric]: Vec[Vec3D[T], T] with
|
||||
def parse(in: String): Option[Vec3D[T]] = Vec3D.parse(in)
|
||||
|
||||
extension (a: Vec3D[T])
|
||||
def +(b: Vec3D[T]): Vec3D[T] = (a.x + b.x, a.y + b.y, a.z + b.z)
|
||||
def -(b: Vec3D[T]): Vec3D[T] = (a.x - b.x, a.y - b.y, a.z - b.z)
|
||||
def *(s: T): Vec3D[T] = (a.x * s, a.y * s, a.z * s)
|
||||
|
||||
def combine(b: Vec3D[T])(f: (T, T) => T): Vec3D[T] = Vec3D(f(a.x, b.x), f(a.y, b.y), f(a.z, b.z))
|
||||
|
||||
/** elementwise multiplication */
|
||||
def ⊙(b: Vec3D[T]): Vec3D[T] = (a.x * b.x, a.y * b.y, a.z * b.z)
|
||||
|
||||
def neighbours: Vector[Vec3D[T]] =
|
||||
neighbourCoords(3).map(n => v + (n(0), n(1), n(2)))
|
||||
neighbourCoords(3).map(n => a + (n(0), n(1), n(2)))
|
||||
|
||||
def x: T = v._1
|
||||
def y: T = v._2
|
||||
def z: T = v._3
|
||||
def x: T = a._1
|
||||
def y: T = a._2
|
||||
def z: T = a._3
|
||||
|
||||
given [T: Numeric]: Vec[Vec4D[T], T] with
|
||||
def parse(in: String): Option[Vec4D[T]] = Vec4D.parse(in)
|
||||
|
||||
extension (a: Vec4D[T])
|
||||
def +(b: Vec4D[T]): Vec4D[T] = (a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w)
|
||||
def -(b: Vec4D[T]): Vec4D[T] = (a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w)
|
||||
def *(s: T): Vec4D[T] = (a.x * s, a.y * s, a.z * s, a.w * s)
|
||||
|
||||
def combine(b: Vec4D[T])(f: (T, T) => T): Vec4D[T] = Vec4D(f(a.x, b.x), f(a.y, b.y), f(a.z, b.z), f(a.w, b.w))
|
||||
|
||||
given [T: Numeric]: Vec[Vec4D[T]] with
|
||||
extension (v: Vec4D[T])
|
||||
def +(u: Vec4D[T]): Vec4D[T] = (v._1 + u._1, v._2 + u._2, v._3 + u._3, v._4 + u._4)
|
||||
def neighbours: Vector[Vec4D[T]] =
|
||||
neighbourCoords(4).map(n => v + (n(0), n(1), n(2), n(3)))
|
||||
neighbourCoords(4).map(n => a + (n(0), n(1), n(2), n(3)))
|
||||
|
||||
def x: T = v._1
|
||||
def y: T = v._2
|
||||
def z: T = v._3
|
||||
def w: T = v._4
|
||||
def x: T = a._1
|
||||
def y: T = a._2
|
||||
def z: T = a._3
|
||||
def w: T = a._4
|
||||
|
||||
|
||||
/* compute these only once per type and dimension*/
|
||||
|
|
Loading…
Reference in a new issue