solved day 1 in python, and day3a/b in c... maybe i'll free() later

This commit is contained in:
kirreen 2023-12-04 22:15:58 +01:00
parent 16b99e9837
commit fc2392d3b8
21 changed files with 8781 additions and 2 deletions

20
day1/day1.py Normal file
View file

@ -0,0 +1,20 @@
numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "hoppasfanintedenhärstringenfinnsiinputfilen",
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
file = open("./input", "r")
sum = 0
for line in file:
first = 999999
last = i = 0
index = [0, 0]
for number in numbers:
if (line.find(number)) != -1:
if (first := min(line.find(number), first)) is line.find(number):
index[0] = i
if (last := max(line.rfind(number), last)) is line.rfind(number):
index[1] = i
i += 1
sum += (index[0] % 10) * 10 + index[1] % 10
print(sum)

4
day1/input.bakeasy Normal file
View file

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

1001
day1/output Normal file

File diff suppressed because it is too large Load diff

106
day2/' Normal file
View file

@ -0,0 +1,106 @@
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define MAX_GAMES 101
// cubes, R, G, B
const int cubes[3] = {12, 13, 14};
const char *colors[] = {"red", "green", "blue"};
int getCubes(char *ptr);
int main ()
{
int gameID = 0;
int gameSum = 0;
char row[160];
char *ptr;
int length = 0;
int gameCubes[3] = {0, 0, 0};
int gameHands = 0;
int offset = 0;
int game[MAX_GAMES] = {0};
for (int i = 1; i < MAX_GAMES; i++)
game[i] = i;
int powerSum = 0;
FILE *input;
input = fopen("./input", "r");
// for each row
while(fgets(row, 160, input) != NULL) {
// increment game count
gameID++;
gameHands = 1;
offset = 0;
for (int i = 0; i < 3; i++) gameCubes[i] = 0;
printf("%s\n", row);
// not neccesary to decide length when we know amount of ';'?
// length = strchr(row, '\n') - &row[0];
// change each ; to \0
ptr = row;
while(ptr = strchr(ptr, ';')) {
ptr[0] = '\0';
gameHands++;
ptr++;
}
printf("\nGame %d hands: %d\n", gameID, gameHands);
// for each 'hand' (semicolon) ((NULL-char))
for(gameHands; gameHands > 0; gameHands--) {
printf("%s\n", row + offset);
// for each color
for(int i = 0; i < 3; i++) {
// use strstr to find color,
// move pointer to the beginning of said colour
ptr = strstr(row + offset, colors[i]);
// if (strstr(row, colors[i]) != NULL)
if (ptr != NULL) {
if (gameCubes[i] < getCubes(ptr))
gameCubes[i] = getCubes(ptr);
}
// higher than colors[i]? invalidate the game
if (gameCubes[i] > cubes[i]) {
game[gameID] = 0;
// printf("Game %d invalid\n", gameID);
}
}
// pointer = strstr \0 + 1
offset += strlen(&row[offset]) + 1;
ptr = row + offset;
}
// printf("Game %d required RGB: %d, %d, %d\n",
// gameID, gameCubes[0],gameCubes[1],gameCubes[2]
// );
powerSum += gameCubes[0] * gameCubes[1] * gameCubes[2];
printf("Game %d powersum: %d\nCurrent Total: %d\n", gameID, gameCubes[0] * gameCubes[1] * gameCubes[2], powerSum);
}
for (int i = gameID + 1; i < MAX_GAMES; i++)
game[i] = 0;
for (int i = 0; i < MAX_GAMES; i++) {
gameSum += game[i];
}
printf("Game ID sum: %d\n", gameSum);
printf("Total power sum: %d\n", powerSum);
}
int getCubes(char *ptr)
{
int num = 0;
// get two chars: pointer -3, pointer -2
// convert them to an int
if (ptr[-3] >= '0' && ptr[-3] <= '9') {
num += (ptr[-3] - '0') * 10;
}
if (ptr[-2] >= '0' && ptr[-2] <= '9') {
num += ptr[-2] - '0';
}
return num;
}

Binary file not shown.

View file

