wrong 15 part two
authorMart Lubbers <mart@martlubbers.net>
Wed, 15 Dec 2021 13:43:31 +0000 (14:43 +0100)
committerMart Lubbers <mart@martlubbers.net>
Wed, 15 Dec 2021 13:43:31 +0000 (14:43 +0100)
15.txt [new file with mode: 0644]
15b.c [new file with mode: 0644]
15e.txt [new file with mode: 0644]
15e2.txt [new file with mode: 0644]

diff --git a/15.txt b/15.txt
new file mode 100644 (file)
index 0000000..c8b996c
--- /dev/null
+++ b/15.txt
@@ -0,0 +1,100 @@
+8795561346959611984851795223993199426691978637498615942935297722897942579728189182383276311836891971
+9971917929239749392313919138788785878436257678478978964999643688839227886859465518439985489795943919
+1519761672617778785983978488277814115999116451991978869729122618211379791865816152742849499151214682
+9897768578325969757223986999919271436414939289747483386572979869519481591978913852845739134918128698
+6131844837515911671618346728935389713865862689991891968992859599212148719512981655915843896819419699
+9994158898369771516892943629594768792167999156447986693115896589979189597554487929195418898275991939
+7899714915991971118529915219739988517292138842395144993792669384948991998684649619885511938319926529
+8439945422238997549198746429761826973391669898967884389843379199899887495381477519258959688562189998
+8193158996336869219329798167599696253894399636289548599616776971684358719751199457412129189416339337
+8915467845679571319719627819399444276933882997919488941218392246725699575931358682988728811692994888
+2183821795618759299397119999912447724899136432675238876256982162673596561194541917989927328994884775
+8789678487971129849969177111293971322992799168799694179116768117969482793885681672825369745192292798
+9899995799751915683316369897193479341895969161197819529318324971956766824967977979682349271859692499
+9738389836681141541749679428151189548958914789921712997989991986792969749119449656969797779397987971
+9699386193977846925881916878943588579248899536993991794576926484969985986958761239832944749381396877
+2938918897233812797889785112782764111371659727455889948919937998893948984527919431599729431858423312
+6669951996546696289498264116385727972556973497368864657695549198987126379129178799142291929595944531
+9593327798991199813978669827818164893823995153184287992392877622665469666148268197736278655385853893
+4489883181443655445696668196922477872145911769238794459937988771294974551878994626812499591419882438
+9828537889259279861788699459666699933159822299169969994311839953287658281994334947114298299449399739
+4188562547592911131922397722799988999499427811169546472448222946122399994149695698195849512899141156
+7812927129675599119818993368441873851158999359195519823173938847593874317319938262871877982994151854
+8925469217741929237517277969589678521292549411978981218821255391918872181337782152599971935434586641
+5539173242188516261195962935992419699921211771998999829366384756736985679791323524894919899791751927
+7997814295618599112279785547892749118824587414992484558129913948987899996139582741731588699495997181
+9294699723167493823939899799875297999693938976152179786642196948529943458129635959926986719188788299
+3382299747968914256996638119179579489495894917369311729612961928153965889817885929465121747699993923
+1269933289896689643116489371719798989991587811658981468139317814827819327181648686519922623159872716
+8749822282959139712191898935929168514293911199984129154669719688839883778199899299699619241169648787
+7789461829886475855754227981617959439666958496998884492729328586718954493775988189711859281328961118
+1996981999456693988591117919251641821968898256445988799178396189162182741171111899482722939388958511
+3281911384597899419698914798194911889589839273747496947197364795912194969393957795992989291837591999
+1293812999287868589289214241179594649119899992343411877615638772289591199319196215849889992628429262
+2598787636835395941715619539222694792812975975481748539877868612571891699419489899939987379366597698
+4781462274896758851786199964998495994884799928116913548959881394813979886818979751785863571721749154
+9781899957313649191946278938943668952725533114949839896192161387789132989919592489457498991499145634
+8939291595887684897787947956711653882547657238349261375815578499482296127299179887193666585829749989
+3987392781148476892679669911899929328939559696918985892627927769683956664885969479599936511452997261
+9718491979119959846848498131157967812971882896718922983832319776899132516188992612996481749999992686
+8919943758399972755642585691411913377667687791139364126793319649518996979999699899691164918399189872
+7377178689183729718471814618846971671488612597556998591379995391219797917687997492569812892162829899
+6198691883598199239981463965166178891395862717797769949534348818992699479916324441967852879113914389
+9216989165552946188295851854481689328373995258899376993895872229212951841639956946995929695927819249
+9188886992578419341444721993392494111118832893996694812192621891797989996585963719519899668496979761
+6178959769221537294292836517919989929381873925111177297994731171813548771847917883712814928381315828
+3716898729898898116791923615175892681249798369195979923194893399955242992199314988739418974466288117
+6712671172426933931999899491979987495165963185198717561866882185885895989659986798139793799819433118
+4584379959151684983916286557263499959997582313992956794499595279918993992689611978329772677199695197
+9983899196819771699641681959945138993831935591154986184953899351941365399295712466533478897952142168
+6899162163198818566972762787289989382494938538296469493317537999999195467999754271988141381266789918
+1452932263797497952199118888137861961329529437698719177689997249821121712191719777639699188944321936
+8937299999674997371979425811822714647182423856472824694292841198359827339725542547754913932519964957
+9994946989869977763295691999117176543478371299189888681177844256187727685388616713239797198891771592
+7554973739824921451596999899131383498318934996922147599937751455999839159799296692986193923888918716
+2183293736212193119141897255991593533998965599699986684333511369999247828115626787252781948997399355
+7166988753117411929413382997948886498187158558318973167282939868978289154979461968949393388198748635
+3848991198627879899197562857896159183619819718874617129918483191133991967698128553471899459892397929
+2765314119181599176729133659328796299792767869687928921291778482422185719921369149813699944966311469
+8482691159398923761627882926982881117188945644896835748384958683499176311775975826629528934976436159
+8629967953659486936456699817244387935152911955491258868396712994893218779896713818697822473611899472
+9869569395962994217619893792989865797229926372388199383181753799972387947281751917911412193198648899
+2911975492385168179852911515718689961977743548821737615588468287984829778119138931993495899999411139
+6627692176997581928496111176845898128798398982139541832937981149719315979978979925639977479916288297
+7971686592726999387593121969616452677783738499693549294716146947144911287158388446849188523198921159
+9654356698192284395828218447993789572441791288511865165275957319726133989295828997899981479293199367
+1983295918471185535848787124187154967288585591968899954121721297447389146121365419765845984999697995
+3772299933945255981887183847913843946135928498137753611472573189883189388898147899899311559517594526
+6935119987289199372691298387496561978547581187375589699139538917889149981989973389296691993898137585
+9596981933155656783964173864653743482911961278323228972783896794973182687777999797879485992688899864
+7946745867994687182819593938168758438993987764997671339742591814996711459998829197381873761169833199
+7998761391174687853825798296855419379326297812911135911535769961499179699318147216113263387981953698
+6975995197822218189738184719494471188758894169771883498878198461499469547885896968499963693355941716
+4351931919415521116137351999816179752593389213165382519362992963377712912373918797312697527115653474
+4219916669692263574989959697799511818886799247369637226524896979114739798437327795849691299932928316
+9469815173589995599191717511929851241512129287419528975628498911949738259719499364539555799899987749
+8479915281919179814239178416812398824865776973192848267616874181477979169919153488172116912595685919
+1289893698599996944651792299632392688252343961243995892218884283891981753441377799862931899681885987
+1918997431886381819198395396582984757593892248817191118278199825665792383989396744979898488291819787
+1593953997936192775781629434971795219615284969133998265488117988899576939192195943941191263898454787
+4116191989544268918925399499117319147593767434896144251686491796711286886419319219768919724488339918
+5724615913863188944231161142188759223298646797932191661684846867251942817233499629528599748994238819
+9568989891141898878889593392781881437292997772591628197913688948429735171975377969524966319878852198
+1451975884978888899851513598956919574939335964999412895192972398975389988737989199973987816587729912
+8266921296948786193491779253862131431671987882857169537999157578838774176914698161799177147198972998
+9899977419231149419726862744398113392793194994238576864681993162199811196897926857981885899436634991
+8863975882112417779869387818526982917699448259848195932976179866278536198163191794669219691194232985
+8964676952961795683996858297181963591827634248715774196944191928959643811872914981968142899983398969
+3819129921898559367111958191971712538989297786949383519349717524859963945439921799892281989822321974
+6843888714292337419966941942419999999577112282934993189218898131672285193999999299488947947756888796
+4897177883982914929221319627191749689347559979399184189239223629815698729511514457552661229971845791
+7951497555381399499653191719458486151898656381679791193539839579318194299745115665134795986878886958
+8999796933696665971996778728127922964493478822898217169185834914999629245188781933911948789292989969
+9196969381136611468918217972388437845173999195912928197355989571899481559772197996243198878913819631
+7166988339662186997128362499643382919978799844892892946796715324518121699785894695193874729916993919
+9298281551968695911458882111884212373479298692144188529258731314391839459899983882393129697999711159
+8558438898992511996283529341897299969324918789817899999965999939196128992393994111199692651355275955
+7689899924699293736392725716697734919419784152918395812482316549583957919989329617954687594936783898
+1169796382849566792128293597457371455827987519511199778291917942178294599929539841167862969924979581
+9496929588378869969158122779414228929724985988984299699786358995268989949878981121159539739996566173
+9791763949837198338991393152592978229948887552199237768112426143281161433828316999916998589965921948
diff --git a/15b.c b/15b.c
new file mode 100644 (file)
index 0000000..65d8081
--- /dev/null
+++ b/15b.c
@@ -0,0 +1,105 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <string.h>
+#include <search.h>
+
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#define MFAC 1
+
+struct point { int x; int y; };
+
+int grid[100][100] = {0};
+int maxx = 0;
+int maxy = 0;
+
+int get_grid(int x, int y)
+{
+       if (x == 0 && y == 0)
+               return 0;
+       int r = grid[y%maxy][x%maxx] + y/maxy + x/maxx;
+       while (r > 9)
+               r -= 9;
+       return r;
+}
+
+int dist[100*MFAC][100*MFAC];
+
+int mindist(const void *l, const void *r)
+{
+       struct point lp = *(const struct point *)l;
+       struct point rp = *(const struct point *)r;
+       return dist[rp.y][rp.x]-dist[lp.y][lp.x];
+}
+
+struct point *is_in_q(struct point *q, size_t nq, int x, int y)
+{
+       for (size_t i = 0; i<nq; i++)
+               if (q[i].x == x && q[i].y == y)
+                       return q+i;
+       return NULL;
+}
+
+int main()
+{
+       int c, x = 0;;
+       while ( (c = getchar()) != EOF) {
+               if (c == '\n') {
+                       maxx = x;
+                       x = 0;
+                       maxy++;
+               } else {
+                       grid[maxy][x++] = c-'0';
+               }
+       }
+
+       struct point q[100*MFAC*100*MFAC];
+       size_t qp = 0;
+       struct point prev[100*MFAC][100*MFAC];
+
+//     grid[0][0] = 0;
+       for (int y = 0; y<maxy*MFAC; y++) {
+               for (int x = 0; x<maxx*MFAC; x++) {
+                       dist[y][x] = INT_MAX;
+                       prev[y][x] = (struct point){.x=-1, .y=-1};
+                       q[qp++] = (struct point){.x=x, .y=y};
+               }
+       }
+       dist[0][0] = 0;
+
+       while (qp > 0) {
+//             printf("size of q: %lu\n", qp);
+               qsort(q, qp, sizeof(struct point), mindist);
+               qp--;
+               struct point u = q[qp];
+               for (int dy = -1; dy <= 1; dy++) {
+                       for (int dx = -1; dx <= 1; dx++) {
+                               //Skip diagonal neigbours
+                               if (dx == dy)
+                                       continue;
+                               //check if v is in Q
+                               struct point *v = is_in_q(&q[0], qp, u.x+dx, u.y+dy);
+                               if (v == NULL)
+                                       continue;
+                               //Update dist if necessary
+                               int alt = dist[u.y][u.x] + get_grid(v->x, v->y);
+                               if (alt < dist[v->y][v->x]) {
+                                       dist[v->y][v->x] = alt;
+                                       prev[v->y][v->x] = u;
+                               }
+                       }
+               }
+       }
+
+       printf("path: ");
+       struct point u = {.y=maxy*MFAC-1, .x=maxx*MFAC-1};
+       do {
+               u = prev[u.y][u.x];
+               printf("%d,%d ", u.x, u.y);
+       } while (u.x!= 0 && u.y != 0);
+
+       printf("\n");
+
+       printf("%d\n", dist[maxy*MFAC-1][maxx*MFAC-1]);
+}
diff --git a/15e.txt b/15e.txt
new file mode 100644 (file)
index 0000000..ab80887
--- /dev/null
+++ b/15e.txt
@@ -0,0 +1,10 @@
+1163751742
+1381373672
+2136511328
+3694931569
+7463417111
+1319128137
+1359912421
+3125421639
+1293138521
+2311944581
diff --git a/15e2.txt b/15e2.txt
new file mode 100644 (file)
index 0000000..4469f3e
--- /dev/null
+++ b/15e2.txt
@@ -0,0 +1,3 @@
+19999
+19111
+11191