Day 15 (with very slow part 2)
This commit is contained in:
parent
62e6f535ee
commit
553b52afa0
10
input/2021/day15-sample1.txt
Normal file
10
input/2021/day15-sample1.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
1163751742
|
||||||
|
1381373672
|
||||||
|
2136511328
|
||||||
|
3694931569
|
||||||
|
7463417111
|
||||||
|
1319128137
|
||||||
|
1359912421
|
||||||
|
3125421639
|
||||||
|
1293138521
|
||||||
|
2311944581
|
100
input/2021/day15.txt
Normal file
100
input/2021/day15.txt
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
1797218461142179319573884891481221191657631838831754357117998319396235714788116521813149318951532839
|
||||||
|
8696981297918411971289919865719836144693333162529142812974198292932295491956499215586135482941739392
|
||||||
|
2991519621128967692174919369871962711711888349621119249111241263371998761114982461871811915749818981
|
||||||
|
9519298621148891919219116926535193934326629926112136839413932239182391497985687943399987482691961448
|
||||||
|
3322441212551229339949332249462272248555173795333563937398919128799491931719178641531998141722584429
|
||||||
|
9118353711988171611983883569964392454546273683762581127881992188984158689612224523966629229869841368
|
||||||
|
6619293351892345695919931961961959917575811995111642219592114857133566312236841811951318937396921471
|
||||||
|
4119719294958136935585131572211882179799139693669968792713194888628799229972719918777119331922298192
|
||||||
|
2897361395359964817739628715924942441921486256913485426413911886166322118384197271142719438976157178
|
||||||
|
3389638941186823278892211919997494993715555987282539661565969785971121324112299762618146712244947159
|
||||||
|
9282911861243373596527491925976176995918989739998978638179963274848391398779728689754153765948619469
|
||||||
|
8659237241858888999515587517811573419717292977626899259293151572296128169661741752918682542171942321
|
||||||
|
7764241289968175261192129897224933828487961991159914599166517495921873868188964356615533731833973441
|
||||||
|
5211819126749298191741999756776373271358917928175911622381895311231992844719941999396791192927375871
|
||||||
|
7147248429776976347262913517219841812956611529169838774299925229977967856924839394369116944811677481
|
||||||
|
9999327358212995943299686946957298991514945979821411233241473988197932142383177144729479991836191427
|
||||||
|
3118856861346926341911713996912169631119911431551249817492292818844125715189849969193192961131621911
|
||||||
|
9725216819926791171517898194558972919139137513355733988171946498928816869874911791891669217257113496
|
||||||
|
7233995695449113678612444521112129251251273195695889939964857843349673413266536268949113769342251889
|
||||||
|
6127221278819178621741141313355129711426692693677392749193553853936972975965634954629141151231879898
|
||||||
|
9316815118477222367841192213391742191959264299125536916699819514396152297399182268877162451983336217
|
||||||
|
9462996178167836998492412349917112812632399196941153512738339788549881946851158124161362774914992894
|
||||||
|
7967964922318976294433211213741866891119759725314487732559823798274829681391981566174639155611351418
|
||||||
|
8929424417132556157634716487111521991271289231745124921192394921986921935952364171299975161231824774
|
||||||
|
3218194149784328917294438859919299949729271441816111296879933691981973292149147435115597882711494998
|
||||||
|
9963686292555144316424191747459116221815592814842158141691641997871258112472186921312965944237595294
|
||||||
|
6895964781364928469137998279116249898846673137174356126748598484323966111982813263974112357699899763
|
||||||
|
7269811648363829147741792898355598248569675813169571842631115991782571118598396148154361996719188448
|
||||||
|
8844191981976773829997291691395479942961199359734575829759316784548382466496616827221178185913513238
|
||||||
|
3257332961391991114574911188523198843295839929161419384837634973422188848727297597838217868798186511
|
||||||
|
6923161896988782579586511869949273142676719174251591792643298288772526859781298141569266186617218381
|
||||||
|
7784717941413921956659125922992333927913184321298989112521798475113465863391148588936386584969341199
|
||||||
|
7729281363926216291184126185146911946599969922215989319947928254933624961823119312713825739752727942
|
||||||
|
9791595898229915783452994189791381989371243326849571162258539814113211999121938944928495688393186918
|
||||||
|
8891733154174974952723922594949579111188836887289616671917191976149512141593991859811284259441183431
|
||||||
|
8882142834538653968328977548117488826724429161466849928479176885239389889939199981835126994327391896
|
||||||
|
9517995278111242818459916811998944915697417117912182611857541473935942618223818819762995154781472981
|
||||||
|
5688665816361987489729842796931921414512473917441794427741369262172816993213983458958198919913823991
|
||||||
|
1191916752824613923926969193925957295939327864123259676198986799118369891115359391957249452752929126
|
||||||
|
7953359279426186179959629664623979247293134816151993933394146989756964927672838611674519931995335481
|
||||||
|
2588619583212263911463327947238911867191914187199422968181249242598932119917178149668551829479381692
|
||||||
|
9139997197211729428879315949158215521759731858227871598393911996977179197936292442539968196974143392
|
||||||
|
4232261554187854128157992418243551168633731913177429834412129191717919582896366737612182389415784188
|
||||||
|
4821243736858296431871216889694294547359824994296126745919812123118983919554436811916197471485951899
|
||||||
|
1789444139925892222466367952996429927297845751229192689449127919198952992139293319744714159936449428
|
||||||
|
9826695391968915127581997372149393515911949277794233746169181958714194329918573751286548977459133984
|
||||||
|
2192337259911892484227791261124721948491913173653784167993128393999959499743131219363182119952283978
|
||||||
|
3217492185391446491561359499251152813678989728154211194698554711579573178949291439543428975396999969
|
||||||
|
3867861429971172192458819948178991957886669668729948991163661783211114251141521367232129119114793985
|
||||||
|
8987756326299362119986998879589364162214661191762462991978789152965978499991924196194961211923983946
|
||||||
|
3941989955272175768425292121934436939925252349513897482811328179579133112112915747923399298497919231
|
||||||
|
8218972259833197878184194815191533999833125399873974758921718196132623614183515273946514288499727111
|
||||||
|
1437751958193491251887139961112992551391137225591413591259855297573633593916711792611198698994214931
|
||||||
|
9222784952843174957289129282688819183797667123851928962298111123475891321173291351957242491579929288
|
||||||
|
4732619159819497865337378815817638211711793289431191451159913994292222658957311641117562199447187762
|
||||||
|
1922578172811266932889929671898587112921216994269163349194897862669631216699811517641929113879429965
|
||||||
|
5161989656251729327191879868564154192726119219819183244889135163523927489855954468739959421881994238
|
||||||
|
4819921164369791999159813491399265881691121929971789718215883948391891598814936575118939822189471793
|
||||||
|
8882513759175398582299829751631629348693254997893362776799788389998788399339972916897658269997913718
|
||||||
|
1292943248881936384125164876519422482179293397552386181794636147294912887491588373821779931911191187
|
||||||
|
2629847941812784111897761715195255788824381859149113249724664791999515675414915516679929144685692278
|
||||||
|
2772682798815662376827394271849212113213692687627694877365198595132768349941735916599485286144676945
|
||||||
|
2393932687723188393839519563914218446613793719169118499134828955151218298152994598289712724891857191
|
||||||
|
3617398318482265926582994889268631869955227758698817939992471699819589382891915143633491667191983217
|
||||||
|
6911471527859211299313851863879935129278786895167159956316181913899978246167119482583766973277121191
|
||||||
|
1981719349418167792118929872278117552711988231248819194187731813131144284461159795271818599994593714
|
||||||
|
2811199196149453976164391892639388999124279731159528893891121792118688173627549881311642338117962758
|
||||||
|
4691716484637299892159474721929413914233367741429229241226899989254686631611518944863887979681529782
|
||||||
|
4482138978699511118519118488472749721682121879121719142129127792216962898129618931812891762991227189
|
||||||
|
9165122888898199618119141611974614971971318493267915612313211217617392424197681312999919621771185729
|
||||||
|
1588131199884516629986871332411227313281192112941476434724378996881979822418196899342121282934856816
|
||||||
|
7126317411441358114193567599682892334987381251188952754399928769359297971214875844983937899953212988
|
||||||
|
8587246861996999349587179148718697599488796398983115487494693657146299118222219134299111489194231965
|
||||||
|
1756191969194346888194346711493762171765976629194948439394646717346291964222987169438133774148812918
|
||||||
|
8789198221258954468986293399419779223378943346282786847513649264261539662294929759499768986798169241
|
||||||
|
1138369993982331549196792976559799283413654949889158671638369975888731165551659877511934953998822274
|
||||||
|
2114826371811112198999564997711125991529298612214145587752669675324982818263881118627791312962819135
|
||||||
|
3289283332363218131299181648827294977213217215138555913699183855926348817213881719932396959992968121
|
||||||
|
8587853367939591685793783997226894713789913999892859923556161114899661253264292692714823239899982193
|
||||||
|
9941317127358865229192397619611212997255961574849569689922816181189151859769399141999257779295762263
|
||||||
|
2171999627694757517559219922489957852841854844663175892649861832198936898853491281118127119111126853
|
||||||
|
9847187251892486999189989912188191379499126327252917351356711496334291364415686662182219562193321111
|
||||||
|
9189929424982871168612797812515916182171273998199863665452736918916924969999612492935667517295168295
|
||||||
|
3624681856881799987199539193723942395334489531142412618491447421164524793877168198845117125491337311
|
||||||
|
9297711591387465324465917921116922742218791943839142882882181986523416323962918891995927533949493181
|
||||||
|
3913897745491184244299396997431791991291596184419148627215918152618971355656911422988362342829957918
|
||||||
|
2161357198129894981258979893981129271657818883175987718298879242297781166689323717991763995914111299
|
||||||
|
1429691393132159496998994989721998771653222413191152795122991422999714758548118211476912919569859657
|
||||||
|
1155146287171621743862845462211983191917592287951249299195262481191199691967277142619868428281772552
|
||||||
|
2435517981879328114574698787193894982992581195327819651511899224491686961899174168226249522658661146
|
||||||
|
1919894989221529553399133386169999552895185552222755159922552431149786511973745343931818539315311175
|
||||||
|
2683381835121351182998489919911372894291894973399493194281379971969183416974346479151274291799853129
|
||||||
|
1751991384249781899947665921219827616999239519455929713722239711149225383616519712941889322712828418
|
||||||
|
3295919911592497192118828258118529133121427523281991929691965274368825689299671219749189813124887278
|
||||||
|
7828319259851721362716353874221716695129721491254728715913967965694595858939949833991856557249153792
|
||||||
|
4643162283932891824519919817417814174343124346663682549387955944171239182344299415172323771718859129
|
||||||
|
9382811937823973227813927835213688211415836968191527337792159834142739892661818877429739395149421471
|
||||||
|
3279512226121999191142499711269119643819431895822859387584439741423768775114929691745825321781118144
|
||||||
|
9391973246579999241217437296325675163824334513949186691912595622177779849572792973716569959311526846
|
||||||
|
4671438158382646393847419899596676468374198874175298772399482512793928838556984113933878117348995239
|
|
@ -1,6 +1,36 @@
|
||||||
package de.qwertyuiop.aoc.`2021`
|
package de.qwertyuiop.aoc.`2021`
|
||||||
|
|
||||||
import de.qwertyuiop.aoc.lib.*
|
import de.qwertyuiop.aoc.lib.*
|
||||||
|
import Vectors.{*, given}
|
||||||
import cats.*, cats.implicits.given
|
import cats.*, cats.implicits.given
|
||||||
|
import util.chaining.given
|
||||||
|
import Day15._
|
||||||
|
|
||||||
def day15(using InputSource): Unit = ???
|
def day15(using InputSource): Unit =
|
||||||
|
val dangerZones = InputFormats.intMatrix
|
||||||
|
val fullDangerZones = fullMap(dangerZones)
|
||||||
|
|
||||||
|
|
||||||
|
println(s"Way through tile: ${findChitonWay(dangerZones).get._2}")
|
||||||
|
println(s"Way through all tiles: ${findChitonWay(fullDangerZones).get._2}")
|
||||||
|
|
||||||
|
|
||||||
|
object Day15:
|
||||||
|
def inc(line: Vector[Int]) = line.map(i => ((i + 1) % 10) max 1)
|
||||||
|
def incNest(tile: Vector[Vector[Int]]) = tile.map(inc)
|
||||||
|
|
||||||
|
def fullMap(tile: Vector[Vector[Int]]): Vector[Vector[Int]] =
|
||||||
|
val topRow = tile.map(l => (1 to 4).foldLeft(l, l){ case ((line, prev), _) => val next = inc(prev); (line ++ next, next) }._1)
|
||||||
|
(1 to 4).foldLeft(topRow, topRow){ case ((line, prev), _) => val next = incNest(prev); (line ++ next, next) }._1
|
||||||
|
|
||||||
|
def findChitonWay(unpaddedDangerZones: Vector[Vector[Int]]) =
|
||||||
|
val dangerZones = pad(999, unpaddedDangerZones)
|
||||||
|
val (maxX, maxY) = (dangerZones.size, dangerZones(0).size)
|
||||||
|
dijkstra[Vec2D[Int], Long](Vec2D(1,1), 0L, Vec2D.allCoords(dangerZones).toSet, Vec2D(maxX - 2, maxY - 2), Long.MaxValue)(
|
||||||
|
neighbours = _.orthoNeighbours(maxX, maxY).toSet,
|
||||||
|
calcCost = (_,t) => dangerZones.get(t)
|
||||||
|
)
|
||||||
|
|
||||||
|
def pad[T](padding: T, matrix: Vector[Vector[T]]) =
|
||||||
|
val hor = Vector.fill(matrix(0).size + 2)(padding)
|
||||||
|
hor +: matrix.map(padding +: _ :+ padding) :+ hor
|
||||||
|
|
38
src/main/scala/de.qwertyuiop.aoc/lib/algorithms.scala
Normal file
38
src/main/scala/de.qwertyuiop.aoc/lib/algorithms.scala
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package de.qwertyuiop.aoc.lib
|
||||||
|
import Vectors.*
|
||||||
|
import math.Numeric.Implicits.infixNumericOps
|
||||||
|
import math.Ordering.Implicits.infixOrderingOps
|
||||||
|
import cats.*, cats.implicits.given
|
||||||
|
|
||||||
|
def dijkstra[T, C](start: T, startCost: C, allNodes: Set[T], target: T, maxValue: C)
|
||||||
|
(neighbours: T => Set[T], calcCost: (T,T) => C)
|
||||||
|
(using n: Numeric[C], eq: CanEqual[T,T]): Option[(List[T], C)] =
|
||||||
|
def rec(unvisited: Set[T], dists: Map[T, C], prevs: Map[T, T]): Option[(List[T], C)] =
|
||||||
|
if unvisited.size % 1000 == 0 then println(s"To visit: ${unvisited.size}")
|
||||||
|
val current = (dists.keySet & unvisited).minBy(t => dists.getOrElse(t, maxValue))
|
||||||
|
if current == target then (backtrack(target, start, prevs, dists), dists(target)).some
|
||||||
|
else
|
||||||
|
val cost = dists(current)
|
||||||
|
val (changedDists, changedPrevs) = (
|
||||||
|
for
|
||||||
|
neigh <- neighbours(current) & unvisited
|
||||||
|
newCost = cost + calcCost(current, neigh)
|
||||||
|
if newCost < dists.getOrElse(neigh, maxValue)
|
||||||
|
yield (neigh -> newCost, neigh -> current)
|
||||||
|
).unzip
|
||||||
|
val newDists = dists ++ changedDists
|
||||||
|
val newPrevs = prevs ++ changedPrevs
|
||||||
|
val unvisitedNow = unvisited - current
|
||||||
|
if unvisitedNow.isEmpty then None
|
||||||
|
else rec(unvisitedNow, newDists, newPrevs)
|
||||||
|
|
||||||
|
|
||||||
|
def backtrack(target: T, start: T, prevs: Map[T,T], dists: Map[T,C]): List[T] =
|
||||||
|
def btRec(current: T, path: List[T]): List[T] =
|
||||||
|
if current == start then current :: path
|
||||||
|
else btRec(prevs(current), current :: path)
|
||||||
|
btRec(target, Nil)
|
||||||
|
|
||||||
|
rec(allNodes, Map(start -> startCost), Map())
|
||||||
|
|
||||||
|
|
|
@ -29,3 +29,7 @@ def flatInputF[A, C[_]](format: String => IterableOnce[A] = identity)(coll: coll
|
||||||
inputLines.flatMap(format).to(coll)
|
inputLines.flatMap(format).to(coll)
|
||||||
|
|
||||||
def boolChar(trueChar: Char): String => Vector[Boolean] = _.map(_ == trueChar).toVector
|
def boolChar(trueChar: Char): String => Vector[Boolean] = _.map(_ == trueChar).toVector
|
||||||
|
|
||||||
|
object InputFormats:
|
||||||
|
def intMatrix(using InputSource): Vector[Vector[Int]] = input(_.map(_ - '0').toVector).toVector
|
||||||
|
def longMatrix(using InputSource): Vector[Vector[Long]] = input(_.map(c => (c - '0').toLong).toVector).toVector
|
||||||
|
|
|
@ -12,9 +12,21 @@ object Vectors:
|
||||||
opaque type Vec3D[T] = (T, T, T)
|
opaque type Vec3D[T] = (T, T, T)
|
||||||
opaque type Vec4D[T] = (T, T, T, T)
|
opaque type Vec4D[T] = (T, T, T, T)
|
||||||
|
|
||||||
def Vec2D[T](x: T, y: T): Vec2D[T] = (x,y)
|
object Vec2D:
|
||||||
def Vec3D[T](x: T, y: T, z: T): Vec3D[T] = (x,y,z)
|
def apply[T](x: T, y: T): Vec2D[T] = (x,y)
|
||||||
def Vec4D[T](x: T, y: T, z: T, w: T): Vec4D[T] = (x,y,z,w)
|
def allCoords(matrix: IndexedSeq[IndexedSeq[_]]): Iterable[Vec2D[Int]] =
|
||||||
|
(
|
||||||
|
for
|
||||||
|
x <- 0 until matrix.size
|
||||||
|
y <- 0 until matrix.headOption.map(_.size).getOrElse(0)
|
||||||
|
yield (x, y)
|
||||||
|
)
|
||||||
|
|
||||||
|
object Vec3D:
|
||||||
|
def apply[T](x: T, y: T, z: T): Vec3D[T] = (x,y,z)
|
||||||
|
|
||||||
|
object Vec4D:
|
||||||
|
def apply[T](x: T, y: T, z: T, w: T): Vec4D[T] = (x,y,z,w)
|
||||||
|
|
||||||
trait Vec[T]:
|
trait Vec[T]:
|
||||||
extension (v: T)
|
extension (v: T)
|
||||||
|
@ -69,6 +81,10 @@ object Vectors:
|
||||||
if y < sizeY - n.one then vb += ((x, y + n.one))
|
if y < sizeY - n.one then vb += ((x, y + n.one))
|
||||||
vb.result()
|
vb.result()
|
||||||
|
|
||||||
|
def orthoNeighbours: Vector[Vec2D[T]] =
|
||||||
|
val n = summon[Numeric[T]]
|
||||||
|
Vector((x - n.one, y), (x + n.one, y), (x, y - n.one), (x, y + n.one))
|
||||||
|
|
||||||
given [T: Monoid]: Monoid[Vec2D[T]] = semiauto.monoid
|
given [T: Monoid]: Monoid[Vec2D[T]] = semiauto.monoid
|
||||||
given [T: Monoid]: Monoid[Vec3D[T]] = semiauto.monoid
|
given [T: Monoid]: Monoid[Vec3D[T]] = semiauto.monoid
|
||||||
given [T: Monoid]: Monoid[Vec4D[T]] = semiauto.monoid
|
given [T: Monoid]: Monoid[Vec4D[T]] = semiauto.monoid
|
||||||
|
|
Loading…
Reference in a new issue