diff --git a/day10/c/day10 b/day10/c/day10 new file mode 100755 index 0000000..687c3ec Binary files /dev/null and b/day10/c/day10 differ diff --git a/day10/c/day10.c b/day10/c/day10.c new file mode 100644 index 0000000..0902302 --- /dev/null +++ b/day10/c/day10.c @@ -0,0 +1,102 @@ +#include +#include + +#define MAX_DIMENSION 128 + +int get_trails(char**, int, int, int, int, int**); + +int main() { + char c; + + char **map = calloc(MAX_DIMENSION, sizeof(map[0])); + + int rows = 0, i = 0, columns; + map[rows] = calloc(MAX_DIMENSION, sizeof(map[0][0])); + + while ((c = getchar()) != EOF) { + map[rows][i] = c; + i++; + if (c != '\n') { + continue; + } + + columns = i - 1; + rows++; + i = 0; + map[rows] = calloc(MAX_DIMENSION, sizeof(map[0][0])); + } + + int **reachable = calloc(rows, sizeof(reachable[0])); + for (i = 0; i < rows; i++) { + reachable[i] = calloc(columns, sizeof(reachable[0][0])); + } + + int score = 0; + int rating = 0; + // For each '0' + for (i = 0; i < rows; i++) { + for (int j = 0; j < columns; j++) { + if (map[i][j] != '0') { + continue; + } + + rating += get_trails(map, rows, columns, i, j, reachable); + // Count reachable + for (int k = 0; k < rows; k++) { + for (int l = 0; l < columns; l++) { + score += reachable[k][l]; + reachable[k][l] = 0; + } + } + } + } + + for (i = 0; i < rows; i++) { + free(reachable[i]); + } + free(reachable); + for (i = 0; i < rows + 1; i++) { + free(map[i]); + } + free(map); + + printf("%i\n", score); + printf("%i\n", rating); +} + +int get_trails(char** map, int rows, int columns, int row, int column, int** reachable) { + if (map[row][column] == '9') { + reachable[row][column] = 1; + return 1; + } + + int x, y; + char next = map[row][column] + 1; + int trails = 0; + // up + x = row - 1; + y = column; + if (x >= 0 && map[x][y] == next) { + trails += get_trails(map, rows, columns, x, y, reachable); + } + // down + x = row + 1; + y = column; + if (x < rows && map[x][y] == next) { + trails += get_trails(map, rows, columns, x, y, reachable); + } + // left + x = row; + y = column + 1; + if (y < columns && map[x][y] == next) { + trails += get_trails(map, rows, columns, x, y, reachable); + } + // right + x = row; + y = column - 1; + if (y >= 0 && map[x][y] == next) { + trails += get_trails(map, rows, columns, x, y, reachable); + } + + return trails; +} diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..3f29526 --- /dev/null +++ b/day10/input.txt @@ -0,0 +1,54 @@ +212521982345455432198898732343201001454321076767899872 +101430671156766718089087001432102122367410789898721701 +234549560049859809670106126549893432198523650127630652 +345678432132343218543215437456784543007694540134544543 +434989010101034567894326458941089656912784343296531014 +123968523218954326287478947032178767843895256787432543 +037879654305869210156565432142189626756706101208956632 +010968760456778981432101943443076212389617890312347701 +323459821678765476583201875654561003476526345423698801 +011234734569012345694112766743432876565430256784510932 +010345654554212014785073450898101986676321100898929841 +143456703623103423456787321287632987685672901289838750 +212769812514398512987896544379443456894589874356745669 +301898453405487600121095434568543670123601065105210178 +498782345654676321436784323877612589010892110234312321 +567101908723521030545654012981003438756763323987603430 +323877819810437841690343021542312321065654332109544543 +014966521923456956781252120455401487654505445678037652 +565455430810398105898763214345566590123218764789128781 +678321876760567234237454905216697687689439653234569390 +549050945651456340145667876107788014578321342106778434 +432167834512301254206758989038979123065430211067884521 +212058723003456763219843210123460132104321203458990690 +103449012124567895430764505674321043895650432143021788 +214530101013098986721256034985452344786766501032134659 +345621232322121234890340125676965435689897865401235678 +238789985421030345234543234767876105476101974321945234 +129376576534781676167652149866765256785232389450876165 +023403456215696787018967019875454343494341071265210074 +016512567304345698101278112562343852014556780374391189 +187876458412234598790349603401438961025698895489580543 +098962389561103347685456784876547873234767216785671672 +123451049870101256576545692910687210189854306894502981 +212945432943232345677834501431296332182344345663213870 +301876501854569431988925232321345345091103216756344561 +676510345763078520123810123410543456780234109865432150 +783401256762107019654320294567632109876542106776843001 +892313879856234198734521287678932323438943769089987612 +341054965447895287645632789456541010567659858123656543 +250123832332196014532745610367692101298365647654567698 +167814001541087123691821001298789854303234737843238787 +078905123456789234780934789656730763214159826943129898 +980876432965410165690695678749821278934067015652010789 +801986501874321074321783265637832123653458234761001656 +212567988965010787210654104521945034562109101891012347 +123498677654321298323545003010876453078045610123456798 +034014576103432186789276512123289342199236769032347810 +145623985412545085652189467898100256787100898741016921 +898767234307696198543011056967221105107231239653455430 +745678101268987585654322343254339012218774381012768741 +234989089456789676789113698107448763329783498019889650 +101345674301410566541004567898758954458692567325676341 +013216765210323455632123476127667763067501101234765432 +322109876323454556789012981034554892155432101289876501 diff --git a/day10/sample.txt b/day10/sample.txt new file mode 100644 index 0000000..a305b9d --- /dev/null +++ b/day10/sample.txt @@ -0,0 +1,4 @@ +0123 +1234 +8765 +9876 diff --git a/day10/sample2.txt b/day10/sample2.txt new file mode 100644 index 0000000..db56de0 --- /dev/null +++ b/day10/sample2.txt @@ -0,0 +1,7 @@ +...0... +...1... +...2... +6543456 +7.....7 +8.....8 +9.....9 diff --git a/day10/sample3.txt b/day10/sample3.txt new file mode 100644 index 0000000..17655af --- /dev/null +++ b/day10/sample3.txt @@ -0,0 +1,7 @@ +..90..9 +...1.98 +...2..7 +6543456 +765.987 +876.... +987.... diff --git a/day10/sample4.txt b/day10/sample4.txt new file mode 100644 index 0000000..185787f --- /dev/null +++ b/day10/sample4.txt @@ -0,0 +1,7 @@ +10..9.. +2...8.. +3...7.. +4567654 +...8..3 +...9..2 +.....01 diff --git a/day10/samplee.txt b/day10/samplee.txt new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/day10/samplee.txt @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732