@ -2,7 +2,7 @@
#include <string.h>
#include <limits.h>
#define MAX_GAMES 101
#define MAX_GAMES 100
// cubes, R, G, B
const int cubes[3] = {12, 13, 14};
@ -36,7 +36,6 @@ int main ()
offset = 0;
for (int i = 0; i < 3; i++) gameCubes[i] = 0;
printf("\nGame %d hands: %d\n", gameID, gameHands);
printf("%s\n", row);
// not neccesary to decide length when we know amount of ';'?
@ -49,6 +48,8 @@ int main ()
ptr++;
}
printf("\nGame %d hands: %d\n", gameID, gameHands);
// for each 'hand' (semicolon) ((NULL-char))
for(gameHands; gameHands > 0; gameHands--) {
printf("%s\n", row + offset);

BIN
day3/day3a Executable file

Binary file not shown.

203
day3/day3a.c Normal file
View file

@ -0,0 +1,203 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <ctype.h>
int rows = 0;
int columns;
typedef struct {
int pos[2];
int sum;
int len;
} number;
typedef struct {
int length;
int listSize;
long unsigned int gearSum;
number numbers[];
} numberList;
numberList *findNumbers();
numberList *addNumber(numberList *list, int sum, int len, int col, int row);
int readNumber(char **schematic, int *len, int *col, int row);
unsigned long int checkNumbers(char **schematic, numberList *list, int rows, int columns);
int main ()
{
int columnsc = 0;
char c;
numberList *list = malloc(sizeof(int) * 2 + sizeof(long unsigned int) + sizeof(number) * 8);
list->listSize = 8;
list->length = 0;
FILE *input;
input = fopen("./input", "r");
// see dimensions of file
while ((c = fgetc(input)) != EOF) {
if (c == '\n') {
rows++;
if (columns < columnsc)
columns = columnsc;
columnsc = 0;
} else { // I could probably not do this every row but w/e
columnsc++;
}
}
// allocate memory for the char array array
char **schematic = malloc(columns * sizeof(char*));
for (int i = 0; i < columns; i++)
schematic[i] = malloc(rows * sizeof(char*));
// read the file again to fill up the array
rewind(input);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
schematic[col][row] = fgetc(input);
}
fgetc(input); // discard \n
}
fclose(input);
// just print the damn thing
// for (int row = 0; row < rows; row++) {
// for (int col = 0; col < columns; col++) {
// printf("%c", schematic[col][row]);
// }
// printf("\n");
// }
printf("\nRows: %d\nColumns: %d\n\n", rows, columns);
list = findNumbers(schematic, list);
printf("Found %d numbers.\n", list->length);
printf("Total sum is %lu.\n", checkNumbers(schematic, list, rows, columns));
printf("Gear ratio sum is %lu.\n", list->gearSum);
}
// Test gear: count how many numbers are around it, if 2, multiply and return product
int checkGear(char **schematic, int col, int row)
{
int numbers[2];
int foundNumbers = 0;
int ratio = 0;
for (int y = row - 1; y <= row + 1; y++) {
if (y < 0) y++;
for (int x = col + 1; x >= col - 1; x--) {
if (x >= columns) x--;
if (x < 0) break;
while (x < columns - 1 &&(schematic[x][y] >= '0' && schematic[x][y] <= '9'
&& schematic[x + 1][y] >= '0' && schematic[x + 1][y] <= '9')) {
x++;
}
if (schematic[x][y] >= '0' && schematic[x][y] <= '9') {
if (foundNumbers >= 2) {
foundNumbers++;
}
int len = 0; // TODO: Really not needed
numbers[foundNumbers] = readNumber(schematic, &len, &x, y);
foundNumbers++;
}
}
}
if (foundNumbers == 2) {
ratio = numbers[0] * numbers[1];
printf("Values: %d and %d = %d\n", numbers[0], numbers[1], numbers[0] * numbers[1]);
}
return ratio;
}
// Why is len a ptr? because I need to return two values duh
int readNumber(char **schematic, int *len, int *col, int row)
{
int sum = 0;
while (*col >= 0 && schematic[*col][row] >= '0' && schematic[*col][row] <= '9') {
sum += (schematic[*col][row] - '0') * (int)pow(10, *len);
(*len)++;
(*col)--;
}
(*col)++;
return sum;
}
// for each number in list, check adjacent fields
unsigned long int checkNumbers(char **schematic, numberList *list, int rows, int columns)
{
unsigned long int sum = 0;
int previous;
for(int i = 0; i < list->length; i++) {
// make sure col or row is never too high / low
number num = list->numbers[i];
for (int row = num.pos[1] - 1; row <= num.pos[1] + 1; row++) {
if (row < 0) row++;
if (row >= rows) break;
for (int col = num.pos[0] - 1; col < num.pos[0] + num.len + 1; col++) {
if (col < 0) col++;
if (col >= columns) break;
char c = schematic[col][row];
if (!isdigit(c) && c != '.'){
previous = num.sum;
sum += num.sum;
goto nextIteration;
}
}
}
nextIteration:
}
return sum;
}
// för varje position i 2D-arrayen, om siffra, leta efter dess "start" genom att gå åt vänster
// lagra en "pekare" till starten, längden (antalet siffror) och totalvärdet
// ^ gör en lista av dessa
// hoppa till slutet av den man hittade och iterera vidare genom sökfältet
numberList *findNumbers(char **schematic, numberList *list)
{
for (int row = 0; row < rows; row++) {
for (int col = columns - 1; col >= 0; col--) {
if (schematic[col][row] == '*') {
int sum = checkGear(schematic, col, row);
printf("%d + %d = ", list->gearSum, sum);
list->gearSum += sum;
printf("%d\n", list->gearSum);
}
if (schematic[col][row] >= '0' && schematic[col][row] <= '9') {
int len = 0;
int sum = readNumber(schematic, &len, &col, row);
list = addNumber(list, sum, len, col, row);
}
}
}
return list;
}
numberList *addNumber(numberList *list, int sum, int len, int col, int row)
{
if (list->length >= list->listSize) {
list->listSize = list->listSize * 2;
printf("Resizing!\n", list->listSize, list->length);
printf("size: %d length: %d\n", list->listSize, list->length);
list = realloc(list, list->listSize * sizeof(number) + sizeof(int) * 2 + sizeof(long unsigned int));
}
list->numbers[list->length].pos[0] = col + 1;
list->numbers[list->length].pos[1] = row;
list->numbers[list->length].sum = sum;
list->numbers[list->length].len = len;
list->length++;
return list;
}

