Skip to content

Commit e146417

Browse files
committed
Day 15: Chiton - finding shortest path with dijkstra
1 parent 4d50e74 commit e146417

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package Day15;
2+
3+
import advent.utils.AdventGridUtils;
4+
5+
import java.awt.*;
6+
import java.util.ArrayList;
7+
import java.util.Comparator;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.PriorityQueue;
12+
13+
public class ChitonDistanceFinder {
14+
15+
private final int maxX;
16+
17+
private final int maxY;
18+
19+
private final List<List<Integer>> cavernMap;
20+
21+
Map<Point, Point> pathPoints;
22+
23+
public ChitonDistanceFinder(List<List<Integer>> cavernMap) {
24+
this.maxX = cavernMap.size();
25+
this.maxY = cavernMap.size();
26+
this.cavernMap = cavernMap;
27+
pathPoints = new HashMap<>();
28+
}
29+
30+
public int calculateLowestRiskPath() {
31+
findShortestPath();
32+
List<Point> path = new ArrayList<>();
33+
Point pathPoint = new Point(maxX - 1, maxY - 1);
34+
Point startPoint = new Point(0, 0);
35+
while (!pathPoint.equals(startPoint)) {
36+
Point cameFrom = pathPoints.get(pathPoint);
37+
path.add(pathPoint);
38+
pathPoint = cameFrom;
39+
}
40+
return path.stream().mapToInt(pointInPath -> cavernMap.get(pointInPath.y).get(pointInPath.x)).sum();
41+
}
42+
43+
private void findShortestPath() {
44+
Map<Point, Integer> pointDistances = new HashMap<>();
45+
pointDistances.put(new Point(0, 0), 0);
46+
PriorityQueue<Point> nextPoints = new PriorityQueue<>(Comparator.comparing(point -> pointDistances.getOrDefault(point, Integer.MAX_VALUE)));
47+
for (int y = 0; y < maxY; y++) {
48+
for (int x = 0; x < maxX; x++) {
49+
nextPoints.add(new Point(x, y));
50+
}
51+
}
52+
while (!nextPoints.isEmpty()) {
53+
Point currentPoint = nextPoints.poll();
54+
{
55+
List<Point> neighbours = AdventGridUtils.getNeighbours(currentPoint, maxX, maxY);
56+
for (Point neighbour : neighbours) {
57+
int distance = pointDistances.getOrDefault(currentPoint, Integer.MAX_VALUE) + cavernMap.get(neighbour.y).get(neighbour.x);
58+
if (distance < pointDistances.getOrDefault(neighbour, Integer.MAX_VALUE)) {
59+
pointDistances.put(neighbour, distance);
60+
pathPoints.put(neighbour, currentPoint);
61+
nextPoints.remove(neighbour);
62+
nextPoints.add(neighbour);
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package Day15;
2+
3+
import advent.utils.AdventFileUtils;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class Day15Main {
10+
11+
public static void main(String[] args) {
12+
List<List<Integer>> inputMatrix = getInput();
13+
System.out.println(inputMatrix);
14+
15+
ChitonDistanceFinder distanceFinder = new ChitonDistanceFinder(inputMatrix);
16+
int lowestRiskPath = distanceFinder.calculateLowestRiskPath();
17+
System.out.println(lowestRiskPath);
18+
19+
List<List<Integer>> newInputMatrix = increaseInput(inputMatrix);
20+
21+
ChitonDistanceFinder distanceFinder1 = new ChitonDistanceFinder(newInputMatrix);
22+
int lowestRiskPath1 = distanceFinder1.calculateLowestRiskPath();
23+
System.out.println(lowestRiskPath1);
24+
25+
}
26+
27+
protected static List<List<Integer>> getInput() {
28+
return AdventFileUtils.readClassInputIntoIntegerMatrix(Day15Main.class);
29+
}
30+
31+
private static List<List<Integer>> increaseInput(List<List<Integer>> inputMatrix) {
32+
// todo: refactor this ugliness at some point ¯\_(ツ)_/¯
33+
List<List<Integer>> newMatrix = new ArrayList<>();
34+
for (List<Integer> row : inputMatrix) {
35+
List<Integer> newRow = new ArrayList<>(row);
36+
List<Integer> increaseWith = findIncreaseWith(row);
37+
newRow.addAll(increaseWith);
38+
int count = 1;
39+
while (count < 4) {
40+
increaseWith = findIncreaseWith(increaseWith);
41+
newRow.addAll(increaseWith);
42+
count++;
43+
}
44+
newMatrix.add(newRow);
45+
}
46+
47+
List<List<Integer>> currentSection = new ArrayList<>(newMatrix);
48+
int count = 1;
49+
while (count < 5) {
50+
List<List<Integer>> temporaryMatrix = new ArrayList<>();
51+
for (List<Integer> row : currentSection) {
52+
List<Integer> increaseWith = findIncreaseWith(row);
53+
temporaryMatrix.add(increaseWith);
54+
newMatrix.add(increaseWith);
55+
}
56+
currentSection = temporaryMatrix;
57+
count++;
58+
}
59+
return newMatrix;
60+
}
61+
62+
private static List<Integer> findIncreaseWith(List<Integer> row) {
63+
return row.stream().map(element -> element >= 9 ? 1 : element + 1).collect(Collectors.toList());
64+
}
65+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package advent.utils;
2+
3+
4+
import java.awt.Point;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class AdventGridUtils {
9+
10+
public static List<Point> getNeighbours(Point currentPoint, int maxX, int maxY) {
11+
List<Point> neighbours = new ArrayList<>();
12+
if (currentPoint.x > 0) {
13+
// left neighbour
14+
neighbours.add(new Point(currentPoint.x - 1, currentPoint.y));
15+
}
16+
if (currentPoint.x < maxX - 1) {
17+
// right Neighbour
18+
neighbours.add(new Point(currentPoint.x + 1, currentPoint.y));
19+
}
20+
if (currentPoint.y > 0) {
21+
// above Neighbour
22+
neighbours.add(new Point(currentPoint.x, currentPoint.y - 1));
23+
}
24+
if (currentPoint.y < maxY - 1) {
25+
// below Neighbour
26+
neighbours.add(new Point(currentPoint.x, currentPoint.y + 1));
27+
}
28+
return neighbours;
29+
}
30+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
4395194575929822238989941988598994946581953236424841813955769288219282998336161199274582725132193662
2+
1715719228759138638397684864996679719167258159599658826174926447916886499139963731181569842792979836
3+
5641957947399279962631596818774779918898868615738299911674929785663922472189972649893918935926989965
4+
2585497851318911812329521892518876883669883794214786673934129871993567882759933254875129861732949942
5+
6326121293629918828545199576444799485199997872984968987116189717399321966789925619859799917896971919
6+
1197891193163157699998465137799499428622885997715629793728799611991922169771186415959393796833328797
7+
9971941176236517711968176833694122359799994332972933319744917758263654615939671218999722759897946883
8+
8999294422711677672138234871949951867361994331595339178487499997983949131794946775549222972399289896
9+
3349291493887252933563637858998292429461181979119894978282958316785791165839546981395193797183659157
10+
7189439835616936936452916284895714389978973983979946899115361688238479929831181859649911588342717174
11+
9919687931991844963923118455824186376858799159986659568599889942949751127924998243532126861192227813
12+
8556752119948211196297781958736978151712384292658126551391659754132796214991112228251319499881769877
13+
1979947819698244184997611639891169294112917898297946132711657818979459522579998285949272941189651219
14+
8526183359487314556663171586821716949298464911898919115161199393481771956882329857552411954641939441
15+
1781919896684119688349391339192196799588645991242746996794955718159288456491121922268271789679196351
16+
1197567977371916661139687299725917634957272938365923151889699976438687129398228799597921729929992714
17+
4612932372899778339369954694952999219672973397661799332114944198582981641791452579999989699742756964
18+
6999162799681597281641651777891979395397835152949992426916756338121612297639319849519239816897924832
19+
2929523873329969919578959424699989299758674993329912629488597872529465316591581194999996221889681928
20+
8228656689838938962979299798868965176111786181396381619674278311396838891887432267914997982584599249
21+
3291189727977984366996998628998999313854947791298837533988923314799893553121114286295795551978824232
22+
1969899739799949932911885277583879342673999999696225694755379718993111283179891261733382476218558941
23+
4139678172235724118199726211119671191998665963518891164122317193835651793851195981494569791148964119
24+
8491859444877458918921985959989568279219722914284989216186773895898218317795329987999133881714858193
25+
2771894559749588998196893858679239272818243739928684589924781981773791798991892831999869587961311167
26+
8698775892993989977451194464865721268484216875311866222111987933418291995941958137194897996649415669
27+
9815831789813838789211849713941639192861814785979938811825669343313889446468439829866799197356199957
28+
8391217969784868593822994733189558658223987532137877169778153978669662784849729448991221591737912928
29+
1286923398138799667979629228969771916586553188988688124597199878412159468838995398755911831124947458
30+
1114829212648462813962865127721383493959596596219921932831948169799141594693396881528179473168999246
31+
8229274989319485131759119628887993878981894123939971871579472951958128273735938172877719459979616966
32+
9499516139937388199431887411757519697113346973918231323926539878654177977781846978129796428916386862
33+
4976998292388572314456265734789687366686748398788899297684913137779698579739992995231592719964862978
34+
2298177775583527949898397959425767832825991552239939999392925878819892226485971811999876289184259875
35+
3598947472448691635139959991352673979496318718231961399989962929352899269143994934946299795998128783
36+
7112174898192377964819299748639161162263488897467958394886818188981289197818113459389985525596199819
37+
9987787819769819287382267885519776534891123996871992292115788186562861498191537481789591841846186882
38+
4932928968769839163293479931958989358947715254682158299764179952675896919281473293998826971956719781
39+
9992216325832365211598714373826949876761198899344963795899592692291996816722739297999357184959969777
40+
8299492186897712559157176181191385891631253997468944991976578985988414979191999779899999718894199893
41+
9599634119932298479281989351167939998231691111219295175188529818284912884666292812879581588751975666
42+
9893411889151989895121929229639869648431989868714855378743883971235628399646641369715828182779286514
43+
8939515728674611191619883917891998789585382765378965697452669997798898499199181191539751395986113886
44+
9985666981399318453128358762184119989474811319212718761249919671981988848392275694674398146181416198
45+
8193892754125669889688695711759861961188261942984936153189888489199611811681269985959959968811384341
46+
8299917437379188117917628771687562449889987984991225519931596159725913681979787939969976651741749698
47+
8285297977988918992157859998969934917144295338397889967636236876746919995248975499929976279814914931
48+
9129499129593186731916693856178488768855292651679735946159317969489119952269991769981967997489693511
49+
7186637914671953873792124571983949693887419197239989992917386832793799278886872648567913786197639677
50+
9667487981719871696217751899991188952177479933799965849967487849683858162517919957637748993181344613
51+
4798696188279263112441176132921127173319858683256972916675925279917946959468161918992879929186429996
52+
1875628214911419676889344457796569819463131975985822779517679476958819822619947289779578492991712138
53+
9729987223699868162597477587276969892989969399943929635225993221991199187491492967948629623789668744
54+
9916111911755127191948896779129983784992661981998299971599989198761289123181443629292169995715974949
55+
1291917878889332658369889135368111113759781552671763529821597651871398558593277892399994199844188899
56+
8857659891128611558849143993177991511814858869793389498556946631129988799922937638978992359857839357
57+
9498927473319717382175369484488993971151116118963956979372884748999382475159478948297342397888172486
58+
8838311993693897959989699851182958819758515849648216159982472145775919486496793748386329827875114795
59+
2646323635456985931835993825299725819616464197571881778646919914938572377378887919941193912366189981
60+
4192777568853894179559191115999836896832987112829422358328999616261299876112799681459689496973841216
61+
4182522211326928934549829947681273461197958221628297222967289879895821718559188491763567998193425175
62+
9941452899981779489899453416943116824565113896695891691999972969869313999989231351588275691638731173
63+
4279199328962391513117193251941896142249199541185874189116658753849872967489875833815972485753775799
64+
2692166517168484891439119972374885161996829937192277189246449949998287236914964598317589854294191818
65+
9691899795175315938351268169518149836746199789392955191246577759192964575629739479919199793594669346
66+
4928854341918568913973278223498427458884195453535861967991191927617293996995778496126372492999583688
67+
1869735122593925853281477159999674511464912381748647211595414615939131393112325927225913832758726989
68+
8673156999937894893938221988172251959669284384873549417558434149199712793826799147878341238872169799
69+
3786244397665631994388328169996499172551393184212436839161983999116249888235795513969896377319987648
70+
3299841254618376148274689819516491144697712179196959898422484829331951929556251191292922792943475111
71+
8979685787892899891511168966657683893794989939899716589793811126757987574288699799754629917678927199
72+
5291969299298572611431791117595444556315389932888323856938713114932929293397712995429591559999339269
73+
5571919139695338299758473188558593632658195263118579497481394132986981877919799978847911917349971951
74+
9447171299311825169951839995999391971743173914736767913282767941796338297879485196369168312316114619
75+
8965937476731877998867589955999781971348978541526728915989299973637949395212351839999632681871119852
76+
7154818129168364927991293583982982964961899491189199159915998987259614876919693993277919976424821339
77+
5863889995599382179127198593932798869731787297368813494929776247986339942885147596969931861841289387
78+
8291299218914728523912933158961272669279911995972979992183926289243118311811961558131192613197929186
79+
8961913468988371188879695178682994979962878269287243998918854923819591848296141587688276923892888765
80+
5496139281898589971291971391186884911787687869879279111147189911627194976288937484497924811159764722
81+
9875395289318577859812192389977827241588661341589921792229379945659193616899945199691585158611997752
82+
9897872534836989139768375684846127999667957297219249999952812149314798361868111441889987449192925623
83+
9138263544984999193841859937147586599945426897796171621827994461911885818295944939999799882269659399
84+
6914856489683269528879792799877759191976961973971938559173939387419513299922889959976555221521424697
85+
1715191986151162997669282366919988483318991958172785998996643612786988727959277619145619891677993797
86+
9942283447167676428491385853611565159691399722133269739927118911631657887991431834279716429273499866
87+
4799972923699768188516165542947747617263791192642512198299894199889995551779128118616736724861699788
88+
4958919625759529361425491889628879199991912868912939317496198948944989139197771291619455967185949537
89+
2223977898998997958945226793679996855922957491361985987152262111657894546195945683493497719917994917
90+
2169115388563391991925213886793982979499187969173993849967129291479453796965869487632218484794186428
91+
8181979148789991463199414217818677797879614992639757619958818656897787992873919194129981917198598753
92+
7298898662149861558411999137899133141939277162229327197891618169842134976193137994322715445297289799
93+
3295636966178697931489122998118891198718116639897914658911799996899497168942241323161993721164174647
94+
1198816571471558697411816978791898982893995992519493175459877178916533784561911924112689959897991892
95+
5618962846185938989183714393155171999657459292691693826899689462277599981997875426218165946485993981
96+
9347291669523992969899819288568678793199991148799181869131292289712895996712494789999957779656718174
97+
1211541591292966678912459115993944851245448935997663676619889855226196181522818261837465192373166996
98+
1797339189173753393939899772787971418172195729879466938989599928482964996915184691991154998224136189
99+
3421551897114792849579376681776925441448992599913934985689149424792976946888488111919491919499858968
100+
1971473988843177925911719936118862197889673997928889139314775919195258317837961344298715732432565457

0 commit comments

Comments
 (0)