Day 13

Previous: Day 12 | Next: Day 14

Haha, ship go brrrrrrrrrrr

###AUTO-ACQUIRED DATA FOLLOWS…
day-13/day13-2.py
day-13/day13-2.py
\#Day 13 Part 2
    from sys import stdin
    stdin.readline()
    ids = list(stdin.readline().rstrip().split(&\#39;,&\#39;))
    print(&\#39;https://www.wolframalpha.com/input/?i=0+%3D+&\#39; + &\#39;+%3D+&\#39;.join([&\#39;((n+%2B+{})+mod+{})&\#39;.format(i, n) for i, n in enumerate(ids) if n != &\#39;x&\#39;]))
    
unknown-cnile/ucnile-day13.c
unknown-cnile/ucnile-day13.c
\#include <stdlib.h>
    \#include <stdio.h>
    \#include <string.h>
    \#include <sys/stat.h>
    \#include <fcntl.h>
    \#include <sys/mman.h>
    \#include <unistd.h>
    \#include <complex.h>
    
    void die(char \*str) {
        fprintf(stderr, "[ERROR] %s\n", str);
        exit(-1);
    }
    
    FILE \*map\_file(const char \*filename) {
        int fd;
        if ((fd = open(filename, O\_RDONLY)) == -1) die("failed to open file");
        struct stat fd\_stat;
        if (fstat(fd, &fd\_stat) == -1) die("failed to stat file");
        void \*data;
        // never unmapped, probably fine
        if ((data = mmap(NULL,
                         fd\_stat.st\_size,
                         PROT\_READ,
                         MAP\_PRIVATE,
                         fd, 0)) == MAP\_FAILED) die("failed to map file");
        close(fd);
        return fmemopen(data, fd\_stat.st\_size, "r");
    }
    
    \#define MAX\_LINE 32
    
    int main(int argc, char \*\*argv) {
        // read input
        FILE \*fd = map\_file("test.txt");
        char line[MAX\_LINE];
        \_Complex int p1\_s = 0;
        \_Complex int p1\_v = 1;
        \_Complex int p2\_s = 0;
        \_Complex int p2\_v = 10+1i;
        while (fgets(line, MAX\_LINE, fd) != NULL) {
            char c;
            unsigned int n;
            if (sscanf(line, "%c%u", &c, &n) != 2) break;
    // generates case for cardinal direction
    \#define PMSK(c, v) case c: p1\_s += n \* v; p2\_v += n \* v; break;
    // generates case for ccw angle change
    \#define RMSK(a, v) case a: p1\_v \*= v; p2\_v \*= v; break;
    // generates switch statements with trap on default
    \#define SW\_SAFE\_END default: \_\_builtin\_trap(); }
    \#define SW\_SAFE(sw, body) switch (sw) { body SW\_SAFE\_END
            switch (c) {
                PMSK(&\#39;N&\#39;, 1i) PMSK(&\#39;S&\#39;, -1i) PMSK(&\#39;E&\#39;, 1) PMSK(&\#39;W&\#39;, -1)
                case &\#39;R&\#39;: n = 360 - n;
                case &\#39;L&\#39;:
                    SW\_SAFE(n, RMSK(90, 1i) RMSK(180, -1) RMSK(270, -1i))
                    break;
                case &\#39;F&\#39;:
                    p1\_s += n \* p1\_v;
                    p2\_s += n \* p2\_v;
                    break;
            SW\_SAFE\_END
        }
        // part one
        printf("P1: %d\n", abs(\_\_real\_\_ p1\_s) + abs(\_\_imag\_\_ p1\_s));
        // part two
        printf("P2: %d\n", abs(\_\_real\_\_ p2\_s) + abs(\_\_imag\_\_ p2\_s));
        return 0;
    }
    

Tags: