From f17cc04b497e6fade6a97234a708a5153f5840d2 Mon Sep 17 00:00:00 2001 From: Benjamyn Love Date: Wed, 6 Dec 2023 20:11:53 +1100 Subject: [PATCH] Nearly finished day 3p1 --- .gitignore | 4 +- .vscode/c_cpp_properties.json | 16 ++++ .vscode/launch.json | 7 -- .vscode/settings.json | 3 +- .vscode/tasks.json | 4 +- day3/input | 140 ++++++++++++++++++++++++++++++++++ day3/libs | 1 + day3/make.sh | 1 + day3/puzzle1.c | 137 +++++++++++++++++++++++++++++++++ libs/fileLoader.c | 50 ++++++------ libs/fileLoader.h | 13 +++- libs/strTools.c | 14 ++++ libs/strTools.h | 4 + 13 files changed, 355 insertions(+), 39 deletions(-) create mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/launch.json create mode 100644 day3/input create mode 120000 day3/libs create mode 100644 day3/make.sh create mode 100644 day3/puzzle1.c create mode 100644 libs/strTools.c create mode 100644 libs/strTools.h diff --git a/.gitignore b/.gitignore index 88d050b..1700366 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -main \ No newline at end of file +main +puzzle* +!puzzle*.c \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..07c08df --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 5c7247b..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 9deab0a..bb4c38d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "stdlib.h": "c" + "stdlib.h": "c", + "stdio.h": "c" } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 08d9005..76f1d11 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -9,7 +9,9 @@ "-g", "${file}", "-o", - "${fileDirname}/${fileBasenameNoExtension}" + "${fileDirname}/${fileBasenameNoExtension}", + "libs/fileLoader.c", + "libs/strTools.c" ], "options": { "cwd": "${fileDirname}" diff --git a/day3/input b/day3/input new file mode 100644 index 0000000..8faa3ba --- /dev/null +++ b/day3/input @@ -0,0 +1,140 @@ +.......12.......935............184.720...243........589.652..........435..........483.............6...........................904........... +......*.....968*.....$............*........=..348...*..........986....*...................459....*........422................#......%482.... +....291............612....290..........903........699......218*.......376............890....*.838...81......*.....138.../194................ +..............156......$..*...891.&731....%..89...................523..........699....+...227......*.......225....=...........388....*...... +................*...189..591.*................*.......783.....107..-...54.287..$................533.../..............909........&.603.424... +...229*952.....938............470.555.......746...28.....+...*.........................................279..826..788*....................... +...................................*...............@.........867.-....102..845...542.779.....................*........182.166...511......... +563.727.....282....237..171.......892...183.......................989....*..........*................$....709...8*974.=...%.....*.....873... +....#..........+...*.......*..........................&......129+......491...................877....715.......................270.......#... +.................53.....781...&295....@773.336......547................................45......*.............=800.....*359.................. +...342*468....&.....573....................*.................................%850............*..465................192.....*.......&115..... +............988.......*...731.............789.673....*256..............677..................621...........#....716......910..297........22.. +204&....69............905...=.....641.472.......#.524............127....*..714.........*62..........622..324..&.................*........... +..........*........................+....@.................812......*..955.....*.....257....../.................../..............545......... +........668....91..&.........840......+..............566.....+...66.........660..............469.................525...765...........25..... +...............$...666.979..*........952...122......%............................71......=39...../94.........................132......*..... +......@.................../.460..#.......=.=..........146..625........214.........#.278................667..........386.......-....936...... +....643.......@..................974...464...........*.....*...........*.............%.....$...........=...=..%995...*...................... +........./...577...-....3.../186.................*.995.....678.&.......882...559...........374.......&...554.........428.......725....*..... +.......96........678....%.....................529..............374...........*......924........=277..606..................#...#......8.257.. +...609....................397.......475...................651.......856......697.&.....&..=...................486........888................ +............-................*..................756...399....*..42................866....287.....#......515...*......................137.... +.....285..947....#..........997......=.....665..*......*..420...*.....-..268*457.............=63..758.....+..623.............+../........... +..............458.....422.......338...827...+..462....797.....26....523..........*.880*.............................&.....934..157.......... +.......149........409....$................................................868.378......144..149............356.....872..............871.533. +....22...*..........$........7.......149....979......................826.&.....................@........-.....*275.....................*.... +........518.773*.........10..$..........*........805..812.972......................123......*............98.........=........./609.682...... +.................5........./............349........$.....*..................786....*.......160..903...40.............209...............*.... +.......*.....................................497...........#.............../.....153.795............./.........&806..........839....419.337. +78...582............675................145.........405....77..361............952.......*..../741....................624..527*............... +...................../../....=.....+....&.............*51.....*..........733*.........728.........173....834.....76......................... +................878....276..792.....372....................385..179.............@610.............................%........146=.............. +..........522.....*...........................-....30....-........*.@................110=..+.......197.....643.................762......%... +......%....*...173.............420..407......261.........687.806.12..104.....914..........685.233.&.......*.....549...=...........*......591 +.....837..135.......313...579....%.=.....613................../..........729.........257#.....&.........430.399*.....157.........970........ +...................*.....*.....*......@.....*............../....566........+.....................*906....................%.................. +....459.78$.775.768..62...345.537......122.803.142*758...148...*....992................711....316.........#...............521.298*590..289.. +....*.................#.........................................444............382.....*...@...............753.......927................/... +.....592..735......+.............634.......652.....853....690................./.......874.846.35.....884.........182.-...................... +.419........*.839.157.......795...................*.......*...........928........................756....*64......*.....*19.....309.......... +.........716...*.......591....*....358$.#378.............623...............687.........-816......*............292...110....&....*......643.. +..134...........910......*.941.......................................547=....*....................490....938+..............422.482.......... +...*..331....$..........80.................#346....180...........737.........76.......992@...91......................../.................... +...16....$.140.464*831.....933.917....303............*......%.....*....336........339.......*.............@..939....583....*564.........864. +..............................*........=...+54.....449......209.912.....+.....615..%.....605............960..*..........219................. +.........@442.....730..................................413...........................26.....................944..336........188......./..... +...190............#...@...524.544....968*.............*........466...222...536...843.@..........=.....32...........$..........-.......410... +......*..80@..491....232..-......*.......16........402......27..*...*.....*...................480....*........&........894.................. +.....218.........*.............=..819......................*........857...896...........677........689........638...45*.....993............. +................754...........434.........622.338...........607....................&......&.%213...........................-................ +........+.894......................681.......*.........484.........963.498...*..538.............................................346......... +.....802.....*162....752...........................................*........520...........229.2.586..513..............25=.746........782*423 +...............................@...307#.497....551..996..696....879...911................%......................277*........................ +....*157.....137....#.........621.........*...#.....*...#............*............527......9........./..............844.....@224....304..... +.541............*.649.............412.....744......560..............404.....240.......986*.@.......346.....@..........................+..... +......268....764.................%.......................902....856.......*....*..........................242.....842.....#................. +........&...............514..38..........+..=...........@..............715.142..587.......307.................731...*...954....878*......... +............................*.....349....76.92....612.....376.......................257...$...................../.968..............765...... +........960*414........713.501......&............%.......*......*941............................#........415...........937%................. +....967............409..*......605......................375..771........599*884.................880.........*...127..$.................$513. +.....*................*..171..*.....@.......507.895..................*.................636...........768...114....-..292.......-...223...... +...807...977.......218.......753...47.......*......*..............507.90....../725.......*.........+...@.....................755......*..... +............*478.............................542....676...............................673........974.....@....*170....261..............134.. +800...............532............................15............%...445..102.............../510.........625.279..........-.....*............. +.......................654.....850.405......................395.....-.......467....477...........973............../........191........398... +.925...793...423*.........@..........................................................=.659..........*.....817.....230...........653..-...... +.......*.........52...........764$....273...................@........430....................................*...........669.....-.......71.. +.435....804..177....990.838.........#..-...475...297..337..360.........*....914.............................833............................. +....*.......*.........*...*.......281......&......*..+.............328..746......=215.......359....295.....................#481......868.... +..967........936.689%.785..515...............*64.827........................761.........+....&.......+..............................*....637 +....../.+265..............................562...............235........585..*...........793........=...838.147....284.902..301*330.......... +....579.........................21%..327................481..*...469%..*.....297.704..............797.....*...........*.................#... +......................638............*...................*....81.......780.........*...../...282....................907.882.781.......797... +..........671....608.................295.................302......953............171..919.......*........................*...*......#....... +............*....*....251.....191........#............*..........................................999.....451....+....286.415.581....274..... +..245$.....217...649.-....................898......950..................495*497.......53*............@...........398..-..................... +...........................942.......323...............+.....623.........................804.......875........................#............. +.....42..543.................*..320....*..918....314....145...........603......991$.921.......261......................762...187.....951*638 +.....*..#........53........374........756...........*.........-.......................*........../..217*550..............*.................. +....217..........*................................743......703....368...$861..4*966.415..703................................*959........*344 +...........626...31...602.................700..........696........*.....................*......396..........591.......*124.................. +..135........@................................699.139.....*.662....46........=797......144.....#...*289......*.....840............-...102.44 +................909.......180.............100.*....#...574..*............52...................................451............764.517....*... +........701*..........127*............117../...787...........67.....903..........273....*900................................=..........112.. +............430............423.67.......*..............*572...........@...199.........94..........686.....679.....................921*...... +.542*119...............396....*.......938...........250......888..........*.....978...................25..../.........306.............780... +...............251........*.....................238......................340....+..............112....*........120...*....24................ +.223.....549......-..98..60..775..28*105...........*.436..951.599.................309*682.......*..937...158.....*....697.*......764..$..... +.....788............#....................768.....266........*..%..............................779........*.......542.......148...*....529... +....*......310.............412.......23.+....289..........560............249+.204..606*................271...805.......394.......898........ +....919.......&...867.......*........*......%.........592............166.........*.....460.........369..........*........*.................. +.........920.......*.....233.....732..500.....950.....*.......514...........110..961..................*309.....450..#...521................. +.................753...............*............#...965..............................852...841....%........524.....981............653....... +...........858...........*..234..922........65..................991.........-839.800....*......869...836..$.............675..789...*....505. +..........*.............516...*..................774.....&.149.=.................*.......737.........*......402....%.......*....#..762...... +........780.........4.........116.....................976..-...........%........791..................300.....%....338.471................... +...................*.................480*160..3..............*727.....363................................................................167 +...........743.....721.......................*..........808...............942...........339.289.............$............416.....$.......... +..955...................606*623.............718...503....*...................*245..........*........531.....686......250..../....275........ +.............@....................110...............*...309..#....&......210..........386.................%...........*........+........391. +.....444.....743......519..691......*.......-824......+......759.617....*.......710..*.....814.........356.........464.......709............ +......*..746......247.....*......596..999.............907.............296.-..../.......760.+.................$.890.......................669 +...479.....*.........*.......561.....+..........*178......................977....266...*.....640..........607.....*.....%...........545..... +........395...=......361........*372.........655..........398...203..11................147..*......../.............196..499........*........ +..561........646......................*898.......175.570.....$.....#...*..135..............633..314...871......898..........891...90.....780 +.......*91........405..............627..............*....736.........760.....*........=..........*...........%....*..........*.............. +....722...........*......................................*.................744.698.367..-........767..........236.692.......308............. +...............740........./............906......*593...977.......645............*......167.............................................*... +........+...............123...................281....................*...543......461........................175.....................381.319 +.282...814......771*........920.......................@............473....*...............&....872...../401.....*......=......18............ +....*.........%......993...*...........................256..223...........242....977.......293.-...............550.....435.................. +....23.........960........307....$.....504.638*656...............&...................203.............33.$430..................731........... +................................885......*................398....763...........805.....=....338.......*......146*916..................513... +..20...............42....................828.............../...................*............$.........531............159*761......78........ +......805-...-..........598...........#.............351........34.........682..516.534..............*........&..........................283. +..............664....../.......255..887.....258......%.........*.........*............*.....226..931.91......128............................ +...557.938#.......75.....585..*...............*.194......#..686........786..........755.......&..........637..............988.......275..... +....*............%....35...*.141...........508.....*865.967..............................80*............./.....978....790*.....@...*........ +.662..................*..............@939...........................204........484....34....99.............@.................350.54......... +............358....832..=......................%......307.95........*......654...*......&.........734.....491...50...924.385................ +............*............340................483.........-.*.......118..794*.....431...........783..*..............-......*.........&594.#... +..........338........=.............$.................*....94....................................*...546.....$.......187..593.768........282. +...............796.380.183....+.564..94.144.........138..............394.392.....971.......#928.447........275..57...*.......&.............. +...........233...*.......%...40........*........................945.....*.........-.................%.............=.438...........*......906 +.....881.../...491...903..........918.....303......................*........578*...................807......597................681.539...... +......*.................*.463.467*....246.......577.....198.....245.....967.....988.&.........759.......=...*.........-889.................. +......462..978........664...%...........*.-731.../.....&....375.............#.......217........@........381.250...219......947.............. +..........&..........................252.......%.............*..............258............427...137...............@........@....-175....... +....864...................461..................234.......69.222........*...........411....=............651...#.............................. +....*......243...............*283...................777-..*..........92.761..581%.*.....................$.....258..867..876................. +.916......%.........629.................911*238...........680...239................672.............137@...469.........*.......737...@....... +..............764....*........894/..289...........944..........#...........@....%........../876..........*.........687.........*....436..... +......442.255.....442..903.............*.....*493..../......................338..346...................927..............764.....536......... +.........*.....*........*.....869*888.597.241..................11-....542#...........412*..................235.............................. +.804.329......344....195...94.......................................*....................880......44.622.........342.18...............417... +....*.....=................+.........76..328.....803.....658.....254.............327..........340......*.............@...................... +...........254........../...........*...*...........*234...%..........20.....238...*..........*.........380......&......78.246%...598....... +.96....990.........%..718...%....869.......596....*......................423.....433..93.....613....$.............905...=........*.....497.. +...*....*........=.86.....499..............*...385.296...................*...........*...............523.$.....................529...$.*.... +...780.685.....822....................560.35.............................529......780.....................453......................711..930. diff --git a/day3/libs b/day3/libs new file mode 120000 index 0000000..7cb0c1a --- /dev/null +++ b/day3/libs @@ -0,0 +1 @@ +/home/ben/Documents/Projects/C/advent-of-code/libs \ No newline at end of file diff --git a/day3/make.sh b/day3/make.sh new file mode 100644 index 0000000..63594ae --- /dev/null +++ b/day3/make.sh @@ -0,0 +1 @@ +gcc -g -o main puzzle1.c libs/*.c \ No newline at end of file diff --git a/day3/puzzle1.c b/day3/puzzle1.c new file mode 100644 index 0000000..e6e1aef --- /dev/null +++ b/day3/puzzle1.c @@ -0,0 +1,137 @@ +#include "libs/fileLoader.h" +#include "libs/strTools.h" +#include +#include +#include +#include +#include + +// https://www.digitalocean.com/community/tutorials/hash-table-in-c-plus-plus +#define CAPACITY 5000 // Size of the hashtable + +struct Ht_entry +{ + int num; +}; + +struct HashTable +{ + struct Ht_entry **entry; + int size; + int count; +}; + +unsigned long +hash_function(int x, int y, char *c, int r) +{ + unsigned long i = 0; + int str_total = 0; + for (int j = 0; j < r; j++) + { + str_total += (int)c[j]; + } + i = x * y - (int)str_total; + return i % CAPACITY; +} + +struct Ht_entry create_item(int x, int y, int num) +{ + // Creates a pointer to the a new HashTable item + struct Ht_entry *entry = (struct Ht_entry *)malloc(sizeof(struct Ht_entry)); + entry->num = malloc(sizeof(num)); + int nDigits = floor(log10(abs(num))) + 1; +} + +int simple_int_conv(char str) +{ + int num = (int)str - '0'; + if (isdigit(num) == 0) + { + return num; + } + return -69420; +} + +char *check_numbers_around(int x, int y, char chars[140][141]) +{ + char *found_str; + // printf("%c", chars[x][y]); + // char *cur_char = (char *)chars[x][y]; + // long int num = char_to_int(cur_char); + int num = simple_int_conv(chars[x][y]); + if (num == -69420) + { + } + if (isdigit(num) == 0) + { + printf("Is a number: %i\n", num); + } + if (1) + { + } + return found_str; +} + +void check_symbols(int x, int y, char chars[140][141]) +{ + char *symbols = "*$%&/@-=+#"; + for (int i = 0; i < 10; i++) + { + char *cur_char = (char *)(**chars * x) + y; + if (chars[x][y] == symbols[i] || chars[x][y] == '1') + { + + check_numbers_around(x, y, chars); + // printf("\n\nGot a symbol: %c\n\n", chars[x][y]); + exit(0); + } + } +} + +int main() +{ + char *filename = "input"; + struct OpenFile *o_file = load_file_to_mem(filename); + + // Load the dataset into memory so we have the full data mapped + // in an easy to work with way + const int line_length = 141; + const int line_count = 140; + char chars[line_count][line_length]; + int iter = 0; + for (int i = 0; i < o_file->fileSize; i++) + { + chars[iter][i % line_length] = o_file->fileData[i]; + if (o_file->fileData[i] == '\n') + { + iter++; + } + + // printf("%c", o_file->fileData[i]); + } + + // Look through the data to find all the symbols + // Once we find a symbol look in the spots around + // the symbol to find any numbers, if we find a + // number then we look for the start point and + // the end point -> convert the string to an int + // Once we have the number we store it in a hashmap + // Using the X/Y coordinates of the char at the start + // point plus the int value if the char... I hope + + for (int i = 0; i < line_count; i++) + { + for (int j = 0; j < line_length; j++) + { + // printf("%c", chars[i][j]); + check_symbols(i, j, chars); + + // if (j % line_length == 0) + // { + // printf("\n"); + // } + } + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/libs/fileLoader.c b/libs/fileLoader.c index 2583107..96b8ff0 100644 --- a/libs/fileLoader.c +++ b/libs/fileLoader.c @@ -9,43 +9,43 @@ #include #include "fileLoader.h" -struct OpenFile +struct OpenFile *load_file_to_mem(char *filename) { - char *fileData; - int fileSize; -}; - -struct OpenFile load_file_to_mem(char *filename) { - struct OpenFile file; + struct OpenFile *file = NULL; + file = (struct OpenFile *) + malloc(sizeof(struct OpenFile)); int fd = open(filename, O_RDONLY); - if (fd < 0) { - printf("\n\"%s \" could not open", filename); - exit(1); + if (fd < 0) + { + printf("\n\"%s \" could not open", filename); + exit(1); } struct stat statbuf; int err = fstat(fd, &statbuf); - if (err < 0) { - printf("\n\"%s \" could not open", filename); - exit(2); + if (err < 0) + { + printf("\n\"%s \" could not open", filename); + exit(2); } char *filecontents = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - if (filecontents == MAP_FAILED) { - printf("Mapping failed"); - exit(3); + if (filecontents == MAP_FAILED) + { + printf("Mapping failed"); + exit(3); + } + file->fileSize = statbuf.st_size; + file->fileData = malloc(statbuf.st_size); + memcpy(file->fileData, filecontents, statbuf.st_size); + + if (err != 0) + { + printf("Unmapping failed"); + exit(1); } - file.fileSize = statbuf.st_size; - file.fileData = malloc(file.fileSize); - memcpy(file.fileData, filecontents, file.fileSize); - - if (err != 0) { - printf("Unmapping failed"); - exit(1); - } close(fd); return file; } - diff --git a/libs/fileLoader.h b/libs/fileLoader.h index b11a5a7..666f5cc 100644 --- a/libs/fileLoader.h +++ b/libs/fileLoader.h @@ -1,6 +1,11 @@ -#ifndef HEADER_FILE -#define HEADER_FILE -struct OpenFile; -char *load_file_to_mem(char *filename); +#ifndef fileloader +#define fileloader +struct OpenFile +{ + char *fileData; + int fileSize; +}; + +struct OpenFile *load_file_to_mem(char *filename); int unmap_file(char *ptr, char *filename); #endif diff --git a/libs/strTools.c b/libs/strTools.c new file mode 100644 index 0000000..6c611c4 --- /dev/null +++ b/libs/strTools.c @@ -0,0 +1,14 @@ +#include "strTools.h" +#include +#include +#include +#include + +int char_to_int(char *str) +{ + int result; + char *tmp; + result = strtol(str, &tmp, 10); + printf("%i", errno); + return result; +} \ No newline at end of file diff --git a/libs/strTools.h b/libs/strTools.h new file mode 100644 index 0000000..0af80a5 --- /dev/null +++ b/libs/strTools.h @@ -0,0 +1,4 @@ +#ifndef HEADER_FILE +#define HEADER_FILE +int char_to_int(char *str); +#endif \ No newline at end of file