Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@randrew
Created December 5, 2020 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrew/dcda93eff81fa42d4d633dd848ff8466 to your computer and use it in GitHub Desktop.
Save randrew/dcda93eff81fa42d4d633dd848ff8466 to your computer and use it in GitHub Desktop.
aoc 2020 day 5
#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);
}
#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);
}
}
@randrew
Copy link
Author

randrew commented Dec 5, 2020

Alternative input reading loop:

    while (fgets(line, BUFSIZ, f))
    {
        y = x = i = 0;
        while (i <  7) if (line[i++] == 'B') y += H >> i;
        while (i < 10) if (line[i++] == 'R') x += W >> (i - 7);
        seats[y] |= 1 << x;
    }

@randrew
Copy link
Author

randrew commented Dec 5, 2020

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