From 4e9132b983c771d3e70d5a6b8bcdfdb1fead81fa Mon Sep 17 00:00:00 2001 From: kartofen Date: Mon, 1 Aug 2022 23:44:04 +0300 Subject: some more --- Advent-of-Code-2021/AOC-3/build.sh | 7 ++ Advent-of-Code-2021/AOC-3/example.txt | 12 ++++ Advent-of-Code-2021/AOC-3/main.c | 127 ++++++++++++++++++---------------- Advent-of-Code-2021/AOC-3/sample.txt | 12 ---- 4 files changed, 88 insertions(+), 70 deletions(-) create mode 100755 Advent-of-Code-2021/AOC-3/build.sh create mode 100644 Advent-of-Code-2021/AOC-3/example.txt delete mode 100644 Advent-of-Code-2021/AOC-3/sample.txt (limited to 'Advent-of-Code-2021/AOC-3') diff --git a/Advent-of-Code-2021/AOC-3/build.sh b/Advent-of-Code-2021/AOC-3/build.sh new file mode 100755 index 0000000..6be9241 --- /dev/null +++ b/Advent-of-Code-2021/AOC-3/build.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -xe + +gcc -o main -Wall -Wextra main.c + +./main diff --git a/Advent-of-Code-2021/AOC-3/example.txt b/Advent-of-Code-2021/AOC-3/example.txt new file mode 100644 index 0000000..a6366a8 --- /dev/null +++ b/Advent-of-Code-2021/AOC-3/example.txt @@ -0,0 +1,12 @@ +00100 +11110 +10110 +10111 +10101 +01111 +00111 +11100 +10000 +11001 +00010 +01010 diff --git a/Advent-of-Code-2021/AOC-3/main.c b/Advent-of-Code-2021/AOC-3/main.c index 51cab24..29d334e 100644 --- a/Advent-of-Code-2021/AOC-3/main.c +++ b/Advent-of-Code-2021/AOC-3/main.c @@ -1,86 +1,97 @@ #include #include #include -#include -#include -#define N 5 -#define INP_COUNT 12 +#if 1 + #define PART_1 +#else + #define PART_2 +#endif -int table[N]; +#if 0 + #define FILE_PATH "example.txt" + #define NUMS 12 + #define BITS 5 +#else + #define FILE_PATH "input.txt" + #define NUMS 1000 + #define BITS 12 +#endif -void ParseInput(char *filepath) +int nums[NUMS] = {0}; + +void parse() { - char ch; - FILE *fp; - fp = fopen(filepath, "r"); + FILE *fp = fopen(FILE_PATH, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file %s\n", FILE_PATH); + exit(EXIT_FAILURE); + } - if(fp == NULL) - { - fprintf(stderr, "ERROR: something with file idk what fuck you"); - exit(EXIT_FAILURE); - } + char line[32]; + int index = 0; + while(fgets(line, sizeof(line), fp) != NULL) + { + for(int i = 0; i < BITS; i++) + nums[index] |= (line[i] - '0') << (BITS-1 - i); + + index++; + } - int i = 0; - while((ch = fgetc(fp)) != EOF) - { - if(ch == '\n') { i = 0; continue; } + fclose(fp); +} - if(ch == '1') - table[i] += 1; +ulong gen_gamma() +{ + int most_common[BITS] = {0}; + + for(int i = 0; i < BITS; i++) + { + int ones = 0; - i += 1; - } + for(int j = 0; j < NUMS; j++) + if(nums[j] & (1 << i)) ones++; - fclose(fp); + if(ones > NUMS/2) most_common[i] = 1; + } + + ulong gamma = 0; + + for(int i = 0; i < BITS; i++) + gamma |= most_common[i] << i; + + return gamma; } -void PrintTable() +void print_nums() { - for(int i=0; i (INP_COUNT/2)) - { - gamma_val = gamma_val | (1 << (N-1-i)); - } - } - - int epsilon_val = gamma_val; - for(int i=0; i