Initial commit

This commit is contained in:
Dimitri Lozeve 2024-11-12 21:43:32 +01:00
commit f242d2b0df
420 changed files with 62521 additions and 0 deletions

34
2020/day15/day15.c Normal file
View file

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30000000
size_t a[SIZE];
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <input list>\n", argv[0]);
return 0;
}
for (size_t i = 1; i < argc - 1; ++i) {
a[atoi(argv[i])] = i;
}
size_t n = atoi(argv[argc - 1]);
for (size_t i = argc - 1; i < SIZE; ++i) {
if (i == 2020) { // Part 1
printf("%zu\n", n);
}
size_t prev_n = n;
if (a[n] == 0) {
n = 0;
} else {
n = i - a[n];
}
a[prev_n] = i;
}
printf("%zu\n", n); // Part 2
return 0;
}

39
2020/day15/day15.rkt Normal file
View file

@ -0,0 +1,39 @@
#lang racket
(module+ test
(require rackunit))
(define (run-game input idx)
(define h (make-hash))
(for ([n (in-list (drop-right input 1))]
[i (in-naturals)])
(hash-set! h n (add1 i)))
(for/fold ([n (last input)])
([i (in-range (length input) idx)])
(define res (- i (hash-ref h n i)))
(hash-set! h n i)
res))
(module+ test
(check-equal? (run-game '(0 3 6) 2020) 436)
(check-equal? (run-game '(1 3 2) 2020) 1)
(check-equal? (run-game '(2 1 3) 2020) 10)
(check-equal? (run-game '(1 2 3) 2020) 27)
(check-equal? (run-game '(2 3 1) 2020) 78)
(check-equal? (run-game '(3 2 1) 2020) 438)
(check-equal? (run-game '(3 1 2) 2020) 1836))
(module+ main
(displayln (run-game '(14 1 17 0 3 20) 2020)))
(module+ test
(check-equal? (run-game '(0 3 6) 30000000) 175594)
(check-equal? (run-game '(1 3 2) 30000000) 2578)
(check-equal? (run-game '(2 1 3) 30000000) 3544142)
(check-equal? (run-game '(1 2 3) 30000000) 261214)
(check-equal? (run-game '(2 3 1) 30000000) 6895259)
(check-equal? (run-game '(3 2 1) 30000000) 18)
(check-equal? (run-game '(3 1 2) 30000000) 362))
(module+ main
(displayln (run-game '(14 1 17 0 3 20) 30000000)))