diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-05 10:37:21 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-05 10:37:21 +0100 |
commit | 2e28981207430301b54d4d74ee620d0f159f4d8b (patch) | |
tree | 5f6af101fd4ee09b16f6f9fdc9e9f002c69bf6ed /2017/03/puzzle-1.c | |
parent | 29fce7fa2a01fe00543e317514e505b4b6524c2b (diff) |
Add day 3 solutions
Diffstat (limited to '2017/03/puzzle-1.c')
-rw-r--r-- | 2017/03/puzzle-1.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/2017/03/puzzle-1.c b/2017/03/puzzle-1.c new file mode 100644 index 0000000..00cebd7 --- /dev/null +++ b/2017/03/puzzle-1.c @@ -0,0 +1,50 @@ +#include <err.h> +#include <stdio.h> +#include <stdlib.h> + +#define ODD(x) (x & 1) +#define MOVE(x) \ + do { \ + for (int k = 0; k < j; k++) { \ + x; \ + if (++m == n) \ + goto out; \ + } \ + } while (0) + +int +main(void) +{ + int n, x, y; + FILE *fp; + + if (!(fp = fopen("input", "r"))) + err(EXIT_FAILURE, "fopen"); + + fscanf(fp, "%d", &n); + fclose(fp); + + x = y = 0; + for (int i = 1, m = 1, j = 1; i < n; i++) { + if (ODD(i)) { + if (ODD(j)) + MOVE(x++); + else + MOVE(x--); + } else { + if (ODD(j)) + MOVE(y++); + else + MOVE(y--); + j++; + } + } +out: + if (x < 0) + x = -x; + if (y < 0) + y = -y; + + printf("%d\n", x + y); + return EXIT_SUCCESS; +} |