day 2 golf to 389 effective bytes
[aoc_eblake.git] / 2016 / advent1.c
blob4dcb174ef7cdce1acfa7fa5743d8aaa9c884412b
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>
8 typedef enum dir {
9 N,
13 } dir;
15 #define BASE 220
16 bool grid[2*BASE][2*BASE]; // cheat: size it by analysis done in part 1
17 int x = BASE, y = BASE;
19 void travel(int dx, int dy, int n)
21 static bool found;
22 while (n--) {
23 x += dx;
24 y += dy;
25 if (grid[x][y] && !found) {
26 found = true;
27 printf ("revisiting spot %d,%d, distance of %d\n", x - BASE, y - BASE,
28 abs (x - BASE) + abs (y - BASE));
30 grid[x][y] = true;
34 int main(void)
36 dir d = N;
37 char c;
38 int l;
39 int maxx = BASE, minx = BASE, maxy = BASE, miny = BASE;
40 grid[x][y] = true;
41 while (scanf ("%c%d, ", &c, &l) == 2) {
42 if (c == 'R')
43 d++;
44 else
45 d--;
46 switch (d % 4) {
47 case N:
48 travel (0, 1, l);
49 if (y > maxy)
50 maxy = y;
51 break;
52 case E:
53 travel (1, 0, l);
54 if (x > maxx)
55 maxx = x;
56 break;
57 case S:
58 travel (0, -1, l);
59 if (y < miny)
60 miny = y;
61 break;
62 case W:
63 travel (-1, 0, l);
64 if (x < minx)
65 minx = x;
66 break;
69 printf ("final distance is %d\n", abs (x - BASE) + abs (y - BASE));
70 printf ("grid size x=%d to %d y=%d to %d\n", minx - BASE, maxx - BASE,
71 miny - BASE, maxy - BASE);
72 return 0;