From 48966f12832ac97228132e56fb3159099f3e466e Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 31 Jul 2022 11:55:45 +0300 Subject: cleanup --- Advent-of-Code-2021/AOC-7/aoc-7 | Bin 20984 -> 0 bytes Advent-of-Code-2021/AOC-7/aoc-7.c | 124 -------------------------------------- Advent-of-Code-2021/AOC-7/main.c | 124 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 124 deletions(-) delete mode 100755 Advent-of-Code-2021/AOC-7/aoc-7 delete mode 100644 Advent-of-Code-2021/AOC-7/aoc-7.c create mode 100644 Advent-of-Code-2021/AOC-7/main.c (limited to 'Advent-of-Code-2021/AOC-7') diff --git a/Advent-of-Code-2021/AOC-7/aoc-7 b/Advent-of-Code-2021/AOC-7/aoc-7 deleted file mode 100755 index b19d5ab..0000000 Binary files a/Advent-of-Code-2021/AOC-7/aoc-7 and /dev/null differ diff --git a/Advent-of-Code-2021/AOC-7/aoc-7.c b/Advent-of-Code-2021/AOC-7/aoc-7.c deleted file mode 100644 index 165c671..0000000 --- a/Advent-of-Code-2021/AOC-7/aoc-7.c +++ /dev/null @@ -1,124 +0,0 @@ -#include -#include -#include - -#define CAP 3000 - -#if 0 -#define PFILE "sample.txt" -#else -#define PFILE "input.txt" -#endif - -int vals[CAP]; -size_t vals_sz = 0; - -void Parse() -{ - // Parse - char ch; - FILE *fp; - fp = fopen(PFILE, "r"); - - char int_buf[6]; - memset(int_buf, '\0', sizeof(int_buf)); - int n = 0; - while((ch = fgetc(fp)) != EOF) - { - if(ch != ',') - { - int_buf[n] = ch; - n += 1; - continue; - } - - vals[vals_sz] = atoi(int_buf); - vals_sz += 1; - memset(int_buf, '\0', sizeof(int_buf)); - n = 0; - } - -} - -void Part1() -{ - Parse(); - size_t fuel_arr[CAP]; - size_t fuel = 0; - - for(int i = 0; i < CAP; i++) - { - int target = i; - for(int j = 0; j < vals_sz; j++) - fuel += abs(target - vals[j]); - - fuel_arr[i] = fuel; - fuel = 0; - } - - // Find the smallest in the fuel array - for(size_t i = 0; i < CAP; i++) - { - size_t counter = 0; - for(int j = 0; j < CAP; j++) - { - if(fuel_arr[i] <= fuel_arr[j]) - counter += 1; - } - - if(counter == CAP) - { - printf("%d\n", fuel_arr[i]); - break; - } - } -} - -size_t CountFuel(int target) -{ - // somehow works idk - size_t sum = 0; - for(size_t i = 1; i <= target; i++) - sum += i; - return sum; -} - -void Part2() -{ - Parse(); - size_t fuel_arr[CAP]; - size_t fuel = 0; - - for(int i = 0; i < CAP; i++) - { - size_t target = i; - for(int j = 0; j < vals_sz; j++) - fuel += CountFuel(abs(target - vals[j])); - - fuel_arr[i] = fuel; - fuel = 0; - } - - // Find the smallest in the fuel array - for(size_t i = 0; i < CAP; i++) - { - size_t counter = 0; - for(size_t j = 0; j < CAP; j++) - { - if(fuel_arr[i] <= fuel_arr[j]) - counter += 1; - } - - if(counter == CAP) - { - printf("%d: %d\n", i, fuel_arr[i]); - break; - } - } -} - -int main() -{ - Part2(); - return 0; -} diff --git a/Advent-of-Code-2021/AOC-7/main.c b/Advent-of-Code-2021/AOC-7/main.c new file mode 100644 index 0000000..165c671 --- /dev/null +++ b/Advent-of-Code-2021/AOC-7/main.c @@ -0,0 +1,124 @@ +#include +#include +#include + +#define CAP 3000 + +#if 0 +#define PFILE "sample.txt" +#else +#define PFILE "input.txt" +#endif + +int vals[CAP]; +size_t vals_sz = 0; + +void Parse() +{ + // Parse + char ch; + FILE *fp; + fp = fopen(PFILE, "r"); + + char int_buf[6]; + memset(int_buf, '\0', sizeof(int_buf)); + int n = 0; + while((ch = fgetc(fp)) != EOF) + { + if(ch != ',') + { + int_buf[n] = ch; + n += 1; + continue; + } + + vals[vals_sz] = atoi(int_buf); + vals_sz += 1; + memset(int_buf, '\0', sizeof(int_buf)); + n = 0; + } + +} + +void Part1() +{ + Parse(); + size_t fuel_arr[CAP]; + size_t fuel = 0; + + for(int i = 0; i < CAP; i++) + { + int target = i; + for(int j = 0; j < vals_sz; j++) + fuel += abs(target - vals[j]); + + fuel_arr[i] = fuel; + fuel = 0; + } + + // Find the smallest in the fuel array + for(size_t i = 0; i < CAP; i++) + { + size_t counter = 0; + for(int j = 0; j < CAP; j++) + { + if(fuel_arr[i] <= fuel_arr[j]) + counter += 1; + } + + if(counter == CAP) + { + printf("%d\n", fuel_arr[i]); + break; + } + } +} + +size_t CountFuel(int target) +{ + // somehow works idk + size_t sum = 0; + for(size_t i = 1; i <= target; i++) + sum += i; + return sum; +} + +void Part2() +{ + Parse(); + size_t fuel_arr[CAP]; + size_t fuel = 0; + + for(int i = 0; i < CAP; i++) + { + size_t target = i; + for(int j = 0; j < vals_sz; j++) + fuel += CountFuel(abs(target - vals[j])); + + fuel_arr[i] = fuel; + fuel = 0; + } + + // Find the smallest in the fuel array + for(size_t i = 0; i < CAP; i++) + { + size_t counter = 0; + for(size_t j = 0; j < CAP; j++) + { + if(fuel_arr[i] <= fuel_arr[j]) + counter += 1; + } + + if(counter == CAP) + { + printf("%d: %d\n", i, fuel_arr[i]); + break; + } + } +} + +int main() +{ + Part2(); + return 0; +} -- cgit v1.2.3