day 8 fix for POSIX
[aoc_eblake.git] / 2017 / advent25.c
blob3b4bb30cb4d026dc0c05e3694c5b1f5ee09f0bb8
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdbool.h>
7 #include <ctype.h>
8 #include <assert.h>
10 #define MAX 30000 // educated guess
11 typedef enum State State;
12 enum State {
13 A, B, C, D, E, F,
15 typedef struct state state;
16 struct state {
17 bool write;
18 bool right;
19 State next;
21 bool tape[MAX];
23 // open-coding based on the input, instead of parsing it...
24 #if 0 // sample
25 #define LIMIT 6
26 state states[2][2] = {
27 { { true, true, B }, { false, false, B }, },
28 { { true, false, A }, { true, true, A }, },
31 #else // day25.input
32 #define LIMIT 12861455
33 state states[6][2] = {
34 { { true, true, B }, { false, false, B }, },
35 { { true, false, C }, { false, true, E }, },
36 { { true, true, E }, { false, false, D }, },
37 { { true, false, A }, { true, false, A }, },
38 { { false, true, A }, { false, true, F }, },
39 { { true, true, E }, { true, true, A }, },
41 #endif
43 int main(int argc, char **argv)
45 int limit = LIMIT;
46 if (argc > 1)
47 limit = atoi (argv[1]);
48 int sum = 0;
49 State S = A;
50 int pos = MAX/2;
51 int min = MAX;
52 int max = 0;
53 while (limit--) {
54 state *s = &states[S][tape[pos]];
55 if (!tape[pos] && s->write)
56 sum++;
57 else if (tape[pos] && !s->write)
58 sum--;
59 tape[pos] = s->write;
60 S = s->next;
61 pos = pos + (s->right ? 1 : -1);
62 if (pos < 0 || pos == MAX) {
63 printf ("still %d ops to perform, out of tape, used %d-%d\n",
64 limit, min, max);
65 return 1;
67 if (pos < min)
68 min = pos;
69 else if (pos > max)
70 max = pos;
72 printf ("checksum is %d\n", sum);
73 return 0;