This commit is contained in:
parent
020b576e1c
commit
96b14c9c42
46
day3.c
46
day3.c
@ -2,7 +2,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define INPUT "inputs/input-day3"
|
||||
|
||||
@ -63,8 +62,8 @@ load_all(int count)
|
||||
return i;
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
void
|
||||
first()
|
||||
{
|
||||
int cnt = count_lines();
|
||||
uint16_t *v = load_all(cnt);
|
||||
@ -94,8 +93,47 @@ main()
|
||||
print_bits(gamma, 16);
|
||||
print_bits(epsilon, 16);
|
||||
printf("result: %d\n", gamma * epsilon);
|
||||
|
||||
free(v);
|
||||
}
|
||||
|
||||
void
|
||||
second()
|
||||
{
|
||||
int cnt = count_lines();
|
||||
uint16_t *v = load_all(cnt);
|
||||
|
||||
uint16_t gamma = 0;
|
||||
uint16_t epsilon = 0;
|
||||
|
||||
for(int z = 0; z < 12; ++z) {
|
||||
int high = 0;
|
||||
int low = 0;
|
||||
for(int i = 0; i < cnt; ++i) {
|
||||
int bit = BIT_CHECK(v[i], z);
|
||||
if(bit) {
|
||||
high++;
|
||||
} else {
|
||||
low++;
|
||||
}
|
||||
}
|
||||
if(high > low) {
|
||||
BIT_SET(gamma, (11-z));
|
||||
} else {
|
||||
BIT_SET(epsilon, (11-z));
|
||||
}
|
||||
}
|
||||
|
||||
printf("gamma: %d\nepsilon: %d\n", gamma, epsilon);
|
||||
print_bits(gamma, 16);
|
||||
print_bits(epsilon, 16);
|
||||
printf("result: %d\n", gamma * epsilon);
|
||||
free(v);
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
first();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user