269
day3/erik.out Normal file
View file

@ -0,0 +1,269 @@
Values: 101 and 893
Values: 102 and 403
Values: 103 and 343
Values: 108 and 845
Values: 110 and 773
Values: 114 and 362
Values: 114 and 480
Values: 116 and 326
Values: 116 and 988
Values: 117 and 981
Values: 126 and 390
Values: 135 and 531
Values: 145 and 313
Values: 14 and 289
Values: 152 and 280
Values: 152 and 382
Values: 154 and 203
Values: 163 and 837
Values: 164 and 650
Values: 165 and 152
Values: 165 and 717
Values: 165 and 93
Values: 167 and 364
Values: 174 and 54
Values: 175 and 666
Values: 177 and 37
Values: 17 and 735
Values: 189 and 142
Values: 202 and 184
Values: 203 and 445
Values: 203 and 604
Values: 205 and 674
Values: 206 and 193
Values: 212 and 290
Values: 213 and 760
Values: 218 and 130
Values: 218 and 639
Values: 224 and 235
Values: 225 and 919
Values: 226 and 717
Values: 229 and 902
Values: 22 and 380
Values: 230 and 793
Values: 238 and 302
Values: 240 and 969
Values: 241 and 822
Values: 241 and 881
Values: 241 and 968
Values: 253 and 984
Values: 254 and 837
Values: 257 and 875
Values: 263 and 325
Values: 263 and 746
Values: 264 and 438
Values: 266 and 387
Values: 266 and 749
Values: 267 and 7
Values: 271 and 486
Values: 273 and 806
Values: 281 and 396
Values: 289 and 447
Values: 306 and 434
Values: 310 and 752
Values: 320 and 670
Values: 322 and 769
Values: 329 and 664
Values: 332 and 491
Values: 334 and 978
Values: 335 and 318
Values: 336 and 814
Values: 336 and 883
Values: 339 and 238
Values: 343 and 283
Values: 347 and 222
Values: 347 and 817
Values: 348 and 252
Values: 349 and 924
Values: 355 and 485
Values: 355 and 838
Values: 355 and 981
Values: 356 and 891
Values: 359 and 793
Values: 359 and 807
Values: 359 and 936
Values: 365 and 553
Values: 370 and 961
Values: 375 and 838
Values: 378 and 251
Values: 378 and 91
Values: 381 and 53
Values: 381 and 922
Values: 385 and 294
Values: 387 and 875
Values: 389 and 963
Values: 394 and 427
Values: 398 and 332
Values: 3 and 162
Values: 414 and 583
Values: 419 and 261
Values: 420 and 833
Values: 424 and 227
Values: 424 and 296
Values: 439 and 299
Values: 441 and 367
Values: 44 and 224
Values: 450 and 576
Values: 454 and 245
Values: 458 and 592
Values: 460 and 105
Values: 464 and 13
Values: 473 and 709
Values: 476 and 350
Values: 488 and 409
Values: 489 and 21
Values: 498 and 268
Values: 49 and 212
Values: 509 and 310
Values: 510 and 823
Values: 512 and 723
Values: 522 and 940
Values: 524 and 978
Values: 526 and 785
Values: 527 and 73
Values: 529 and 554
Values: 536 and 501
Values: 544 and 599
Values: 544 and 619
Values: 548 and 699
Values: 566 and 266
Values: 568 and 719
Values: 569 and 498
Values: 571 and 394
Values: 580 and 308
Values: 586 and 811
Values: 588 and 243
Values: 58 and 85
Values: 591 and 169
Values: 595 and 958
Values: 5 and 434
Values: 5 and 714
Values: 600 and 826
Values: 601 and 424
Values: 609 and 477
Values: 612 and 680
Values: 617 and 179
Values: 617 and 519
Values: 622 and 769
Values: 631 and 92
Values: 637 and 324
Values: 640 and 859
Values: 643 and 698
Values: 64 and 38
Values: 64 and 971
Values: 652 and 398
Values: 653 and 734
Values: 654 and 991
Values: 661 and 598
Values: 661 and 771
Values: 662 and 587
Values: 663 and 369
Values: 667 and 742
Values: 676 and 218
Values: 678 and 233
Values: 679 and 532
Values: 679 and 670
Values: 67 and 914
Values: 688 and 586
Values: 691 and 689
Values: 694 and 101
Values: 695 and 766
Values: 698 and 616
Values: 69 and 437
Values: 700 and 831
Values: 701 and 918
Values: 702 and 242
Values: 703 and 256
Values: 704 and 195
Values: 704 and 29
Values: 704 and 94
Values: 705 and 699
Values: 710 and 112
Values: 712 and 250
Values: 717 and 911
Values: 721 and 336
Values: 723 and 507
Values: 725 and 830
Values: 727 and 179
Values: 734 and 666
Values: 735 and 361
Values: 739 and 701
Values: 741 and 637
Values: 746 and 984
Values: 749 and 491
Values: 74 and 312
Values: 74 and 374
Values: 750 and 557
Values: 750 and 638
Values: 755 and 662
Values: 75 and 313
Values: 777 and 693
Values: 782 and 420
Values: 784 and 828
Values: 784 and 940
Values: 785 and 798
Values: 788 and 964
Values: 790 and 120
Values: 793 and 790
Values: 797 and 211
Values: 806 and 675
Values: 810 and 889
Values: 813 and 998
Values: 818 and 136
Values: 81 and 56
Values: 826 and 963
Values: 82 and 23
Values: 833 and 589
Values: 834 and 346
Values: 842 and 118
Values: 844 and 906
Values: 846 and 144
Values: 848 and 904
Values: 849 and 226
Values: 851 and 949
Values: 85 and 258
Values: 860 and 219
Values: 866 and 859
Values: 86 and 308
Values: 87 and 586
Values: 889 and 66
Values: 88 and 235
Values: 89 and 265
Values: 8 and 581
Values: 900 and 152
Values: 900 and 649
Values: 908 and 496
Values: 90 and 496
Values: 910 and 620
Values: 914 and 192
Values: 91 and 794
Values: 922 and 751
Values: 928 and 730
Values: 931 and 60
Values: 932 and 344
Values: 935 and 486
Values: 938 and 59
Values: 939 and 180
Values: 943 and 445
Values: 944 and 333
Values: 947 and 740
Values: 948 and 675
Values: 949 and 11
Values: 950 and 524
Values: 950 and 912
Values: 953 and 808
Values: 954 and 665
Values: 956 and 998
Values: 957 and 474
Values: 961 and 208
Values: 962 and 308
Values: 965 and 914
Values: 96 and 421
Values: 971 and 225
Values: 972 and 408
Values: 974 and 767
Values: 975 and 360
Values: 985 and 514
Values: 98 and 55
Values: 990 and 739
Values: 9 and 402

