Skip to content

Commit b605d91

Browse files
committed
day 15 solution, part 1
1 parent 80b89f5 commit b605d91

File tree

7 files changed

+238
-0
lines changed

7 files changed

+238
-0
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

day15/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id("org.adventofcode.kotlin-application-conventions")
3+
}
4+
5+
dependencies {
6+
7+
}
8+
9+
application {
10+
mainClass.set("org.adventofcode.AppKt")
11+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.adventofcode
2+
3+
import java.io.File
4+
5+
data class Point(val col: Int, val row: Int)
6+
typealias Path = List<Point>
7+
8+
fun Point.above(): Point = Point(col, row - 1)
9+
fun Point.below(): Point = Point(col, row + 1)
10+
fun Point.right(): Point = Point(col + 1, row)
11+
fun Point.left(): Point = Point(col - 1, row)
12+
fun Point.neighbors(): List<Point> = listOf(above(), below(), left(), right())
13+
fun Point.toCompactString(): String = "($col,$row)"
14+
15+
val ORIGIN_POINT = Point(0, 0)
16+
17+
interface Grid {
18+
val width: Int;
19+
val height: Int;
20+
val lastPoint: Point;
21+
operator fun contains(p: Point): Boolean;
22+
operator fun get(p: Point): Int?;
23+
}
24+
25+
data class ImmutableGrid(val data: List<List<Int>>) : Grid {
26+
override val height: Int by lazy { data.size }
27+
override val width: Int by lazy { if (data.isNotEmpty()) data[0].size else 0 }
28+
override val lastPoint: Point by lazy { Point(width - 1, height -1 ) }
29+
30+
override operator fun contains(p: Point): Boolean {
31+
val insideRowRange = p.row in 0 until height
32+
val insideColRange = p.col in 0 until width
33+
return insideRowRange && insideColRange
34+
}
35+
36+
override fun get(p: Point): Int? {
37+
return data[p.row][p.col]
38+
}
39+
}
40+
41+
fun ImmutableGrid.prettyPrint(withPath: Path? = null) {
42+
val pointset = withPath?.let { it.toSet() } ?: setOf()
43+
val padding = 2 + (withPath?.let { 4 } ?: 0)
44+
this.data.forEachIndexed { rowIdx, row ->
45+
val s = row.mapIndexed { colIdx, value ->
46+
val point = Point(colIdx, rowIdx)
47+
val attr = withPath?.let { if (point in pointset) "{*}" else null }
48+
"$value${attr?.let { it } ?: ""}".padEnd(padding)
49+
}
50+
println(s.joinToString(""))
51+
}
52+
}
53+
54+
data class TraversalEntry(val parent: Point? = null, val cost: Double = 0.0)
55+
56+
object PathFinder {
57+
58+
fun findShortestPath(grid: Grid): Path {
59+
val destinationPoint = grid.lastPoint
60+
val initTraversalEntry = TraversalEntry()
61+
val traversedEntries = mutableMapOf(ORIGIN_POINT to initTraversalEntry)
62+
val openEntries = mutableListOf(ORIGIN_POINT to initTraversalEntry)
63+
64+
while (openEntries.isNotEmpty()) {
65+
val (currentPoint, currentEntry) = openEntries.removeFirst()
66+
67+
if (currentPoint == destinationPoint) {
68+
break
69+
}
70+
71+
val neighboringPoints = currentPoint.neighbors().filter { p -> p in grid }
72+
73+
for (neighborPoint in neighboringPoints) {
74+
val neighborValue = grid[neighborPoint] ?: throw error("Invalid neighboring point $neighborPoint")
75+
val nextCost = currentEntry.cost + neighborValue
76+
val neighborEntry = traversedEntries[neighborPoint]
77+
val entry = if ((neighborEntry == null) || (nextCost < neighborEntry.cost)) {
78+
TraversalEntry(parent = currentPoint, cost = nextCost)
79+
} else null
80+
81+
entry?.let {
82+
openEntries.add(neighborPoint to it)
83+
traversedEntries[neighborPoint] = it
84+
}
85+
}
86+
87+
openEntries.sortBy { (_, entry) -> entry.cost }
88+
}
89+
90+
val destinationEntry = traversedEntries[destinationPoint]
91+
val pathSequence = generateSequence(destinationEntry) {
92+
traversedEntries[it.parent]
93+
}
94+
return pathSequence.toList().mapNotNull { it.parent }.reversed() + destinationPoint
95+
}
96+
97+
}
98+
99+
fun main() {
100+
val data = File("day15/src/main/resources/puzzleInput.txt")
101+
.readLines()
102+
.map { it.split("").drop(1).dropLast(1).map(String::toInt) }
103+
104+
val grid = ImmutableGrid(data)
105+
val path = PathFinder.findShortestPath(grid)
106+
grid.prettyPrint(path)
107+
println()
108+
val cost = path.drop(1).sumOf { grid[it] ?: 0 }
109+
println("total cost: $cost")
110+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
9996597169934462866674298311585462164938429595325898982446756778926623312419928462581954959682691967
2+
2985473821192251974951857599267129581722193984669499111891148299417367918194777369292323577917271689
3+
9925747219565218292791878884867871912812888438419913951838884629861174692194127299557997716292419899
4+
5689394257397924968615986194575967939829494536315791891189399346198354797971455482896478478991915819
5+
7191714231614914258843698935928469828947261253778874596549317179317739769211815941393178993235483164
6+
8261411311858625525189141927634584957969193998594589831161827486428181822492341148933916699735911951
7+
8491916338957614879947827918271919292115249669528752137626999289878321718995174471152853256534998862
8+
2878942979689932518788993693919232994181295141998674786229493444893529921396251916792869778369761577
9+
5324299179293789278896352999521161196335675668494898163723519189354519915572911924817148697972884595
10+
3249286984664762888964519927147388331192962971447998569588811898887938916912876838845226891917694396
11+
8384711585589932817217197266234448864986267887731451266551819546679493985489271887959192715994617118
12+
8558392988645926497661221561199187591614971862693418259749682995917799111375997911491314618563955283
13+
8448982133981186899328338219912614175929696865411141936944349293218792978158697238989149481799787693
14+
6829788549538172915212861761762811411999446212987957999818898769129995716668189729153281278992882961
15+
8171853935996999759827785274119369819474116329221912117887992386979999931924642786162346838988896998
16+
1611259796732365683997911728853115978724281285998878729487172937996381111893368427199788995529699915
17+
5688512981128461937192312761945159148391968976355979921691491771799226893997198194759896799954945399
18+
4677331887688119697949511996723791999294219225462628938768446491296191832983981819292814141771743713
19+
6998483931417251929193268819892118766128664979798963467991984184987179579514795981938448296897618517
20+
1319691989733199973528388427162273193372475922119779921892954934985598398886515997181951338365969481
21+
4299527919598836712396166784912989538495958199899819597168429291995797395512392555913678895994639498
22+
1959943162267357312789489261812239925964689121762221787667885625878744214497989669145322976481797489
23+
2395186851949597519521959999946556211159613271161993818642229693391155187199898781144194699852896652
24+
8995881734129869469494233213758985566876122341955919419837192269724184297999191789915397161175694514
25+
1589989198744781329419599199511686425468596978475911132726959429936172193239693316868625965916184839
26+
9882447993214964282121932219232574629549391691929133268994142998198147342636396881745981998993474912
27+
7737876929128219459528645811539339996618399972961981295848297621996944591699891682671676889993245341
28+
2158439725377317758939718768374247166592199417399889641427291896886226693898817697782973998951155297
29+
9887435694697319583419914934749159528953559489752368919149391989598118395465194898962938213247967394
30+
5517647144893878432868162191929768517614669477961621993427299179961283174693342728139917919748564391
31+
9779481169993213628493311246996281191589186121851592447341849279199269441668863494199994929698131648
32+
2959874885233989493331717181293295698768911881968258917777516222479221154718272378589492728986899898
33+
6336128726683932115888889787212994762911499913872419883197591582599568128238312873213867483934993736
34+
4159514962399649263976989199987979698821179147379581119235896886328996193984413131685592771291576266
35+
1593442591996141992339729558718999183319971698622191712984511552643854986614687182148724814175817656
36+
9792498287944398191893973114979339967886679599475949871199956319839493957942371899296491239678972934
37+
6958832763784733851418838934315594789797315719727432698172191667929493981747889875995275627797923389
38+
2912489714937795668149651893389788991785743292299526654245111715698898839692818936313581913792918926
39+
7195756671161364812491586641918192917591128991769216529117641729128224625938118993381221258566281874
40+
4919251993929919793698991748199857829677991159297389859596679221721172471911475458139992481162519175
41+
2254995628727981496178769799122851151827187992613599888143189293495389836799918172649522571495393365
42+
4919469549846964135387296992591431749927527554992629919141933995127179342887519454978599414789914783
43+
8249277916939616589494692899215986219117999611126787797761663947855882548552921696511335319977977276
44+
8997247891948158197997424577668119969512395999198777982793597792164299869129186219578886115816121295
45+
6941997972868952192836588639197579791529983259434179858113289553975791179662335714993722671597998927
46+
6694662791989839799999721874596512192467821383498167219689731819817638219918398599817984591678772498
47+
8858288185291451396798937151154999428339834613586898546191955738849619381719419835132493839871399615
48+
7529911675212868597977638119426882947159197216327972592891619718857968144833998898924864651272849995
49+
9691484419158964971779927177911682159384281915994991576187561796853919134899954697897563168694867221
50+
4894319635215192279188115698914991697424519884293976389911441262789983112191971613112113166199375431
51+
1511639279893277978712597619227629471932123889359895191237277184999881711199775191369185982853995942
52+
9915422489642151365815679799243528945862628419597469996679371174699116156719192447889887994175395158
53+
1269519797393822199696946291999871185118994883519698996879178838176969819961121912895184999235939982
54+
8998169189895585969989213678844886699295797199711994499817992161989593968875298663522818358585897162
55+
7927682215931182986837324955117927481866962848794911121399817493187817199924163786732968396717889912
56+
8311863849984418139391492994599723197997936818681266467612993599437842931897692691123198157196313828
57+
3912811962539933372757862888979829436791914818558936298392631514184279598868598428219711472666991639
58+
6916684642174849949229678714798199182288419234936896995119656411631851528789981973983524139588322938
59+
3899591899121528899189971692387979612889537681737837516481351153864963849151779816549651236111723594
60+
3733748925586297882817174179168829219857222829879737341141839283411189725169226158941341846729768669
61+
9789499627896993539271958577799419338952872815148576127561639829528596519399349651538276989994528487
62+
7388981178985997387282499451819991165879682469882199551165119298566527989112579262966861989646115399
63+
5991942761492919732325149912387694118188713956668248812874198274888581191798982696129299728788152392
64+
4893929462916669917284961278939751989925132199496199189137998549751266279931261714899988486719869185
65+
4997993581899573559368431987787949249487616996236465166438295477511852952993911916999513819315563739
66+
5694962633999723232519196778886841863659775543315314187999819975671489384312111125194191191689318842
67+
8117917171397291331189259254585716591711966747299169269591559696147192299722924212179199479985927778
68+
5877195869199369738399992996992351916954198321916324999535799172491147977863276571643974197933991225
69+
9989937132116396991282665553786386787251927628645189196271484472765767798223882649693739891271941718
70+
9881817923976739289587441977932118769978916716257286976119129982935796939299745998797611525399338946
71+
7971721113779639519693896636532198958154533493955284625816918678591158938299299514956545289189225246
72+
1934255841143924219963318212532349185169864629346812611498281935883975296729898789599295275471239935
73+
9389259195122479459673587535287714317984291529979184993896171591567899939324815119172791936569419171
74+
1219545154899922238148139999125192284119454489112162247977417126936591683779289118922958941952169529
75+
8931326645155712193944977992128197782593896232969361888983783158482969662613471498363997158128983917
76+
9391814334978943893899898719238927345819158859742137386199831611671627868629743974529895867689722899
77+
8899661813588998957926823145981293975684977898749619148875149895569991796276683223974512522996784281
78+
8189997341861632296117873952976155948326181517876925234797513284989779614111171824983144719982838692
79+
6829974748888281152995139169943951978697714989859236979688489367767193968499469271347883923978496486
80+
3582825929871712773829442944988717183399644827597944269949291896788936218399384642479246326487212216
81+
1281849189867928125128247997991274988592254197114976969923792926319271988297834927369539637996142862
82+
6975385386529147231116538827916688428583685119999922155687868872191934969189912616939972416699937619
83+
9142581394887759489992391495154883364165399163526193468348698682816285172516792933881369365183699737
84+
3411779799639918597997735278279522191199866497528592736267813281569999199119412823772227677998946384
85+
2116411967515514942119364428959419118117956378157649922119835139438587436793347611988988395844999146
86+
4969594274619966968618996995355173423281217195299985411254849697474491117942266985699122594883385615
87+
4917299221629599799699849452938637368998116959389958177198157236988184989389719268887182197869999794
88+
9933199734925928814814699974319532158151925514912941886226193291877951342254648694293572133944495113
89+
9987885196669594244937469588999294967538347679792413734999119397189999758851998539321185392791823319
90+
6989985129833911782351863869583492932291897395825982916227178892928481149185294531999169898771899993
91+
8181799944142196786791987474137119471971711249456315997994855914169196337574821618642899989499999967
92+
9977674398586181511429839918697885836894979877115969228276796238492289811696176629546719787979932155
93+
9649322578254321813692488189287617696897892613327148796662618417432796649759345844497523367451481152
94+
5129599141866519189896719299898197851393894869211189289921289673533589213789998748469233769479419838
95+
1914167773815939689484489593599191199453921623428226899327789139828531391877739791958192158441984193
96+
8338474519167599455272191263393519999999117471971142469799444885161861196178994691697689794932649387
97+
1559148889238879814969113114369991178858114182289991437257816149529871139727898999977787479892611941
98+
2195956622389911468295917219887214665812887892199883687933786497999277478287123418492269694943326749
99+
9812749311865999362692593879136236263719259266549637913645675293968172469772769845996496649211944289
100+
2292883723888976611993688774892793962329432919349328271848881342422189315889383193139146839498568176
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1163751742
2+
1381373672
3+
2136511328
4+
3694931569
5+
7463417111
6+
1319128137
7+
1359912421
8+
3125421639
9+
1293138521
10+
2311944581
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
19111
2+
19191
3+
11191
4+
99991
5+
99991

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ include(
2323
"day12",
2424
"day13",
2525
"day14",
26+
"day15",
2627
)

0 commit comments

Comments
 (0)