mirror of
https://github.com/peter-tanner/advent-of-code-2021.git
synced 2024-11-30 11:10:20 +08:00
46 lines
799 B
C
46 lines
799 B
C
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
#define WIDTH 5
|
||
|
|
||
|
int main(int argc, char const *argv[])
|
||
|
{
|
||
|
FILE *p_file = fopen("input", "r");
|
||
|
|
||
|
uint n_one[WIDTH] = {0};
|
||
|
uint length = 0;
|
||
|
|
||
|
char line[WIDTH + 2];
|
||
|
while (fgets(line, sizeof line, p_file) != NULL)
|
||
|
{
|
||
|
length++;
|
||
|
for (size_t i = 0; i < WIDTH; i++)
|
||
|
{
|
||
|
if (line[i] - '0') // 1
|
||
|
{
|
||
|
n_one[i]++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// GET GAMMA/EPSILON
|
||
|
uint gamma = 0, epsilon = 0;
|
||
|
uint place = 1;
|
||
|
for (size_t i = WIDTH; i--;)
|
||
|
{
|
||
|
if (n_one[i] > length / 2)
|
||
|
{
|
||
|
gamma += place;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
epsilon += place;
|
||
|
}
|
||
|
place *= 2;
|
||
|
}
|
||
|
|
||
|
printf("%d\n", gamma * epsilon);
|
||
|
|
||
|
return 0;
|
||
|
}
|