From 3e0f83d638a38902621edfdbbabcafdeb2a8aa38 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Fri, 11 Dec 2020 14:23:56 +0100 Subject: [PATCH] c implementatino --- 11/one.c | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 11/one.c diff --git a/11/one.c b/11/one.c new file mode 100644 index 0000000..c6fb07e --- /dev/null +++ b/11/one.c @@ -0,0 +1,113 @@ +#include +#include +#include + +enum cellstatus {floor, empty, occup, oob}; + +uint16_t map1[100][100] = {oob}; +uint16_t map2[100][100] = {oob}; +int xmax, ymax; + +enum cellstatus seat(int x, int y, uint16_t map[100][100]) +{ + return x < 0 || y < 0 || x >= xmax || y >= ymax ? oob + : (enum cellstatus)((map[x][y] >> 8) & 0xff); +} + +bool lookone(int x, int y, int dx, int dy, uint16_t map[100][100]) +{ + return seat(x+dx, y+dy, map) == occup; +} + +bool looktwo(int x, int y, int dx, int dy, uint16_t map[100][100]) +{ + enum cellstatus s; + while((s = seat(x += dx, y += dy, map)) == floor); + return s == occup; +} + +bool step(bool (*lookfun)(int, int, int, int, uint16_t map[100][100]), int die, uint16_t map[100][100]) +{ + //Move the old to the leftmost byte + for (int y = 0; y= die) { + same = false; + map[x][y] |= empty; + } else { + map[x][y] |= occup; + } + } + } + } + return same; +} + +int main() +{ + int x = 0, y = 0; + int c; + while ((c = getchar()) != EOF) { + switch (c) { + case 'L': + map1[x][y] = empty; + map2[x++][y] = empty; + break; + case '.': + map1[x][y] = floor; + map2[x++][y] = floor; + break; + case '\n': + xmax = x; + x = 0; + y++; + break; + } + } + ymax = y; + + //Do one + while(!step(lookone, 4, map1)); + + //Do two + while(!step(looktwo, 5, map2)); + + int count1 = 0, count2 = 0; + for (int y = 0; y