summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2021/AOC-3/main.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-08-01 23:44:04 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-08-01 23:44:04 +0300
commit4e9132b983c771d3e70d5a6b8bcdfdb1fead81fa (patch)
tree9f5e195a6c8600f065a057004670c00cfa5abee1 /Advent-of-Code-2021/AOC-3/main.c
parent1d96c68c66ba5648d1dd4d8ff2976fc97eea8a14 (diff)
some more
Diffstat (limited to 'Advent-of-Code-2021/AOC-3/main.c')
-rw-r--r--Advent-of-Code-2021/AOC-3/main.c127
1 files changed, 69 insertions, 58 deletions
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <assert.h>
-#include <stdint.h>
-#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<N; i++)
- {
- printf("bitplace %d: %d\n", i+1, table[i]);
- }
+ for(int i = 0; i < NUMS; i++)
+ printf("%b\n", nums[i]);
}
-int PartOne()
+void part_1()
{
- int gamma_val = 0;
- for(int i=0; i<N; i++)
- {
- assert(table[i] != (INP_COUNT/2));
-
- if(table[i] > (INP_COUNT/2))
- {
- gamma_val = gamma_val | (1 << (N-1-i));
- }
- }
-
- int epsilon_val = gamma_val;
- for(int i=0; i<N; i++)
- {
- epsilon_val = epsilon_val ^ (1 << i);
- }
+ ulong gamma = gen_gamma();
+ ulong epsilon = (~gamma & ~(~0 << BITS));
- int result = gamma_val * epsilon_val;
- return result;
+ printf("Power consumption is %lu", gamma*epsilon);
}
-int PartTwo()
+void part_2()
{
-
-
- int result = 0;
- return result;
+ exit(EXIT_FAILURE);
}
int main(void)
{
- memset(table, 0, sizeof(int)*N);
- ParseInput("sample.txt");
+ parse();
+
+#ifdef PART_1
+ part_1();
+#endif
+#ifdef PART_2
+ part_2();
+#endif
- PrintTable();
- printf("Part 1-RESULT: %d\n", PartOne());
-
- return 0;
+ return 0;
}