-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15_2.groovy
67 lines (60 loc) · 12.2 KB
/
day15_2.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def inputs = [
"2963924119481127152315549141231231178123994331851932272412441692851653891219119354456482964197268714", "2125898928818174266137112211175698633271838994151281976712281111698717796156549261335965222291569163", "8111185636613134811732598421968521115422392923197154966144825217611454445719142229524413998144151911", "3962657499999133116799939121116659728843742371812415949114112811191119631998123361711522626139129171", "6111135446425897553993611911343415171253137374519612644715145982129413191192199133421576676147421524", "1711672712467328162521961114611891129216275453292129417212429121384919912329928499114733518591698113", "9818123947466132628284152218324212939785378646971725918917928244119747535812775619624751855161318981", "1167117151118391264548783563849236287283591453964124132191355313219327921631216122146335183389221113", "4519416121144292832972521591771225196983398212493418121299943189472967713311611419393716191418524821", "8428151118121833231318216491191252126818777326142251911129592186434264163213111772928317934268148824", "3165873554832211849131719721524511318259881915976248976322219137369799967164281286516811242517129121", "2885812131756932666817582491177655654972619112119823253481651239931563118125311134911194239141712113", "1291116939138958141123114115522514155273133336126316131953879219441958934525282324651984231814137524", "3268713319383431269751193953123199316712461891774923811111264211537915123118993921211467341232291328", "1494182213459282199711916513717111332986532263139142817996233779117813262222529217149131145579428468", "2697351273997219318622991361717868167136617226119591728729254459578918512181825139842913972912163292", "4649244299282136159214235411642232639958861769448683538531845519299865524111181139421177419587127323", "3543182111143716348275153911855164846384145338299561311328131821251217621151319111511699119734389151", "1411255412612863712513518224791192112451112527213113392657715893271915912124213141141728382477174219", "1122359691112221519865261462121166815581294962184246211532888923125388424211111231193234163228112195", "4145311618243811248861591879898912931516871146131541116423751167138243961469252993912619943298276422", "5519429121442891167257251264111755556712414151352261193746111467972254111413747978231599118162372126", "1145417418587941219111122661693121921584919867311581225392119698218211721651155429112564168135511612", "9272947191812116588892122924349111264991911921243382199126482683561953362213319365121813155131526123", "1319879121918199619161129114929963711297731884167115243399519143324794317951793699892983118561139599", "5219569819981113221826649462842329152547163921411151796321246417257397174726111213439143122368211929", "7433314232514278111791692212222141991821512493966321182757959252126975721484136154521951579966271383", "6183193371915321413778211632498384352524162353452472391192336211241298999113233394515299724168612632", "2171187924623111338949875111717141996916117194788252183429391811177116196211223887389131993353836119", "2427431145431214912646266117461139266791132812163271871582692132181281333982283121185351311671961732", "7491294482479151766224145236386125216711884297315141695131819197293112171942399123776191157911719242", "2312795118911362931858691259199491268939388187295115137789286422151941147891481552489951411812771875", "8728128518985727114695196739113737192138971877757418196989711136115261531193916179116619555115593174", "2437292141241911215663815132791734799189986346884172122226227542372176911191511212724129943993813258", "5143637349912324371399545879655434124114319477255218854426226265112281437412581879885241819693584912", "2918132961135361433861971115732219541356911118191458773129276291815452246633124741622956471241413411", "3358883915111144822193192371322767731113316212311549441994571291191148155689112458991122915191512216", "3472828377837922171439763612815912836184211899512144991182757124172785288425791974933195292229419473", "9312344493111981221146294231577844264931391591262321229929412191181412451712132494391147715581712231", "5242921113833146422321116237121944441324126151339341962229158496699156121162624151917846199113417154", "9111433331111131511783875372178968411614292753191491238321224117331953599875112965148598421852176214", "7676146111963915194834928161116131217363169742316492947111287331356725317119231391111778171342914621", "1241219521118785326941741341283132219954151623962411139243911141626912274612913134114614271594863391", "1225999831224818222899928212112294846796324438431115785179796893262432881179412821629974381961375987", "2441733648223323632211841997337585739133244143336941181131641161192511143112548993292321719217319121", "8197214411129343882115114341141231831313531251311215141556924994623311426284835715113422274915111255", "8112111411411188918626613121987732491495914714471922861746692891391111932427419461155523924183386676", "9457114128973786341636631365312621532724211532275218921232483157174391119144317973299154629929911378", "8568973311838958182135225318992538413324456893281182152131616516212196258272422812113142351432139829", "1151245356495957534618717791531191142979395331311255323191241511124316191149786311623783151132271233", "2573329419615152133414186194563141193195252214478259131113127749193244612992158761143169378112369384", "1789242241173451911132921261222616192454614331112611233292953119971219366535385631123433225293818345", "1712112516593319843843721133199755947153531389137923421211117718112938545111119683883317134282451252", "1638913431327233991136864919114114637169545572922189945115136541257349192311963769611395656119463131", "9476696421793114612412933381216139311113789476898116335224621717243513531294477171946859546669651684", "8291919992224747618218541242788893119518189867345681191151816917721271241157535119296242221873229149", "8129191712726492624592192969288339192171629764991579128318228622521441171125163556151497172694123642", "2274193372268113971813783846592121861239763317191131222194912227181887352942312234152523246621522223", "4989714935253862379645391833156921163528823111132192997621791388151611311953985114718593811415992969", "4114543412474713854581256239114488136161617211921132397225112919539553181922444211568424937918313233", "4321997791248291511323181718727424264141492527191599451158916211153561435793775723481242131951117878", "3141624621141142829115675442781395199165327128119113111427928134561132191172221192131114373238278894", "1436369141244297392911282951124321291577442289322132342891162116569495117936111631121429297975984693", "1136414688934754511429831499198781149372952259153397889274815321491856291593124232311922212669488113", "1139726679711311941445347273915627817932697262443122282211331981721123191261287193221111458462297716", "5145715783977232812179114758297324822118732461421818716268178365269349993116511996657831213931138716", "8911112529112428235811151619841481971632515758821129986197181257941924177171111579345893883213144319", "1533516979222585826196339685581226411851867235111794919279817518111698495324117982934114644111174913", "2427619429228199127364352251614923498591512172511549283193634118539641151597811113212611531211427372", "4391562917135271183375912347134291921223213322741221714198818941541122679221183112413891338673451686", "5816218837159791472213912118214177276315123771186191828771361946316984681123334926733953334748912312", "4211123378911812691411632131549957118335159131242151943814431172125514614576256319998212691733198994", "4881671259137283493119591411334267139531812257129362391682951512194131112191236941716342113723958434", "3833361112428656261411696233613914131312691141443411698325222161236291332449941461152916148398528969", "8131112411242367163188922181162241478993554981981221935919242523311122653417299113343337831565311258", "5895182118929141396121594511616115435763719711111113894125828191258132211311138468939744843492912247", "7144111134826912511161811217685262814191334219182489398551818219152431226224123183631984931184183372", "4195144292979335248257615561516487811468285119624141238373789528521274262281272123216149211299533113", "1695473611111397223452174852161199198822182353923819191277472291111331191589413263371461154226792559", "2411821831658188921528925292821243197313418212319626468179452815841139215514938176113361992358117979", "2265541891391719417142211171154132191382397142192143718996182412473991312582138122312193171917921373", "3938296731281129116874875235345983132541838512815428139113921133443582161118389154125168113611532565", "9193163331242719294145814558495219281366952353141172262611248557418162443194113895378175219131221551", "9588761535552171917283384268991982211352784122965222733817888215929299311213312111391964154251118713", "6181885511251531911143555976611786936961191774655474241117114811611343896561257258462991255136363916", "3218981915119391113152927292912117416454311341536651811493215531911969426383427419442928431113113881", "9611126171255393759974871579112733881512941411291547161191542445994431191832897184514747372443667912", "8215324734491116217991914215387819134813571479328348611572483551124954737319792868739552222216844889", "9621273332813128573851531389146611214291117214681251126233582538167194812868538144651122111647817261", "2311268637889271984781675958267525174131895351126257124913812499272978912532529191161792651591218799", "1791895438174699617919372768272131214632361111231123842192443111322458381135598421287538118754211113", "5119941142588899458221193224889891711971133936959141932619843951146381188251646116924942941374541319", "8946124462529731516737692687431961611313517721516641349352972464213215959182523429221414181351454112", "1921113112191196288811151721211198122137546721131528131726923331364438661672956415429339368552752258", "1541182251973219762685321771167787229213992239262119115712924929473677281179814422976341948517111145", "3149114383884893421125991511925388342254478482277218125926311321138253285731115981948969419996115998", "2552163226832289218492121329118371413823225491511648691321196294227282112871439293121832612219957896", "3579354332165976422137212582298381255921918919331618842389499267415333331721624611777953113159341971", "1921191727117521919328245116217413116523112387129363375913911283241371194923613231252581732271345529", "8236441711172181739335885718511173168384583225866471231914531215429515772515312136392519922717888144"
]
map = inputs.collect { it.split('').collect { it as Integer } }
bigMap = []
5.times { time1 ->
5.times { time2 ->
map.eachWithIndex { list, mi ->
def toAppend = list*.plus(time1+time2).collect{ it > 9 ? it-9 : it }
def index = mi+map.size()*time1
bigMap[index] = (bigMap[index] ? [bigMap[index], toAppend].flatten() : toAppend)
}
}
}
map = bigMap
pathMap = [:]
leastPath(0, 1)
leastPath(1, 0)
turn=0
while (true) {
def changed = false
def last = [-1, -1]
(map.size()-1..0).each { x ->
(map[0].size()-1..0).each { y ->
if ([x,y] == [0,0]) {
return
}
def origin = pathMap["${x},${y}"]
def option1 = map[x][y] + (pathMap["${x},${y-1}"] ? pathMap["${x},${y-1}"] : 9999)
def option2 = map[x][y] + (pathMap["${x-1},${y}"] ? pathMap["${x-1},${y}"] : 9999)
def option3 = map[x][y] + (pathMap["${x+1},${y}"] ? pathMap["${x+1},${y}"] : 9999)
def option4 = map[x][y] + (pathMap["${x},${y+1}"] ? pathMap["${x},${y+1}"] : 9999)
pathMap["${x},${y}"] = [option1, option2, option3, option4, origin].min()
if (origin != pathMap["${x},${y}"]) {
changed = true
last = [x, y]
}
}
}
println("${pathMap['0,1']}/${pathMap['1,0']}/${last}")
println("${++turn}: ${Math.min(pathMap['0,1'], pathMap['1,0'])}")
if (!changed || !(last in [[0,1], [1,0]])) {
break
}
}
Math.min(pathMap['0,1'], pathMap['1,0'])
def leastPath(x, y) {
if (pathMap["${x},${y}"]) {
return pathMap["${x},${y}"]
} else if (x >= map.size() || y >= map[0].size() || x < 0 || y < 0) {
pathMap["${x},${y}"] = 9999
return 9999
} else if (x == map.size()-1 && y == map[0].size()-1) {
pathMap["${x},${y}"] = map[x][y]
return map[x][y]
}
def risk = map[x][y] + Math.min(leastPath(x, y+1), leastPath(x+1, y))
pathMap["${x},${y}"] = risk
return risk
}