day 1 golfed M4 500 significant bytes
[aoc_eblake.git] / 2017 / advent10.c
blob061aafca7d0911c7b18606956136cc6a17d4f0d4
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
7 #if 0 // part 1
9 int main(int argc, char **argv)
11 if (argc != 3)
12 return 1;
13 int size = atoi(argv[1]);
14 if (size < 2 || size > 256)
15 return 2;
16 unsigned char *list = malloc (size);
17 for (int i = 0; i < size; i++)
18 list[i] = i;
19 char *p = argv[2];
20 char *end;
21 int skip = 0;
22 int pos = 0;
23 int length;
24 while ((length = strtol (p, &end, 10)), p != end) {
25 printf ("length %d, skip %d, pos %d\n", length, skip, pos % size);
26 for (int i = 0; i < length / 2; i++) {
27 int tmp = list[(i + pos) % size];
28 list[(i + pos) % size] = list[(length - i - 1 + pos) % size];
29 list[(length - i - 1 + pos) % size] = tmp;
31 if (size == 5)
32 printf (" %d %d %d %d %d\n", list[0], list[1], list[2], list[3], list[4]);
33 pos += length + skip++;
34 p = end + 1;
36 printf ("final product %d\n", list[0] * list[1]);
37 return 0;
40 #else // part 2
42 int main(int argc, char **argv)
44 if (argc != 2)
45 return 1;
46 const int size = 256;
47 unsigned char list[size];
48 for (int i = 0; i < size; i++)
49 list[i] = i;
50 int skip = 0;
51 int pos = 0;
52 const char suffix[6] = { 17, 31, 73, 47, 23 };
53 char *str = malloc(strlen(argv[1]) + 5 + 1);
54 strcpy(stpcpy(str, argv[1]), suffix);
55 for (int j = 0; j < 64; j++) {
56 printf ("round %d, skip %d, pos %d\n", j, skip, pos % size);
57 for (char *p = str; *p; p++) {
58 int length = *p;
59 for (int i = 0; i < length / 2; i++) {
60 int tmp = list[(i + pos) % size];
61 list[(i + pos) % size] = list[(length - i - 1 + pos) % size];
62 list[(length - i - 1 + pos) % size] = tmp;
64 pos += length + skip++;
67 unsigned char chunk = 0;
68 for (int i = 0; i < size; i++) {
69 chunk ^= list[i];
70 if (i % 16 == 15) {
71 printf ("%02x", chunk);
72 chunk = 0;
75 puts ("");
76 return 0;
79 #endif