-
-
Save randrew/dcda93eff81fa42d4d633dd848ff8466 to your computer and use it in GitHub Desktop.
aoc 2020 day 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void) | |
{ | |
FILE *f = fopen("input.txt", "r"); | |
char line[BUFSIZ]; | |
int highest = 0, y, x, i; | |
while (fgets(line, BUFSIZ, f)) | |
{ | |
for (y = x = i = 0; i < 10; i++) | |
{ | |
if (line[i] == 'B') y += 64 >> i; | |
if (line[i] == 'R') x += 4 >> (i - 7); | |
} | |
if (y * 8 + x > highest) highest = y * 8 + x; | |
} | |
printf("%d\n", highest); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main(void) | |
{ | |
FILE *f = fopen("input.txt", "r"); | |
char line[BUFSIZ], seats[128] = {0}; | |
int H = sizeof seats, W = 8, y, x, i, row, me; | |
while (fgets(line, BUFSIZ, f)) | |
{ | |
for (y = x = i = 0; i < 10; i++) | |
{ | |
if (line[i] == 'B') y += H >> (i + 1); | |
if (line[i] == 'R') x += W >> (i - 6); | |
} | |
seats[y] |= 1 << x; | |
} | |
for (y = 0; y < H; y++) | |
{ | |
for (x = row = 0; x < W; x++) | |
if (seats[y] & 1 << x) row++; else me = x; | |
if (row == 7) printf("%d\n", y * 8 + me); | |
} | |
} |
Also, you can replace the +=
addition with a |=
bitwise OR. They're equivalent, in this case.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative input reading loop: