diff options
author | kartofen <mladenovnasko0@gmail.com> | 2022-12-09 23:29:28 +0200 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2022-12-09 23:29:28 +0200 |
commit | b284257038b6d4777de7c6ef5499e8b7c64b53b5 (patch) | |
tree | 256b0f105f0775e206aed8536bbd5a28eea16cfb | |
parent | c5a8824311e7c814f58593f43b0528505756f46a (diff) |
day 8 and day 9
-rwxr-xr-x | Advent-of-Code-2022/aoc-8/build.sh | 5 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-8/input.txt | 99 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-8/main.c | 124 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-8/sample.txt | 5 | ||||
-rwxr-xr-x | Advent-of-Code-2022/aoc-9/build.sh | 5 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-9/input.txt | 2000 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-9/main.c | 113 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-9/sample.txt | 8 |
8 files changed, 2359 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-8/build.sh b/Advent-of-Code-2022/aoc-8/build.sh new file mode 100755 index 0000000..30f495e --- /dev/null +++ b/Advent-of-Code-2022/aoc-8/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic diff --git a/Advent-of-Code-2022/aoc-8/input.txt b/Advent-of-Code-2022/aoc-8/input.txt new file mode 100644 index 0000000..e658b1d --- /dev/null +++ b/Advent-of-Code-2022/aoc-8/input.txt @@ -0,0 +1,99 @@ +120121010220011210213341221400410032024200305455123332224004344232032410401431102230313221032220201 +201110121023013230302330021311131013421112311431532122324332345241340200121101343122032000302222002 +102002033003222123334224022231014024154332332522532413152443343521420024142120221020312130000012220 +022021002022122234142411233442032215244333211545534421223521213425452321323211314230012132230101111 +000001213311312040044040324004345251313532532244214111234445435523443223231014031402202003220202022 +120012020112324402101203201012524422234555211555441254543532525542455422431100123100134112211321001 +201331122323231022322414132513445443542225314522442122134451213343255351243403333214100001221310002 +202213230323304224323401211524355443322423124143363225331551434545141211212254401343122332131333320 +121211210204211420223411323113111133452563332665665366256342425412125353114131102200130004332132023 +112113312123342311122121342553114544542544233526256565633442525642534431424122340140222330201020323 +212220201231231331225325433524212465354244363465433323422435453425443544233314142214213330210013003 +323312311034244021124115523324235453334553635453645456346262262243535541114542314510442100141301122 +322110024020203105422444554225662245423642666363652244366624233565255245323234242444442021114003131 +020022410424334054535554351356634265526252563433644446436343335353246322341442512355214000224142031 +113011113224033331332141536246552466454462445665475466223436433443654356455435321342422024224240233 +122102233311322113321454362252243234624432447544375657355353324635433643234333542125422313433332321 +233120110314034111141332425232564555665674743363443764643543745663655355633235321453124432104042231 +100223100402324212124236262364446355557657673733745547363557734565242545543336131354254512432331033 +220330034134143234154666534352252567443643755654677737776567374445733425452244254511151514234122213 +223443144015521314236554452665635663377377657447643476573534645467643254356634655544313443004430033 +101024341052411454165252566424674556674773657353656433343654447535674354245262332333523555242331132 +220222110223533215564454533347744755673537673333455433773653447746564747422325642363122431352422211 +004240214354322434225324663557754746553337448764574547564453643767763554322626552333144243243100443 +244002112413445223342664642343745646463566545585488848785555544433334435346654656363551511425432311 +304400052221222432253436664545656765657858858457776876475466485467576564653534555232555135554512103 +301034122214253266644646654774435447685475487674646855754787758675544354374563635536335552414541214 +023143512353534433525535355774437554848684458474566677886556568485755464753466263454354215342330021 +114124535331116466652546536664743784557757457584466558864655558574887544735673255226463153414244423 +321342532351563246464464534353744766487574566878456865566554674786854545747634762245326241111545302 +222324535212433454342577763447374686575568646767976677984686747686474557575435676242252334324424133 +041455115451655624645633347335876745564448989976988776878785466666874474733445456453534561112433131 +234035233445234443233733563648557487657498899968897866857865785856886577346464734446452554244252101 +042322443436343444646433663564848665688656875758665978859999876545768558637766334653546264321513150 +311341431232552555557665566457765567779965958979678879768989766764576764655455375556543222315153443 +432422413526624252456643546576745546967687555668559776976867857598647675565463476444325456215124324 +141355224535323342657744536454664777877655975965957569957665675698786568684357375345633653521322134 +141122432443266326344466678445475489879775756757768969966588589795858867477644375464354434554334542 +404524251535424526674473367658588859789659999696879679797895959568796848845577655453633456432522251 +052555241454534576467457557666865778585785777799889798799796659588785886875774465345553622342424344 +431231215564564335455644575547778969999599889767689766876767676769986454648577766665523563621423244 +015225326665656474355776784465595855559969779697867888768786867958987787865555557547456236256414443 +342141412264436237555367786748757775996887869887989697887868985969768974878458377746343563334112432 +245424112423634535653674854786759556887899698796699686769888766789698865458448564436356633653255423 +152344536556354376777665885575687899789896978768778768999866666875968688488776547446465633626145213 +143323326635362775675356868684997875878998968799998887969968886965597768474646637337765355426415522 +233135153446345733457544575578758698668698796778899989988686677699869766655775463667634536224515421 +442322432622254677457747586645559867668696769899897879878696967979757585566766656433333444234655425 +211425433422356366455578477849687667769687699987878799877668866765868769745448755533566453422341433 +142142345456557463367644747779979677698996689878999779799897968888567959674764537364645446523214543 +112243356444435356743678858455568578689876999787888879899876878888997787884868645546363254345642533 +341213364343435537345688745769797686679769679778778788899769986867599798667874534334444664434313333 +243532164222666764774486748579698698876689888987878887998777687869755585776544863447444553624622141 +341332522665223377644645444775976687977778897889989999977786997965895889866888467355775645335253141 +451243166652422747755745765448975967698978698799997898999688997896678767488644763664752333224424553 +343541133453346465373775878855568559988786799997787988999967996859859695684886756457573444332411142 +314533526655524646634488455785868658876797968989787988796698776878757988467468647665474336362332241 +442414135564436647537445687767866699679676668888999999798976797688656765447854434566735624532545551 +032213154226544776557438554788789576777666889966978788867789879775988668444584736553534343622315541 +255412122466665773547546685885987799779967977989879687777878699876566957766566647736545453255421212 +211412336445563474775766856667577998798997678997686866778969879788687858487546455537653524366143412 +051255123462256533566636777744877655896767699778989868868796978589575976844685335654756444462141342 +322542554464342265336333465776645696986799889779679986879686787697697647574587563753453625343545521 +033123425643433264635737446877447585887867896898797976879886786698688684876443473335322466665351333 +133544314253462427775743366646484968555695958697698797876679667978854648774634475773653542364235421 +224252431244343247466566786445767556659899789679866787689999766568777856867437767535434222445521233 +125411234266255436753533664888768586696657765695958586799598696979557774846357447354325235425345222 +321143533344454465635634634548656884666775656786776897656977675678544868467454563642465352614111534 +324314254344466535257774447774685768796778758589556968777657876755454678874457376763662322355442241 +120211113135325666447367553357474668876977665968588879588965668457446445756555755344632353524213111 +311224554131565262327577435344586647744599595585679959557779868848845756333753447342232421323535422 +114341442141636522627743367646645776654857585789589599759987575646778664434443566622565323551144104 +210244242413152456645637357357684777785854766967688795794848665476646666547365474664225335235523134 +124135421531242254525555475654745664788867846468845666746887888854654564764543562353564341341354241 +232040414531142663343623666666355755588686458756864548766766867588477574653643266543552344453423142 +241410112355426236262654356375777777866756675768668766578744448586537335565654443353226124123350230 +140311131221515422466665656675435767464774857745654575455565568556777765663432626225661213413111223 +110443321215553263625546467777476774558756864846764586756488688436634346356345662654634352234124332 +221343332131351442346665233764775367674745555446558654675758467463467434543336344234234541352014440 +133230443252342342426444235644376747467676654877885765875675433747574653525324245321411114352213140 +040240221445511125623332465444547333774437463744467533446475535636346363436524235541352245340433401 +204040003033154341135523356453644337774767676777576456764567576775643742526424535545245441222304411 +304042224012435531346652244635426465733436576763444573475575775466734463356433425322423121010012441 +020411330415234333353346655465454535766463345353547737546577344756646645346366361232312212001341120 +222312310221442455555216363452342336634374577563373335557775555536643536565552525532355153044340022 +321033011221232232531512333546323244633765663543646734377575566264364252534433124442412113401004401 +221333321104001131445331226542565353624544444743335737563556662324243523642455442214142202003030231 +031021212444432454341232542543223433534244654624443452522654546656624352422154453544520212142411231 +000032101021330433425441534355232524355526522443326623545256546525633243254355513511514204131400330 +010223333430411314211115534412532344324626322226423546334526362632342264344512422433334011314221121 +030012220233010021431154541112223663556346352526365422265452334332256431412154223121433241032331112 +221110003130033344202155154114455435426433642224264344325523445244211424335321121000334310001120112 +010013223234422000001143241135132415264644343446443425245345324221223223212325552323312333412330222 +032103213130121140030443255125332243343336336555656433255235254313433424224542501100302101221320301 +112201332000400003333114425525244134115212434522644332332513434523545134353120230443131202022212112 +212133330330243143424430334431513443235234452344525122433141135525522345433024123302104122132331020 +110220302112033431311324214242235434551543155442332335242312153314452535320343340301332123320021320 +121223310122302143024320402120433335414432343211234251415554145543214421134023034143302200002123012 +221212001102321134443014113311424542341431224511531255544142141422243112400414323421322323011122100 +102101010031312320301133133420124425352551441553552411331314122515423420100020312041230123102220212 diff --git a/Advent-of-Code-2022/aoc-8/main.c b/Advent-of-Code-2022/aoc-8/main.c new file mode 100644 index 0000000..ee36c26 --- /dev/null +++ b/Advent-of-Code-2022/aoc-8/main.c @@ -0,0 +1,124 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +#if 0 + #define PART part1 +#else + #define PART part2 +#endif + +#if 0 + #define FILENAME "sample.txt" + #define SIDE 5 +#else + #define FILENAME "input.txt" + #define SIDE 99 +#endif + +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) + +int ans = 0; +int table[SIDE][SIDE]; + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not opne file: %s\n", FILENAME); + exit(1); + } + + char line[256]; + int col = 0; + while(fgets(line, sizeof(line), fp)) + { + assert(col < SIDE); + + for(int i = 0; i < SIDE; i++) + table[i][col] = line[i] - '0'; + col++; + } + + fclose(fp); +} + +void part1() +{ + ans = (SIDE*4) - 4; + + for(int y = 1; y < (SIDE-1); y++) + for(int x = 1; x < (SIDE-1); x++) + { + int cur = table[x][y]; + + // row left + int rl = 1; + for(int i = 0; i < x; i++) + rl &= cur > table[i][y]; + + // row right + int rr = 1; + for(int i = (x+1); i < SIDE; i++) + rr &= cur > table[i][y]; + + // col up + int cu = 1; + for(int i = 0; i < y; i++) + cu &= cur > table[x][i]; + + // col down + int cd = 1; + for(int i = (y+1); i < SIDE; i++) + cd &= cur > table[x][i]; + + if(rl || rr || cu || cd) ans++; + } +} + +void part2() +{ + for(int y = 1; y < (SIDE-1); y++) + for(int x = 1; x < (SIDE-1); x++) + { + int cur = table[x][y]; + + // row left + int rl = 0; + for(int i = (x-1); i >= 0; i--) { + rl++; + if(cur <= table[i][y]) break; + } + + // row right + int rr = 0; + for(int i = (x+1); i < SIDE; i++) { + rr++; + if(cur <= table[i][y]) break; + } + + // col up + int cu = 0; + for(int i = (y-1); i >= 0; i--) { + cu++; + if(cur <= table[x][i]) break; + } + + // col down + int cd = 0; + for(int i = (y+1); i < SIDE; i++) { + cd++; + if(cur <= table[x][i]) break; + } + + ans = MAX(ans, rl * rr * cu * cd); + } +} + +int main(void) +{ + parse(); + PART(); + printf("%d\n", ans); + return 0; +} diff --git a/Advent-of-Code-2022/aoc-8/sample.txt b/Advent-of-Code-2022/aoc-8/sample.txt new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/Advent-of-Code-2022/aoc-8/sample.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390 diff --git a/Advent-of-Code-2022/aoc-9/build.sh b/Advent-of-Code-2022/aoc-9/build.sh new file mode 100755 index 0000000..30f495e --- /dev/null +++ b/Advent-of-Code-2022/aoc-9/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic diff --git a/Advent-of-Code-2022/aoc-9/input.txt b/Advent-of-Code-2022/aoc-9/input.txt new file mode 100644 index 0000000..b9aa23e --- /dev/null +++ b/Advent-of-Code-2022/aoc-9/input.txt @@ -0,0 +1,2000 @@ +R 1 +U 2 +R 1 +L 2 +D 2 +U 1 +R 2 +U 1 +L 2 +U 1 +D 2 +R 1 +D 1 +L 1 +U 2 +D 2 +L 2 +D 1 +U 2 +D 2 +L 1 +U 1 +L 2 +D 2 +U 2 +L 1 +U 2 +L 1 +R 2 +U 1 +L 1 +R 2 +D 1 +U 2 +R 1 +U 1 +R 2 +U 1 +R 2 +U 1 +D 1 +U 1 +D 1 +L 2 +D 2 +R 1 +U 1 +R 1 +U 1 +L 1 +U 1 +D 2 +R 2 +D 2 +L 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D 14 +L 1 +D 1 +U 13 +D 7 +U 13 +D 15 +U 15 +L 6 +D 7 +R 15 +L 10 +U 6 +R 7 +L 13 +U 3 +D 9 +L 3 +D 10 +R 14 +D 15 +L 6 +D 4 +U 11 +L 11 +D 6 +U 8 +D 15 +R 5 +L 9 +D 15 +U 15 +R 9 +D 11 +U 7 +L 9 +U 8 +D 2 +L 4 +D 11 +U 12 +R 11 +U 5 +L 12 +U 5 +R 14 +L 10 +R 6 +D 3 +L 13 +U 6 +R 10 +D 10 +U 9 +D 3 +R 7 +L 13 +R 15 +U 6 +L 13 +U 11 +D 9 +L 7 +R 8 +U 6 +R 12 +U 2 +L 8 +U 10 +D 11 +U 14 +R 3 +L 15 +U 9 +L 2 +U 2 +D 11 +L 11 +R 3 +D 14 +U 3 +L 1 +U 2 +D 12 +L 5 +R 5 +D 10 +L 16 +R 4 +U 3 +L 6 +R 9 +L 7 +R 2 +U 15 +R 6 +L 12 +D 16 +L 16 +D 15 +U 12 +L 6 +D 9 +U 8 +L 10 +D 11 +U 4 +D 12 +U 7 +D 14 +U 1 +D 10 +R 10 +D 14 +L 5 +D 6 +R 8 +L 16 +U 7 +L 12 +R 13 +L 11 +D 12 +R 5 +D 11 +L 5 +R 4 +D 15 +L 13 +D 10 +R 2 +L 16 +D 14 +R 15 +D 11 +U 2 +D 12 +L 11 +D 1 +R 5 +D 6 +U 13 +L 10 +R 1 +U 10 +D 12 +L 10 +D 10 +R 11 +L 10 +D 11 +L 3 +R 11 +D 12 +L 6 +U 2 +L 10 +D 9 +L 11 +U 12 +D 14 +L 6 +U 16 +D 2 +L 2 +D 14 +U 7 +R 12 +D 14 +L 4 +U 3 +R 16 +D 2 +L 5 +R 10 +L 1 +R 11 +L 14 +D 3 +L 17 +D 4 +U 1 +R 8 +D 8 +L 5 +U 1 +L 13 +D 15 +U 17 +L 1 +D 14 +U 4 +R 1 +U 2 +D 14 +R 10 +U 14 +D 17 +R 10 +U 13 +D 12 +U 8 +L 11 +U 17 +R 12 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 +L 5 +R 16 +U 9 +L 5 +D 15 +U 2 +D 7 +L 15 +U 1 +L 16 +R 8 +L 13 +R 3 +D 8 +R 1 +D 8 +L 10 +U 16 +R 16 +L 2 +U 3 +D 9 +U 12 +D 1 +L 10 +D 15 +U 7 +R 3 +D 15 +U 4 +L 6 +R 14 +U 13 +R 16 +U 2 +R 9 +U 15 +D 8 +L 8 +R 17 +D 3 +L 17 +D 13 +U 8 +D 11 +R 13 +L 3 +D 2 +U 14 +R 1 +U 10 +R 11 +D 16 +L 10 +R 17 +U 7 +L 13 +R 3 +L 18 +R 5 +U 7 +L 12 +D 4 +U 5 +L 17 +R 18 +L 4 +R 19 +L 12 +U 17 +R 2 +L 11 +R 9 +D 16 +R 19 +L 18 +U 11 +D 4 +R 13 +L 18 +U 13 +R 19 +D 3 +R 10 +L 3 +R 5 +D 2 +R 2 +L 18 +D 15 +R 19 +L 5 +D 4 +U 5 +R 12 +D 15 +L 13 +R 16 +U 16 +L 6 +U 12 +L 8 diff --git a/Advent-of-Code-2022/aoc-9/main.c b/Advent-of-Code-2022/aoc-9/main.c new file mode 100644 index 0000000..00945eb --- /dev/null +++ b/Advent-of-Code-2022/aoc-9/main.c @@ -0,0 +1,113 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#if 0 + #define KNOTS 2 +#else + #define KNOTS 10 +#endif + +#if 0 + #define FILENAME "sample.txt" +#else + #define FILENAME "input.txt" +#endif + +#define SIDE 1000 +#define OFFSET (SIDE/2) + +typedef enum { + NONE = 0, + NORTH = 1, + SOUTH = -1, + EAST = 1, + WEST = -1 +} direction; + +int board[SIDE][SIDE] = {0}; +int rope[KNOTS][2] = {0}; +direction rope_dir[KNOTS][2] = {0}; + +void move(int k) +{ + rope[k][0] += rope_dir[k][0]; + rope[k][1] += rope_dir[k][1]; +} + +void parse_dir(char d) +{ + rope_dir[0][0] = NONE; + rope_dir[0][1] = NONE; + + switch(d) { + case 'U': + rope_dir[0][1] = NORTH; + return; + case 'D': + rope_dir[0][1] = SOUTH; + return; + case 'R': + rope_dir[0][0] = EAST; + return; + case 'L': + rope_dir[0][0] = WEST; + return; + } +} + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME); + exit(1); + } + + char line[256]; + while(fgets(line, sizeof(line), fp)) + { + parse_dir(strtok(line, " ")[0]); + int n = atoi(strtok(NULL, " ")); + + for(int i = 0; i < n; i++) + { + move(0); + for(int j = 1; j < KNOTS; j++) + { + int hor = rope[j-1][0] - rope[j][0]; + if(hor > 0) rope_dir[j][0] = EAST; + else if(hor < 0) rope_dir[j][0] = WEST; + else rope_dir[j][0] = NONE; + + int vert = rope[j-1][1] - rope[j][1]; + if(vert > 0) rope_dir[j][1] = NORTH; + else if(vert < 0) rope_dir[j][1] = SOUTH; + else rope_dir[j][1] = NONE; + + if(abs(vert) > 1 || abs(hor) > 1) + move(j); + } + + board[rope[KNOTS-1][0] + OFFSET][rope[KNOTS-1][1] + OFFSET]++; + } + } + + fclose(fp); +} + +int sum() +{ + int sum = 0; + for(int i = 0; i < SIDE; i++) + for(int j = 0; j < SIDE; j++) + if(board[j][i] > 0) sum++; + return sum; +} + +int main(void) +{ + parse(); + printf("%d\n", sum()); + return 0; +} diff --git a/Advent-of-Code-2022/aoc-9/sample.txt b/Advent-of-Code-2022/aoc-9/sample.txt new file mode 100644 index 0000000..60bd43b --- /dev/null +++ b/Advent-of-Code-2022/aoc-9/sample.txt @@ -0,0 +1,8 @@ +R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20 |