Initial commit
This commit is contained in:
commit
f242d2b0df
420 changed files with 62521 additions and 0 deletions
119
2020/day12/day12.c
Normal file
119
2020/day12/day12.c
Normal file
|
@ -0,0 +1,119 @@
|
|||
#include <complex.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define PI
|
||||
|
||||
struct instruction {
|
||||
char action;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct state {
|
||||
double complex pos;
|
||||
double complex dir;
|
||||
};
|
||||
|
||||
void step1(struct state *st, struct instruction instr) {
|
||||
switch (instr.action) {
|
||||
case 'N': {
|
||||
st->pos += instr.value * I;
|
||||
break;
|
||||
}
|
||||
case 'S': {
|
||||
st->pos -= instr.value * I;
|
||||
break;
|
||||
}
|
||||
case 'E': {
|
||||
st->pos += instr.value;
|
||||
break;
|
||||
}
|
||||
case 'W': {
|
||||
st->pos -= instr.value;
|
||||
break;
|
||||
}
|
||||
case 'F': {
|
||||
st->pos += instr.value * st->dir;
|
||||
break;
|
||||
}
|
||||
case 'R': {
|
||||
st->dir *= cexp(-instr.value * I * M_PI / 180);
|
||||
break;
|
||||
}
|
||||
case 'L': {
|
||||
st->dir *= cexp(instr.value * I * M_PI / 180);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
printf("Invalid instruction %c %d\n", instr.action, instr.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void step2(struct state *st, struct instruction instr) {
|
||||
switch (instr.action) {
|
||||
case 'N': {
|
||||
st->dir += instr.value * I;
|
||||
break;
|
||||
}
|
||||
case 'S': {
|
||||
st->dir -= instr.value * I;
|
||||
break;
|
||||
}
|
||||
case 'E': {
|
||||
st->dir += instr.value;
|
||||
break;
|
||||
}
|
||||
case 'W': {
|
||||
st->dir -= instr.value;
|
||||
break;
|
||||
}
|
||||
case 'F': {
|
||||
st->pos += instr.value * st->dir;
|
||||
break;
|
||||
}
|
||||
case 'R': {
|
||||
st->dir *= cexp(-instr.value * I * M_PI / 180);
|
||||
break;
|
||||
}
|
||||
case 'L': {
|
||||
st->dir *= cexp(instr.value * I * M_PI / 180);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
printf("Invalid instruction %c %d\n", instr.action, instr.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s <input file>\n", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *fp = fopen(argv[1], "r");
|
||||
if (fp == NULL) {
|
||||
printf("Could not open file %s\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct instruction in[1024];
|
||||
int n = 0;
|
||||
while (fscanf(fp, "%c%d\n", &in[n].action, &in[n].value) == 2) {
|
||||
n++;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
struct state st1 = {.pos = 0, .dir = 1};
|
||||
struct state st2 = {.pos = 0, .dir = 10 + I};
|
||||
for (int i = 0; i < n; ++i) {
|
||||
step1(&st1, in[i]);
|
||||
step2(&st2, in[i]);
|
||||
}
|
||||
printf("%d\n", (int)round(fabs(creal(st1.pos)) + fabs(cimag(st1.pos))));
|
||||
printf("%d\n", (int)round(fabs(creal(st2.pos)) + fabs(cimag(st2.pos))));
|
||||
|
||||
return 0;
|
||||
}
|
66
2020/day12/day12.rkt
Normal file
66
2020/day12/day12.rkt
Normal file
|
@ -0,0 +1,66 @@
|
|||
#lang racket
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
(module+ main
|
||||
(displayln "Day 12"))
|
||||
|
||||
(define (parse-instruction str)
|
||||
(list (string->symbol (substring str 0 1))
|
||||
(string->number (substring str 1))))
|
||||
|
||||
(define (read-input filename)
|
||||
(map parse-instruction (file->lines filename)))
|
||||
|
||||
(struct state
|
||||
(pos dir)
|
||||
#:transparent)
|
||||
|
||||
(define (step st instr)
|
||||
(match instr
|
||||
[`(N ,n) (struct-copy state st [pos (+ (state-pos st) (make-rectangular 0 n))])]
|
||||
[`(S ,n) (struct-copy state st [pos (- (state-pos st) (make-rectangular 0 n))])]
|
||||
[`(E ,n) (struct-copy state st [pos (+ (state-pos st) (make-rectangular n 0))])]
|
||||
[`(W ,n) (struct-copy state st [pos (- (state-pos st) (make-rectangular n 0))])]
|
||||
[`(F ,n) (struct-copy state st [pos (+ (state-pos st) (* n (state-dir st)))])]
|
||||
[`(R ,n) (struct-copy state st [dir (* (state-dir st) (make-polar 1 (degrees->radians (- n))))])]
|
||||
[`(L ,n) (struct-copy state st [dir (* (state-dir st) (make-polar 1 (degrees->radians n)))])]))
|
||||
|
||||
(define (part1 filename)
|
||||
(define input (read-input filename))
|
||||
(define final
|
||||
(state-pos (for/fold ([st (state 0 1)])
|
||||
([instr (in-list input)])
|
||||
(step st instr))))
|
||||
(exact-round (+ (abs (real-part final)) (abs (imag-part final)))))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (part1 "test") 25))
|
||||
|
||||
(module+ main
|
||||
(displayln (part1 "input")))
|
||||
|
||||
(define (step2 st instr)
|
||||
(match instr
|
||||
[`(N ,n) (struct-copy state st [dir (+ (state-dir st) (make-rectangular 0 n))])]
|
||||
[`(S ,n) (struct-copy state st [dir (- (state-dir st) (make-rectangular 0 n))])]
|
||||
[`(E ,n) (struct-copy state st [dir (+ (state-dir st) (make-rectangular n 0))])]
|
||||
[`(W ,n) (struct-copy state st [dir (- (state-dir st) (make-rectangular n 0))])]
|
||||
[`(F ,n) (struct-copy state st [pos (+ (state-pos st) (* n (state-dir st)))])]
|
||||
[`(R ,n) (struct-copy state st [dir (* (state-dir st) (make-polar 1 (degrees->radians (- n))))])]
|
||||
[`(L ,n) (struct-copy state st [dir (* (state-dir st) (make-polar 1 (degrees->radians n)))])]))
|
||||
|
||||
(define (part2 filename)
|
||||
(define input (read-input filename))
|
||||
(define final
|
||||
(state-pos (for/fold ([st (state 0 (make-rectangular 10 1))])
|
||||
([instr (in-list input)])
|
||||
(step2 st instr))))
|
||||
(exact-round (+ (abs (real-part final)) (abs (imag-part final)))))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (part2 "test") 286))
|
||||
|
||||
(module+ main
|
||||
(displayln (part2 "input")))
|
776
2020/day12/input
Normal file
776
2020/day12/input
Normal file
|
@ -0,0 +1,776 @@
|
|||
R180
|
||||
E1
|
||||
N1
|
||||
R90
|
||||
E4
|
||||
F84
|
||||
W3
|
||||
F19
|
||||
E5
|
||||
N1
|
||||
W5
|
||||
W4
|
||||
R90
|
||||
S1
|
||||
F70
|
||||
R90
|
||||
W2
|
||||
S3
|
||||
L180
|
||||
E2
|
||||
R90
|
||||
E1
|
||||
R90
|
||||
N2
|
||||
E3
|
||||
F55
|
||||
W3
|
||||
R90
|
||||
S2
|
||||
E4
|
||||
L90
|
||||
F33
|
||||
W4
|
||||
S3
|
||||
F11
|
||||
N4
|
||||
W4
|
||||
F88
|
||||
W3
|
||||
F62
|
||||
L90
|
||||
S3
|
||||
W3
|
||||
N1
|
||||
E3
|
||||
E2
|
||||
N1
|
||||
E3
|
||||
S3
|
||||
E5
|
||||
S4
|
||||
W5
|
||||
L90
|
||||
W5
|
||||
R90
|
||||
E4
|
||||
F36
|
||||
N4
|
||||
E4
|
||||
E5
|
||||
F47
|
||||
R90
|
||||
N3
|
||||
E3
|
||||
L180
|
||||
S5
|
||||
R90
|
||||
S5
|
||||
W2
|
||||
S3
|
||||
F43
|
||||
W2
|
||||
R180
|
||||
W2
|
||||
S2
|
||||
L180
|
||||
E2
|
||||
F49
|
||||
L90
|
||||
F77
|
||||
S5
|
||||
E1
|
||||
S2
|
||||
F39
|
||||
L180
|
||||
F12
|
||||
W1
|
||||
L90
|
||||
F60
|
||||
S2
|
||||
E3
|
||||
N1
|
||||
E5
|
||||
R90
|
||||
E1
|
||||
N4
|
||||
W4
|
||||
S4
|
||||
E2
|
||||
L90
|
||||
F100
|
||||
E3
|
||||
F58
|
||||
S2
|
||||
E4
|
||||
F83
|
||||
W3
|
||||
N1
|
||||
R90
|
||||
F99
|
||||
W5
|
||||
W4
|
||||
F19
|
||||
N3
|
||||
W1
|
||||
W5
|
||||
F70
|
||||
R180
|
||||
R90
|
||||
F58
|
||||
E5
|
||||
N3
|
||||
R90
|
||||
N4
|
||||
F86
|
||||
N4
|
||||
F70
|
||||
L90
|
||||
F36
|
||||
R90
|
||||
S4
|
||||
R180
|
||||
N5
|
||||
F81
|
||||
W5
|
||||
R90
|
||||
E5
|
||||
L90
|
||||
F73
|
||||
S5
|
||||
E3
|
||||
N1
|
||||
F27
|
||||
E2
|
||||
N3
|
||||
L180
|
||||
W2
|
||||
F24
|
||||
N2
|
||||
L90
|
||||
F69
|
||||
N2
|
||||
E3
|
||||
F49
|
||||
L90
|
||||
S4
|
||||
W5
|
||||
F18
|
||||
R180
|
||||
F62
|
||||
S2
|
||||
F65
|
||||
E3
|
||||
F100
|
||||
N1
|
||||
F96
|
||||
W1
|
||||
F75
|
||||
L180
|
||||
W2
|
||||
S2
|
||||
F93
|
||||
E5
|
||||
S3
|
||||
R90
|
||||
S2
|
||||
F34
|
||||
R90
|
||||
F83
|
||||
S1
|
||||
W1
|
||||
F58
|
||||
L180
|
||||
N4
|
||||
E1
|
||||
R90
|
||||
W2
|
||||
F93
|
||||
S3
|
||||
F20
|
||||
R90
|
||||
N3
|
||||
E3
|
||||
N3
|
||||
L90
|
||||
F8
|
||||
S3
|
||||
F18
|
||||
S3
|
||||
N2
|
||||
F6
|
||||
L270
|
||||
F70
|
||||
W5
|
||||
S3
|
||||
F54
|
||||
E2
|
||||
F75
|
||||
E3
|
||||
R90
|
||||
N2
|
||||
W3
|
||||
L90
|
||||
E4
|
||||
F58
|
||||
N5
|
||||
F97
|
||||
W2
|
||||
L90
|
||||
W5
|
||||
S2
|
||||
W1
|
||||
S5
|
||||
F85
|
||||
N1
|
||||
E1
|
||||
N4
|
||||
E4
|
||||
L90
|
||||
E1
|
||||
R90
|
||||
S5
|
||||
L180
|
||||
R270
|
||||
N1
|
||||
L90
|
||||
E4
|
||||
N3
|
||||
F45
|
||||
N2
|
||||
F68
|
||||
R90
|
||||
F36
|
||||
N5
|
||||
F82
|
||||
S5
|
||||
E4
|
||||
R180
|
||||
S2
|
||||
L180
|
||||
N3
|
||||
R270
|
||||
W5
|
||||
F70
|
||||
L90
|
||||
W5
|
||||
F80
|
||||
W4
|
||||
N2
|
||||
R90
|
||||
S3
|
||||
W1
|
||||
F23
|
||||
N1
|
||||
W1
|
||||
N4
|
||||
F70
|
||||
S3
|
||||
L180
|
||||
F57
|
||||
R90
|
||||
L90
|
||||
F55
|
||||
L90
|
||||
N4
|
||||
F87
|
||||
L90
|
||||
F1
|
||||
L270
|
||||
F17
|
||||
N5
|
||||
R180
|
||||
F84
|
||||
R90
|
||||
W5
|
||||
F7
|
||||
W1
|
||||
S1
|
||||
E3
|
||||
F46
|
||||
S5
|
||||
E2
|
||||
F23
|
||||
R90
|
||||
E4
|
||||
W2
|
||||
F96
|
||||
E5
|
||||
L90
|
||||
F65
|
||||
F3
|
||||
S5
|
||||
E5
|
||||
N3
|
||||
W4
|
||||
L90
|
||||
S2
|
||||
F57
|
||||
E1
|
||||
R90
|
||||
F68
|
||||
E3
|
||||
L90
|
||||
W1
|
||||
F29
|
||||
N5
|
||||
W5
|
||||
N1
|
||||
F95
|
||||
N1
|
||||
L90
|
||||
F31
|
||||
S5
|
||||
L180
|
||||
N2
|
||||
W5
|
||||
R90
|
||||
F27
|
||||
E1
|
||||
R90
|
||||
E3
|
||||
S5
|
||||
F10
|
||||
R90
|
||||
N4
|
||||
E2
|
||||
F25
|
||||
S4
|
||||
E5
|
||||
F51
|
||||
N3
|
||||
W2
|
||||
L90
|
||||
S3
|
||||
L180
|
||||
F17
|
||||
E4
|
||||
F93
|
||||
E3
|
||||
L90
|
||||
F41
|
||||
L90
|
||||
S5
|
||||
L90
|
||||
W5
|
||||
N1
|
||||
F81
|
||||
L90
|
||||
E4
|
||||
W2
|
||||
R90
|
||||
W1
|
||||
S5
|
||||
R90
|
||||
F39
|
||||
W3
|
||||
R90
|
||||
N5
|
||||
E1
|
||||
L90
|
||||
F82
|
||||
S3
|
||||
R90
|
||||
W4
|
||||
F66
|
||||
F4
|
||||
L90
|
||||
F77
|
||||
R90
|
||||
E1
|
||||
L90
|
||||
F53
|
||||
S4
|
||||
F35
|
||||
W1
|
||||
F64
|
||||
R90
|
||||
F9
|
||||
S1
|
||||
E1
|
||||
L90
|
||||
W4
|
||||
R90
|
||||
S2
|
||||
W5
|
||||
R90
|
||||
S4
|
||||
L90
|
||||
N3
|
||||
F8
|
||||
L180
|
||||
N5
|
||||
E5
|
||||
N4
|
||||
F35
|
||||
N5
|
||||
W1
|
||||
N1
|
||||
E5
|
||||
F15
|
||||
R180
|
||||
F92
|
||||
W3
|
||||
L90
|
||||
F4
|
||||
L90
|
||||
E1
|
||||
S3
|
||||
W3
|
||||
R90
|
||||
F37
|
||||
N5
|
||||
F19
|
||||
S2
|
||||
F98
|
||||
L90
|
||||
F24
|
||||
W3
|
||||
F68
|
||||
N5
|
||||
R90
|
||||
W3
|
||||
L90
|
||||
W3
|
||||
L90
|
||||
S1
|
||||
L90
|
||||
S4
|
||||
W3
|
||||
F56
|
||||
N4
|
||||
R90
|
||||
E3
|
||||
W1
|
||||
L90
|
||||
E4
|
||||
N3
|
||||
R180
|
||||
E1
|
||||
S1
|
||||
W2
|
||||
R90
|
||||
N3
|
||||
F82
|
||||
N2
|
||||
F37
|
||||
S3
|
||||
L180
|
||||
E2
|
||||
L180
|
||||
F6
|
||||
N2
|
||||
F96
|
||||
E2
|
||||
R180
|
||||
E2
|
||||
W3
|
||||
R90
|
||||
E2
|
||||
S5
|
||||
S1
|
||||
F23
|
||||
R90
|
||||
W5
|
||||
F75
|
||||
S1
|
||||
L90
|
||||
S3
|
||||
E1
|
||||
F83
|
||||
W4
|
||||
L180
|
||||
W5
|
||||
L90
|
||||
N1
|
||||
E1
|
||||
S2
|
||||
F17
|
||||
L90
|
||||
S2
|
||||
F53
|
||||
R90
|
||||
S3
|
||||
N3
|
||||
W1
|
||||
N4
|
||||
L180
|
||||
L90
|
||||
E3
|
||||
F9
|
||||
S5
|
||||
F24
|
||||
W3
|
||||
E5
|
||||
N2
|
||||
F73
|
||||
N1
|
||||
F28
|
||||
N2
|
||||
W4
|
||||
N3
|
||||
F53
|
||||
E5
|
||||
F47
|
||||
W2
|
||||
F60
|
||||
L90
|
||||
E2
|
||||
F19
|
||||
S1
|
||||
F63
|
||||
W5
|
||||
F100
|
||||
N3
|
||||
L180
|
||||
F83
|
||||
N4
|
||||
W5
|
||||
F37
|
||||
S1
|
||||
F50
|
||||
E1
|
||||
N2
|
||||
W3
|
||||
R90
|
||||
F85
|
||||
S4
|
||||
F72
|
||||
N4
|
||||
L90
|
||||
F48
|
||||
R90
|
||||
F99
|
||||
R90
|
||||
F58
|
||||
W3
|
||||
W4
|
||||
F64
|
||||
E1
|
||||
R90
|
||||
F74
|
||||
L90
|
||||
F23
|
||||
N3
|
||||
N3
|
||||
E1
|
||||
S1
|
||||
W5
|
||||
L180
|
||||
F98
|
||||
L90
|
||||
F36
|
||||
W4
|
||||
S2
|
||||
W3
|
||||
F9
|
||||
F72
|
||||
W5
|
||||
F78
|
||||
N2
|
||||
F65
|
||||
S3
|
||||
F47
|
||||
S5
|
||||
R90
|
||||
F68
|
||||
L180
|
||||
W2
|
||||
F7
|
||||
E2
|
||||
E3
|
||||
S4
|
||||
R90
|
||||
N2
|
||||
L180
|
||||
W2
|
||||
R180
|
||||
E4
|
||||
R90
|
||||
W3
|
||||
L90
|
||||
E4
|
||||
F54
|
||||
L180
|
||||
E2
|
||||
F6
|
||||
W5
|
||||
F82
|
||||
E4
|
||||
R90
|
||||
E4
|
||||
F25
|
||||
N2
|
||||
R270
|
||||
N4
|
||||
F18
|
||||
N5
|
||||
R90
|
||||
S3
|
||||
R90
|
||||
F38
|
||||
R90
|
||||
F97
|
||||
W4
|
||||
F85
|
||||
S4
|
||||
F56
|
||||
E4
|
||||
S1
|
||||
F40
|
||||
W3
|
||||
F52
|
||||
L90
|
||||
F76
|
||||
N4
|
||||
F15
|
||||
S2
|
||||
F22
|
||||
S5
|
||||
L180
|
||||
F91
|
||||
L180
|
||||
F8
|
||||
L90
|
||||
E4
|
||||
N4
|
||||
F67
|
||||
L90
|
||||
S3
|
||||
R180
|
||||
R90
|
||||
N4
|
||||
F71
|
||||
W3
|
||||
F34
|
||||
E2
|
||||
N1
|
||||
F43
|
||||
W5
|
||||
L180
|
||||
N5
|
||||
W2
|
||||
F42
|
||||
R90
|
||||
W3
|
||||
F39
|
||||
E1
|
||||
S2
|
||||
L180
|
||||
N5
|
||||
E3
|
||||
N5
|
||||
F28
|
||||
E1
|
||||
R90
|
||||
S3
|
||||
F40
|
||||
L90
|
||||
S2
|
||||
S2
|
||||
L90
|
||||
W5
|
||||
L90
|
||||
F93
|
||||
R180
|
||||
W4
|
||||
S4
|
||||
W4
|
||||
F100
|
||||
S3
|
||||
R90
|
||||
E2
|
||||
L180
|
||||
W1
|
||||
E3
|
||||
S5
|
||||
L90
|
||||
F87
|
||||
N1
|
||||
R90
|
||||
F3
|
||||
R90
|
||||
E5
|
||||
R90
|
||||
S3
|
||||
F45
|
||||
L90
|
||||
S2
|
||||
F42
|
||||
R90
|
||||
F95
|
||||
L90
|
||||
E1
|
||||
N3
|
||||
R90
|
||||
F73
|
||||
S3
|
||||
E1
|
||||
L90
|
||||
S2
|
||||
E3
|
||||
L90
|
||||
L270
|
||||
F38
|
||||
S5
|
||||
R90
|
||||
F42
|
||||
L90
|
||||
N1
|
||||
F7
|
||||
S3
|
||||
F65
|
||||
N2
|
||||
F42
|
||||
L180
|
||||
W5
|
||||
S4
|
||||
E4
|
||||
F65
|
||||
S4
|
||||
E5
|
||||
F51
|
||||
E4
|
||||
R180
|
||||
F70
|
||||
R90
|
||||
F28
|
||||
N5
|
||||
W5
|
||||
N1
|
||||
F96
|
||||
L90
|
||||
W4
|
||||
S3
|
||||
W3
|
||||
F89
|
||||
W1
|
||||
L90
|
||||
F75
|
||||
L270
|
||||
S3
|
||||
R90
|
||||
L90
|
||||
F7
|
||||
E2
|
||||
F24
|
||||
R180
|
||||
S2
|
||||
L180
|
||||
F48
|
||||
R90
|
||||
F37
|
||||
W2
|
||||
R90
|
||||
W4
|
||||
L90
|
||||
W3
|
||||
F81
|
||||
E4
|
||||
N2
|
||||
F39
|
||||
E4
|
||||
N1
|
||||
W1
|
||||
L90
|
||||
F59
|
5
2020/day12/test
Normal file
5
2020/day12/test
Normal file
|
@ -0,0 +1,5 @@
|
|||
F10
|
||||
N3
|
||||
F7
|
||||
R90
|
||||
F11
|
Loading…
Add table
Add a link
Reference in a new issue