mirror of
https://github.com/peter-tanner/advent-of-code-2021.git
synced 2024-11-30 19:20:19 +08:00
46 lines
831 B
C
46 lines
831 B
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#define WIDTH 5
|
|
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
FILE *p_file = fopen("input", "r");
|
|
|
|
unsigned int n_one[WIDTH] = {0};
|
|
unsigned int 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
|
|
unsigned int gamma = 0, epsilon = 0;
|
|
unsigned int 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;
|
|
}
|