1097
day3/erik.out2 Normal file

File diff suppressed because it is too large Load diff

1092
day3/erik.out3 Normal file

File diff suppressed because it is too large Load diff

140
day3/input Normal file
View file

@ -0,0 +1,140 @@
..224.....487...................718.....................378............................................284........310......313..........311.
....*..............................*744....486*485......*......741......@...359.#666...439................*925....*......$..+........@......
.235................758..440...........................251....*......262.....*..........*......................752......774.......515.......
.........705%..@746........+..942*591.347.470...#..257.........637...........793.......299..../.....813....509......464......&.........688..
.....82................................*.../..901.....*..................836.....&............814...*........*..............80...17*....*...
.../...*...679.661.....299...........222.............875.....213...161............964...894.........998.....310....258.85...........735.586.
.650..23..#......*.......................................760*........@./........................202...................*.....339.............
..............598......#.....536....702*.........705..........793......957............./........*...935...........965.......................
..................*.357...+.....@.......242.......*......283..*...=956.......118.......959...184...*..............*....401*527.....348...161
.63=...955.523..77......978.846....849..........699..........790........=911.*.....87...............486.........914..............=..........
.........*.........842......*.......*..................*127....................844...*....946.......................235...209@...710.735....
...........@.........*.....144....226....298........560.....253.........431......*...586.......723..../...482.......&...................*...
...........871../918........................+....................329*......%.....906.............*...350.....@...............365.......361..
......................536............................+..793..........664.....739................507......289.....256%...73..*...............
.356..834..............*.........96.939..............28.+....=..............*...........................*................$.553..........526.
...*...*.............501..746....*..-.......=.................890.....+...701..............807........447......97$..%...........58......$...
.891....346...699...........*.421......+....666......198*791......340.433............#...%...*.......................316........*.....%.....
..............%........81.984...........462....................../..........116...711....963...$................380.......745...85....224...
.........370......923..@.....................563...........188+.........326*...................67.815..............*582.....................
.........*........-........#530.........489........59...................................................#.....57.........100.*.......441*161
.637*....961...........118...........21*...........@..216+.........................+....432*.*309..64....992.......81......=.781.644........
.....324.........126......*166...............&...................385*..............233............*................*..436...........*517....
.88*.........390*................355..335...372....+254..............294...$589........&..803/.....38..-...........56..$....................
....235.-.........+196...948....*........-................359.........................398.............608..../.................145..346.....
........915.................-.485.945.................936*................114.............118...............490.536..............*.%........
.663........61.......856..........+....343....859...............*345.......*...=862............711.............................313.....606..
........23...$.....@..................*......................316.....529.362.............49......................307*791....................
225...............281...#..222.....@..283..............675............*.........911*90..-.....914..%39....509...............................
.......797*....$........23..../...895.............67.......164.896.....554.....................*............=..............460........181...
...........211.474...............................@...........*....*201..........911.717.#....192.75..679...........$.........*.........=....
....355.........................&.....................678.650......................*.....128.....*.....*.........925..230...105.............
....*.......409#.......*897.....698....427.........+....*.......*...............................313...670..521.......*....&.............*308
...838..335.................367........-..........325....233...224...950......688.....199.774.........................793.108........501....
...........*..102..924.349...+..595....................................*................&..............395+.....................753.........
.........318...*......*........*....676...759.154...................524...........908.........694.211................173...........*214..424
....458........403...........958.98....*........*..............688......957...488..+...........*.../....................................*...
.......*.............591...............218.....203.....196$.......+....*........*......................765......971......./....927*741..227.
......592..%60.$.....*.....885/....287...............................474.........409......226.174.......@........*.....230..................
...............162.169.................510..962..............17......................32@..*...*.................225....................-....
.....880...................201.....823*........*...914.724.........89...&.......267=.....717.54.436..........60...............&.........69..
710........601....306.287.....+.........65...308....&...=.../......*.....926..................................*........278.415......-.......
..............*..*......&..........241.%...................827../.265.............662.678......@...#......@.....$......-............734.144.
.............424..434.....701.....*........380*731.............24.................*...&........973..741....728..357.........................
........304.................*...881................238.....740.........947........587...22...........................271*........603....@...
...................266.....918....................*..........+.519.617..*...826..........*............./268..974.........486.376....*.295...
...939.........363.*...............378..........302....586........*....740...=..........380........931...........................12.........
...*..........-.....387...............*..588..............*..../...............315...................-...712..491.........956.....#.........
.180.454...................104..661...91...*.............811....370......471...-....419....804..........*...................*...............
.....*.......@../464........%..*............243.....578...................&..........*...........604..250.....784....750....998.............
.....245....165.................771...................&.......#.691...768....654....261.........................*.......*............566....
441..................91.....194.......643...932*............379.#....$.......*.................86.............940..263.638...........*......
...+.............%.../.......#.........*........344...795..................991..........544....*..................*..........767...266......
.......-..708..727.....191$......*.....698....=...................990................+.....*....308.......@......325.........*.........44...
...18.397....=.....93.........649.900.........62....-........322.....*.......@......339....619..........404..........540........./....*.....
..../...............%............................742............*...........196................874.205........96#..............203.224......
.........704...727.................496.......*........135....769........./.................%....&....*.../..=.........757...................
........*........*..424..........&........714.5........*...........@....884...............114.......674.144..83..........*302...........%...
........29....179..*............104..................531....629.455..................473......300....................254..............640...
..................296.......................................................1........*.......%..............548........*.....103............
....381...........................365#..713.282.......%744....................../....709...#...=...431...$.*......*.....837.....*9.....*....
......*.90.....347.179#...................%.......359..........92............304...*.....923.446.......385.699.....749..............193.206.
....922.+.......*.......-197.....................*.......128........273....@.....758..............439...................650*54.736#.........
................817..-..........152..594..949..807.......$...224...*.....124............889.......=...................................625...
....139....667......239........*...........*.......387........%..806........../...........*..788....632...........=8......584-......*.%.....
...........*.................280...........11.........*726............181......761..-....66.....%....-................64...........14.......
............742.736.725..........708.667..........332.......828*750......-.........425.......................944.....*...................926
.719...110...........*.....348...=..........121......*........................................*.......961.....*.......971...........+.......
..../.....*629........830..*............91....&..241..491...930............561................889....*....710.333..............163...311....
......%...................252.../..................*...........*270.......................165.....208.......*..................../.......694
...914...126...267.183........952..$645......414.822................................69.......-..........=..112.464..267....359..........*...
.............7*..........................806.../.....931.....*.....355..............*....928...........854.........-..............*993...101
439.897...........%...........8......381...*........*....748..987....*............437.......*....954.......................*................
.......*595....783........49.....110.$...675...8...60.....=........981........&..........730....*.............336.......120.790..........386
............90..............*212...*..........*.......985.....153........*..12...912*383......665.....754.....-.....................950.....
......790#.*........896@.........773....777..581..514*....910.....5.....579.............................=.......#.....................*.....
...&.......496...........347..............*...............*...................../............................790..............334......912..
692............609....&.......694..........693...213....620......695......871..385.......82..........785.......................*..163.......
.........#....*......984.........*.................=............*.......................................*.545...-............978.....*......
......971...477.............465......196*313..........447.......766......49.......126*480.............798......792..359*193...........837...
................157...........+..................773.=..............752$....................447*565..........................*53............
.384%..........@.........449............470..749...@.........@792..............138..527.......................339.....299.505...........287.
......691........700........@......96..........*........721*.....................@....*...942......845........*......-.........713..........
.....*......260/...*...........................491..452.....336.......................73....+..960..........238...........163$..&...=.@.....
....689.............831...282....709.960.............=.............204............................#..............59...97..........199..111..
........................$........../........-....498......................557....&...........972............450.............................
.............653........476...398.........619...*.....*280..923..............*31..691.........*........=...*.....67......782................
..............*....%..........*.........+......268...........&........428....................408....740....576....*.......*.........$.......
....-../...734......317.114.332...795...743.........................-..=..908...........381.......................914...420.*647....528.....
..284.825...............*......................721/.=929.........324.....*....167.561...*.....815*466.......................................
..............389..304.480.......851..@....585..............942%......#.496....*....-.53................170....938*..........518............
........368..*.....@..............*....941..+.......*...............930.........364.......522......................59.........+....$....652.
...........+.963...................949...........828.784......860............*......420.....*..................*......548...........310.*...
.....476...............*...............=...94.+..................&........589.833....*..426.940.....&.....959.993.&88....................398
.......*................624....917...838..*...664.......721........................833...$.......672......$............19...................
....350.......................&....=..............333.................707....................................240..336....=...746.263..$463..
.........................-.........220.....663...../.............*849..#..631.....712....=.../...308.580.............*..........*...........
.78@......../..........422....444.............*...............281............*...*....447..37.......*................814....................
........./...788...810.........&..............369..985....-................92................................684...............74...........
......279.........*......128/....464........#...../......944........978...............163..984*5.@............+.......165......*....285.....
..................889.............*.......969........................%.......5......&....$........272....................*......374.........
.........&..............@948..*..13.358........796.............632............*....789........91*..............*66........152...............
........508...+..............3...........544.....$.517.....703....*774.......434............/....794........932.............................
...$...........241.....74...........974..*...954............*.......................375..772...........218.........414...........*366....252
.501..566...............*..........*....599.....#........256.....468@..942.101.........*..........156...*.............*.......998...........
...../.............263.312....+..767..............#.........................*...&95..838.162=.....+....130.........583..544.......176.......
............................936......826.......421.......-139.......982...893.......................*..................*........./..........
.284.....=724...................661.$....694........85..........438.............51...............616.698.*36.....262......357+........224...
....@..........491........+........................@......193......*596...........%../844...387.................$.....132.........%.........
.......84..............362.................................*..98..........&................*....14....394..........$..............56........
.........*432.....512................704......424.............*............74........196%..875...*.....*............132.......229...........
..............723*.........568........*..108........672......55.172...217.........................289.427........................*..........
.........#795.........214....*.336...94....*....988................%..&.............*86......*...............838&.......540.......902.......
.922...........288........719.....*........845...&..558......#...................524......195.704.....................................77....
...*....842....../...166........883...-................*503..451..305../....600............................900..280.....*169...586..........
751....*.........../.*......944......116....416.........................877....*....418...194........644..*....=.....244..........#.....354.
.......118......208...................................788...................826........$....$...207.....@..152...........893*608.......&....
..948.......................203../....206.............*....341./977.916.........49...#........@....-............+385........................
...*......266.................*...607...@.990......964..............-..........*.....340...697........62..............526.....-.............
..675.....*..........612....604.............*..............492.884...........212...............*157....*...679.........*....706...716%.518..
......103.749..989...*...................739..............$......=..=..................212..684.....35.......*.........785..............+...
..343*......../.......680.....................*423.................476....&....420..............241..*......532.....+.......864$.....&......
..................826........403..311...60.451.............+...........562....../.......306.......*..............420..................321...
...241..............*...........+..=....@......*240.......845....@.........740.................968..389.177*...................640..........
......*82...........963...211%...............38...............901.....404.........&..253..............-.....37.........218.501...*....320...
....*...........................#215...264......298........................*970..801....*....569...............3.......*......%..859.*......
.136.818...212...........91*72........*.........-...750.....775*728.29.270...............984.&...371..........*.........639..........670....
...........*.../959................438.....524......*...943.........*................755...................162.................446#.........
..........290.............751................*.....557..*...483.............421.........*............189.%................/.................
....................9.786..-......835.......978........445.%..........350........860..662.......#...*....354........*.....682...............
.117..57+....441...*....+............................................../............*.........88....142..........%.927......................
...*...........*..402....................874.....734.926$../...............*633......219.84/............662...205...................334.....
...981........367........338...332/.........=......*......119....375#.334.......146..............%.....$................384.............-511
........165................=....................666.....................#..614.....-..%744..617.887.......848*..............................
...........*.203.593...158....*..........152..............+817.....866.......&...............*......../.......904........701.165.80.........
404..977.93..*....*....*...396.281......*....953....*................*.........116..97.70=..179....107..338................$...*....622.....
......=.....445........................382.....*...672................859......*...%....................&..........-.571......717....*......
..................240............175..........808..............225..........988............/604..............232.448..*..651......769.......
.........569*....*........975*.....*....968..............585.....*................26.................................394....@.142...........
......*......498..969.........360.666...%.........................919.......360........-.*.........%...................................484..
...407................886...................................84......................933...101....58........839..425.........................

