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

58
2020/day06/day06.c Normal file
View file

@ -0,0 +1,58 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
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;
}
bool flag_newline = false;
unsigned int answer_count = 0;
unsigned int counts[26] = {0};
unsigned int part1 = 0;
unsigned int part2 = 0;
for (char c = getc(fp); c != EOF; c = getc(fp)) {
if (c == '\n' && flag_newline) {
for (int i = 0; i < 26; ++i) {
if (counts[i] > 0) {
part1++;
}
if (counts[i] == answer_count) {
part2++;
}
counts[i] = 0;
}
flag_newline = false;
answer_count = 0;
} else if (c == '\n') {
flag_newline = true;
answer_count++;
} else {
counts[c - 'a']++;
flag_newline = false;
}
}
for (int i = 0; i < 26; ++i) {
if (counts[i] > 0) {
part1++;
}
if (counts[i] == answer_count) {
part2++;
}
}
printf("%u\n", part1);
printf("%u\n", part2);
fclose(fp);
return 0;
}

4
2020/day06/day06.dyalog Normal file
View file

@ -0,0 +1,4 @@
p←⊃⎕NGET'input'1
p←(~p∊⊂'')⊆p
+/{≢⊃∪/⍵}¨p ⍝ Part 1
+/{≢⊃∩/⍵}¨p ⍝ Part 2

37
2020/day06/day06.rkt Normal file
View file

@ -0,0 +1,37 @@
#lang racket
(module+ test
(require rackunit))
(module+ main
(displayln "Day 6"))
(define (read-input filename)
(string-split (file->string filename) "\n\n"))
(define (count-unique str)
(set-count (set-remove (list->set (string->list str)) #\newline)))
(define (part1 filename)
(apply + (map count-unique (read-input filename))))
(module+ test
(check-equal? (part1 "test") 11))
(module+ main
(displayln (part1 "input")))
(define (count-intersection str)
(define answers (string-split str "\n"))
(set-count
(apply set-intersect
(map (compose list->set string->list) answers))))
(define (part2 filename)
(apply + (map count-intersection (read-input filename))))
(module+ test
(check-equal? (part2 "test") 6))
(module+ main
(displayln (part2 "input")))

2232
2020/day06/input Normal file

File diff suppressed because it is too large Load diff

15
2020/day06/test Normal file
View file

@ -0,0 +1,15 @@
abc
a
b
c
ab
ac
a
a
a
a
b