Initial commit
This commit is contained in:
commit
f242d2b0df
420 changed files with 62521 additions and 0 deletions
89
2020/day13/day13.c
Normal file
89
2020/day13/day13.c
Normal file
|
@ -0,0 +1,89 @@
|
|||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
long egcd(long *x, long *y, long a, long b) {
|
||||
if (b == 0) {
|
||||
*x = 1;
|
||||
*y = 0;
|
||||
return a;
|
||||
}
|
||||
|
||||
long x1, y1;
|
||||
long gcd = egcd(&x1, &y1, b, a % b);
|
||||
*x = y1;
|
||||
*y = x1 - (a / b) * y1;
|
||||
|
||||
return gcd;
|
||||
}
|
||||
|
||||
long modinv(long a, long m) {
|
||||
long x, y;
|
||||
long gcd = egcd(&x, &y, a, m);
|
||||
if (gcd != 1) {
|
||||
return -1;
|
||||
}
|
||||
if (x < 0) {
|
||||
return m + x;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int t;
|
||||
fscanf(fp, "%d\n", &t);
|
||||
|
||||
int buses[100] = {0};
|
||||
int n = 0;
|
||||
while (fscanf(fp, "%d,", &buses[n++]) == 1 || fscanf(fp, "x,") == 0)
|
||||
;
|
||||
n--;
|
||||
fclose(fp);
|
||||
|
||||
// Part 1
|
||||
int min = INT_MAX;
|
||||
int tmin = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (buses[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
int x = buses[i] - t % buses[i];
|
||||
if (x < min) {
|
||||
min = x;
|
||||
tmin = i;
|
||||
}
|
||||
}
|
||||
printf("%d\n", min * buses[tmin]);
|
||||
|
||||
// Part 2
|
||||
unsigned long N = 1;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (buses[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
N *= buses[i];
|
||||
}
|
||||
|
||||
long x = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (buses[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
long Ni = N / buses[i];
|
||||
x += (buses[i] - i) * modinv(Ni, buses[i]) * Ni;
|
||||
}
|
||||
printf("%ld\n", x % N);
|
||||
|
||||
return 0;
|
||||
}
|
18
2020/day13/day13.dyalog
Normal file
18
2020/day13/day13.dyalog
Normal file
|
@ -0,0 +1,18 @@
|
|||
⎕IO←0 ⋄ (⎕FR ⎕PP)←1287 34
|
||||
p←⊃⎕NGET'input'1
|
||||
t←⍎⊃p
|
||||
b←⍎¨((1<',x'∘⍳)⊆⊢)1⊃p
|
||||
b{×/((⊢⍳⌊/)⍵)⌷¨(⊂⍺),⊂⍵}b|-t ⍝ Part 1
|
||||
⍝ https://www.jsoftware.com/papers/50/50_44.htm
|
||||
xea←{(⍺,1 0){0=⊃⍵:⍺ ⋄ ⍵ ∇ ⍺-⍵×⌊(⊃⍺)÷⊃⍵}(⍵,0 1)}
|
||||
∇r←larg cr rarg
|
||||
m r←larg
|
||||
n s←rarg
|
||||
gcd a b←m xea n
|
||||
lcm←m×n÷gcd
|
||||
c←lcm|gcd÷⍨(r×b×n)+(s×a×m)
|
||||
r←lcm,c
|
||||
∇
|
||||
c←','(≠⊆⊢)1⊃p
|
||||
b←↓⍉⍎¨⍕¨↑(⊂~('x'≡⊃)¨c)/¨(⊂c),⊂-⍳⍴c
|
||||
1⊃⊃cr/{x y←⍵ ⋄ 0=y:x,y ⋄ 1:x(x+y)}¨b ⍝ Part 2
|
42
2020/day13/day13.rkt
Normal file
42
2020/day13/day13.rkt
Normal file
|
@ -0,0 +1,42 @@
|
|||
#lang racket
|
||||
|
||||
(require math/number-theory)
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
(module+ main
|
||||
(displayln "Day 13"))
|
||||
|
||||
(define (parse-buses str)
|
||||
(map string->number (string-split str ",")))
|
||||
|
||||
(define (read-input filename)
|
||||
(define in (file->lines filename))
|
||||
(values (string->number (car in))
|
||||
(parse-buses (cadr in))))
|
||||
|
||||
(define (part1 filename)
|
||||
(define-values (t buses) (read-input filename))
|
||||
(define waits (for/list ([b (in-list buses)]
|
||||
#:when b)
|
||||
(list b (- b (modulo t b)))))
|
||||
(apply * (argmin cadr waits)))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (part1 "test") 295))
|
||||
|
||||
(module+ main
|
||||
(displayln (part1 "input")))
|
||||
|
||||
(define (part2 filename)
|
||||
(define-values (t buses) (read-input filename))
|
||||
(define active-buses (for/list ([b (in-list buses)] #:when b) b))
|
||||
(define times (for/list ([b (in-list buses)] [i (in-naturals)] #:when b) (- i)))
|
||||
(solve-chinese times active-buses))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (part2 "test") 1068781))
|
||||
|
||||
(module+ main
|
||||
(displayln (part2 "input")))
|
2
2020/day13/input
Normal file
2
2020/day13/input
Normal file
|
@ -0,0 +1,2 @@
|
|||
1000509
|
||||
17,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,739,x,29,x,x,x,x,x,x,x,x,x,x,13,x,x,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,971,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,19
|
2
2020/day13/test
Normal file
2
2020/day13/test
Normal file
|
@ -0,0 +1,2 @@
|
|||
939
|
||||
7,13,x,x,59,x,31,19
|
Loading…
Add table
Add a link
Reference in a new issue