10
day3/input.bakeasy Normal file
View file

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

1092
day3/mathias.out Normal file

File diff suppressed because it is too large Load diff

1092
day3/mathias.out2 Normal file

File diff suppressed because it is too large Load diff

1387
day3/output.txt Normal file

File diff suppressed because it is too large Load diff

5
day3/symbolList Normal file
View file

@ -0,0 +1,5 @@
(inclusive list)
33:47
58:63
91:96
123:126

BIN
day3/test Executable file

Binary file not shown.

106
day3/test.c Normal file
View file

@ -0,0 +1,106 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#define MAP_SIZE 140
typedef struct{
int x, y;
int width;
int value;
}NumberBox;
char map[MAP_SIZE*MAP_SIZE] = {0};
bool touching_symbol[MAP_SIZE*MAP_SIZE] = {false};
NumberBox number_boxes[10000] = {0};
bool is_touching_symbol( int x, int y) {
for( int test_y=y-1; test_y<=y+1; test_y ++ ) {
for( int test_x=x-1; test_x<=x+1; test_x ++ ) {
if( test_y < 0 || test_x < 0 || test_y >= MAP_SIZE || test_x >= MAP_SIZE) continue;
char c = map[ test_y*MAP_SIZE+test_x];
if( !isdigit(c) && c != '.') {
return true;
}
}
}
return false;
}
int main()
{
FILE *fp = fopen( "./input", "r");
char line[256];
int y = 0;
while ( fgets( line, 256, fp ) != NULL) {
memcpy( &map[MAP_SIZE*y++], line, MAP_SIZE );
}
int all_sum = 0;
int schematic_sum = 0;
int num_number_boxes = 0;
for( int y=0; y<MAP_SIZE; y++ ) {
char* read_pos = &map[y*MAP_SIZE];
char* next_line = &map[(y+1)*MAP_SIZE];
for( ;; ) {
char* num_start = strpbrk( read_pos, "0123456789");
if( num_start >= next_line || num_start == NULL ) break;
char* num_end = num_start + strspn( num_start, "0123456789");
if( num_end > next_line) num_end = next_line;
// Extract the number
char number_string[ 64] = {0};
strncpy( number_string, num_start, num_end-num_start);
int value = atoi( number_string );
int x = num_start-&map[y*MAP_SIZE];
int width = num_end - num_start;
// Store the number box for the next part of the problem
NumberBox nb = {x,y, width, value};
number_boxes[ num_number_boxes++ ] = nb;
// Are any of the digits touching a symbol
bool is_touching = false;
for( int x_pos=x; x_pos<x+width; x_pos++ ) {
is_touching |= is_touching_symbol( x_pos, y);
}
if( is_touching ) schematic_sum += value;
if( is_touching ) {
// printf("%d, col: %d row: %d\n", value, x, y);
}
all_sum += value;
read_pos = num_end;
}
}
printf( "\nSum of schematic numbers: %d", schematic_sum);
// Part 2
int sum_of_gear_ratios = 0;
for( int y=0; y<MAP_SIZE; y++ ) {
for( int x=0; x<MAP_SIZE; x++ ) {
if( map[y*MAP_SIZE+x] == '*') {
// Found gear, find any touching numbers
int num_touching = 0;
int ratio = 1;
for( int n_box=0; n_box<num_number_boxes; n_box++ ) {
if( number_boxes[ n_box].x -1 <= x && number_boxes[ n_box].y -1 <=y &&
(number_boxes[ n_box].x + number_boxes[ n_box].width)>= x && number_boxes[ n_box].y >=y ) {
ratio *= number_boxes[n_box].value;
printf("%d *", number_boxes[n_box].value, ratio);
num_touching ++;
}
}
if( num_touching >= 2) {
printf(" + %d", sum_of_gear_ratios);
sum_of_gear_ratios += ratio;
printf(" = %d\n", sum_of_gear_ratios);
}
}
}
}
printf( "\nSum of gear ratios: %d\n", sum_of_gear_ratios);
}

