Initial commit
This commit is contained in:
commit
f242d2b0df
420 changed files with 62521 additions and 0 deletions
63
2020/day10/day10.c
Normal file
63
2020/day10/day10.c
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
int compare(const void* a, const void* b) {
|
||||
unsigned int x = *(const unsigned int*)a;
|
||||
unsigned int y = *(const unsigned int*)b;
|
||||
|
||||
if (x < y) return -1;
|
||||
if (x > y) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
unsigned int input[128] = {0};
|
||||
unsigned int n = 1;
|
||||
unsigned int d = 0;
|
||||
unsigned int max = 0;
|
||||
while (fscanf(fp, "%d\n", &d) == 1) {
|
||||
input[n] = d;
|
||||
n++;
|
||||
max = d > max ? d : max;
|
||||
}
|
||||
max += 3;
|
||||
input[n] = max;
|
||||
n++;
|
||||
fclose(fp);
|
||||
|
||||
qsort(input, n, sizeof(unsigned int), compare);
|
||||
|
||||
// Part 1
|
||||
int ones = 0;
|
||||
int threes = 0;
|
||||
for (int i = 1; i < n; ++i) {
|
||||
int diff = input[i] - input[i-1];
|
||||
ones += (diff == 1);
|
||||
threes += (diff == 3);
|
||||
}
|
||||
printf("%d\n", ones * threes);
|
||||
|
||||
// Part 2
|
||||
unsigned long counts[1024] = {0};
|
||||
counts[2] = 1;
|
||||
for (int i = 1; i < n; ++i) {
|
||||
unsigned int x = input[i];
|
||||
counts[x+2] = counts[x-1] + counts[x] + counts[x+1];
|
||||
}
|
||||
printf("%lu\n", counts[max+2]);
|
||||
|
||||
return 0;
|
||||
}
|
10
2020/day10/day10.dyalog
Normal file
10
2020/day10/day10.dyalog
Normal file
|
@ -0,0 +1,10 @@
|
|||
⎕IO←0 ⋄ ⎕PP←15
|
||||
p←⍎¨⊃⎕NGET'input'1
|
||||
p←0,p[⍋p],3+⌈/p
|
||||
×/⊢∘≢⌸2-/p ⍝ Part 1
|
||||
c←0,0,1,(⌈/p)⍴0 ⍝ paths counts
|
||||
{c[⍵+2]←+/c[⍵+¯1 0 1]}¨1↓p
|
||||
⊃⌽c ⍝ Part 2
|
||||
|
||||
a←(0∘<∧≤∘3)∘.-⍨p ⍝ adjacency matrix
|
||||
+/{⊃⊖({a+.×⍵}⍣⍵)a}¨⍳⍴p ⍝ Part 2, the matrix way
|
49
2020/day10/day10.rkt
Normal file
49
2020/day10/day10.rkt
Normal file
|
@ -0,0 +1,49 @@
|
|||
#lang racket
|
||||
|
||||
(module+ test
|
||||
(require rackunit))
|
||||
|
||||
(module+ main
|
||||
(displayln "Day 10"))
|
||||
|
||||
(define (read-input filename)
|
||||
(define input (map string->number (file->lines filename)))
|
||||
(sort (cons 0 (cons (target input) input)) <))
|
||||
|
||||
(define (target lst)
|
||||
(+ 3 (apply max lst)))
|
||||
|
||||
(define (part1 filename)
|
||||
(define jolts (read-input filename))
|
||||
(define counts (make-hash))
|
||||
(for ([x (in-list (drop jolts 1))]
|
||||
[y (in-list jolts)])
|
||||
(hash-update! counts (- x y) add1 0))
|
||||
(* (hash-ref counts 1 0) (hash-ref counts 3 0)))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (part1 "test1") (* 7 5))
|
||||
(check-equal? (part1 "test2") (* 22 10)))
|
||||
|
||||
(module+ main
|
||||
(displayln (part1 "input")))
|
||||
|
||||
(define (count-paths jolts)
|
||||
(define counts (make-hash))
|
||||
(hash-set! counts 0 1)
|
||||
(for ([x (in-list (cdr jolts))])
|
||||
(hash-set! counts x (+ (hash-ref counts (- x 1) 0)
|
||||
(hash-ref counts (- x 2) 0)
|
||||
(hash-ref counts (- x 3) 0))))
|
||||
(hash-ref counts (last jolts)))
|
||||
|
||||
(define (part2 filename)
|
||||
(define jolts (read-input filename))
|
||||
(count-paths jolts))
|
||||
|
||||
(module+ test
|
||||
(check-equal? (part2 "test1") 8)
|
||||
(check-equal? (part2 "test2") 19208))
|
||||
|
||||
(module+ main
|
||||
(displayln (part2 "input")))
|
95
2020/day10/input
Normal file
95
2020/day10/input
Normal file
|
@ -0,0 +1,95 @@
|
|||
86
|
||||
149
|
||||
4
|
||||
75
|
||||
87
|
||||
132
|
||||
12
|
||||
115
|
||||
62
|
||||
61
|
||||
153
|
||||
78
|
||||
138
|
||||
43
|
||||
88
|
||||
108
|
||||
59
|
||||
152
|
||||
109
|
||||
63
|
||||
42
|
||||
60
|
||||
7
|
||||
104
|
||||
49
|
||||
156
|
||||
35
|
||||
2
|
||||
52
|
||||
72
|
||||
125
|
||||
94
|
||||
46
|
||||
136
|
||||
26
|
||||
16
|
||||
76
|
||||
117
|
||||
116
|
||||
150
|
||||
20
|
||||
13
|
||||
141
|
||||
131
|
||||
127
|
||||
67
|
||||
3
|
||||
40
|
||||
54
|
||||
82
|
||||
36
|
||||
100
|
||||
41
|
||||
56
|
||||
146
|
||||
157
|
||||
89
|
||||
23
|
||||
8
|
||||
55
|
||||
111
|
||||
135
|
||||
144
|
||||
77
|
||||
124
|
||||
18
|
||||
53
|
||||
92
|
||||
126
|
||||
101
|
||||
69
|
||||
27
|
||||
145
|
||||
11
|
||||
151
|
||||
31
|
||||
19
|
||||
34
|
||||
17
|
||||
130
|
||||
118
|
||||
28
|
||||
107
|
||||
137
|
||||
68
|
||||
93
|
||||
85
|
||||
66
|
||||
97
|
||||
110
|
||||
37
|
||||
114
|
||||
79
|
||||
121
|
||||
1
|
11
2020/day10/test1
Normal file
11
2020/day10/test1
Normal file
|
@ -0,0 +1,11 @@
|
|||
16
|
||||
10
|
||||
15
|
||||
5
|
||||
1
|
||||
11
|
||||
7
|
||||
19
|
||||
6
|
||||
12
|
||||
4
|
31
2020/day10/test2
Normal file
31
2020/day10/test2
Normal file
|
@ -0,0 +1,31 @@
|
|||
28
|
||||
33
|
||||
18
|
||||
42
|
||||
31
|
||||
14
|
||||
46
|
||||
20
|
||||
48
|
||||
47
|
||||
24
|
||||
23
|
||||
49
|
||||
45
|
||||
19
|
||||
38
|
||||
39
|
||||
11
|
||||
1
|
||||
32
|
||||
25
|
||||
35
|
||||
8
|
||||
17
|
||||
7
|
||||
9
|
||||
4
|
||||
2
|
||||
34
|
||||
10
|
||||
3
|
Loading…
Add table
Add a link
Reference in a new issue