62
day3/test.out Normal file
View file

@ -0,0 +1,62 @@
Sum of schematic numbers: 539590235 *744 *486 *485 * + 0 = 235710
251 *925 *752 *637 *793 *299 *942 *591 * + 235710 = 792432
222 *875 *998 *310 *17 *735 * + 792432 = 804927
586 *23 *598 *760 *702 *242 * + 804927 = 974811
184 *77 *699 *790 *486 *914 *401 *527 * + 974811 = 1186138
144 *226 *127 *560 * + 1186138 = 1257258
586 *906 *361 *329 *664 * + 1257258 = 1475714
507 *553 *501 *701 *447 *891 *346 *421 *984 *198 *791 * + 1475714 = 1632332
85 *326 *582 *961 *781 *441 *161 * + 1632332 = 1703333
637 *324 * + 1703333 = 1909721
21 *432 *309 *166 *385 *294 * + 1909721 = 2022911
38 *56 *517 *88 *235 * + 2022911 = 2043591
390 *485 *936 *313 *345 *316 * + 2043591 = 2152611
362 *283 *307 *791 * + 2152611 = 2395448
554 *911 *90 * + 2395448 = 2477438
797 *211 * + 2477438 = 2645605
192 *650 *201 *105 *313 *670 *838 *897 *233 *224 *793 *308 *501 * + 2645605 = 2799913
318 *524 *403 *958 *214 *218 *203 *227 *592 *474 *409 *927 *741 * + 2799913 = 3486820
169 *225 *717 *54 *823 *308 *265 *424 *434 *881 *380 *731 * + 3486820 = 3764600
587 *918 *271 *486 * + 3764600 = 3896306
302 *740 *380 *387 *180 *91 *811 *243 *250 *998 *245 *771 *261 *940 *638 *932 *344 * + 3896306 = 4216914
991 *266 *698 *308 *325 *649 *900 * + 4216914 = 4801014
619 *224 *769 *714 *5 * + 4801014 = 4804584
674 *29 *179 *531 *302 *296 *709 *837 *699 *749 *9 *193 *206 * + 4804584 = 4844342
922 *758 *817 *807 *650 *54 * + 4844342 = 4879442
806 *280 *11 *66 *14 *742 *726 *828 *750 * + 4879442 = 5500442
971 *830 *491 *889 *333 *629 *252 *208 *822 *270 *112 *101 *7 *987 *437 *993 *675 *60 *981 *730 *120 *790 * + 5500442 = 5595242
595 *665 *212 *773 *581 *579 *912 *383 * + 5595242 = 5944538
496 *514 *912 *693 *620 *978 *477 *766 *798 *837 *196 *313 * + 5944538 = 6005886
126 *480 * + 6005886 = 6066366
359 *193 * + 6066366 = 6135653
447 *565 * + 6135653 = 6388208
53 *505 * + 6388208 = 6414973
491 *721 *336 * + 6414973 = 6657229
73 *238 *689 *831 *268 *280 *31 *408 *576 *734 *332 *914 *420 *647 *480 *496 *53 *815 *466 * + 6657229 = 7037019
364 *938 *59 * + 7037019 = 7092361
963 *949 *828 *784 * + 7092361 = 7741513
589 *833 * + 7741513 = 8232150
940 *993 *398 *624 *833 *350 *849 *281 * + 8232150 = 8470719
814 *369 *92 *889 *984 *5 * + 8470719 = 8475639
374 *13 *152 *3 *434 *91 *794 * + 8475639 = 8547893
66 *932 * + 8547893 = 8609405
774 *599 *256 *366 *998 * + 8609405 = 8974673
312 *767 *838 *130 *583 *893 *616 *698 * + 8974673 = 9404641
36 *596 *875 *432 *55 *289 *427 *723 *94 *902 *719 *845 *86 *524 * + 9404641 = 9449705
195 *704 * + 9449705 = 9586985
883 *751 *503 *169 *244 * + 9586985 = 9628221
118 *826 *152 *893 *608 * + 9628221 = 10171165
964 *675 *604 *212 *749 *739 *157 *684 * + 10171165 = 10278553
785 *680 *532 *343 *423 *451 * + 10278553 = 10469326
968 *963 *240 *38 * + 10469326 = 10478446
177 *37 * + 10478446 = 10484995
82 *859 *136 *818 * + 10484995 = 10596243
970 *984 *639 *670 *91 *72 * + 10596243 = 10602795
438 *775 *728 * + 10602795 = 11166995
162 *290 *557 *978 *445 *662 *142 *927 *402 *219 *981 *367 *633 *666 *848 *904 * + 11166995 = 11933587
93 *396 *281 * + 11933587 = 12044863
179 *445 *382 *672 *859 *717 *808 *988 *769 *394 *569 *498 * + 12044863 = 12328225
969 *975 *360 * + 12328225 = 12679225
666 *919 *407 *101 *
Sum of gear ratios: 12679225

1092
day3/test.out2 Normal file

File diff suppressed because it is too large Load diff