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

79
2020/day03/day03.c Normal file
View file

@ -0,0 +1,79 @@
#include <stdbool.h>
#include <stdio.h>
int count_lines(FILE *fp) {
int count = 0;
for (char c = getc(fp); c != EOF; c = getc(fp)) {
if (c == '\n') {
count++;
}
}
return count;
}
int line_length(FILE *fp) {
int count = 0;
for (char c = getc(fp); c != '\n'; c = getc(fp)) {
count++;
}
return count;
}
int count_trees(int n, int m, int grid[n][m], int down_offset, int right_offset) {
int count = 0;
for (int i = 0; i < n / down_offset; ++i) {
count += grid[i * down_offset][i * right_offset % m];
}
return count;
}
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 n = count_lines(fp);
rewind(fp);
int m = line_length(fp);
rewind(fp);
printf("Grid size: %d×%d\n", n, m);
int grid[n][m];
char c;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
c = getc(fp);
if (c == '\n' || c == EOF) {
c = getc(fp);
}
grid[i][j] = (c == '#');
}
}
fclose(fp);
printf("%d\n", count_trees(n, m, grid, 1, 3));
int total = 1;
int down_offsets[] = {1, 1, 1, 1, 2};
int right_offsets[] = {1, 3, 5, 7, 1};
for (int i = 0; i < 5; ++i) {
total *= count_trees(n, m, grid, down_offsets[i], right_offsets[i]);
}
printf("%d\n", total);
return 0;
}

5
2020/day03/day03.dyalog Normal file
View file

@ -0,0 +1,5 @@
⎕io←0
p←'#'=↑⊃⎕NGET'input'1
f←{+/p[↓⍉↑(p)|⍵×2⌈(⊃p)÷⊃⍵]}
f 1 3 ⍝ Part 1
×/f¨(1 1)(1 3)(1 5)(1 7)(2 1) ⍝ Part 2

52
2020/day03/day03.rkt Normal file
View file

@ -0,0 +1,52 @@
#lang racket
(module+ test
(require rackunit))
(module+ main
(displayln "Day 3"))
(define (parse-line str)
(list->vector
(map (λ (c) (if (equal? c #\#) 1 0)) (string->list str))))
(define (read-input filename)
(with-input-from-file filename
(lambda ()
(for/vector ([line (in-lines)])
(parse-line line)))))
(define (traj grid down right)
(define height (vector-length grid))
(define width (vector-length (vector-ref grid 0)))
(for/list ([line (in-vector grid 0 #f down)]
[j (in-range 0 (* width height right) right)])
(vector-ref line (remainder j width))))
(define (part1 filename)
(define grid (read-input filename))
(apply + (traj grid 1 3)))
(module+ test
(check-equal? (part1 "test") 7))
(module+ main
(displayln (part1 "input")))
(define slopes
'((1 1)
(1 3)
(1 5)
(1 7)
(2 1)))
(define (part2 filename)
(define grid (read-input filename))
(for/product ([slope slopes])
(apply + (traj grid (car slope) (cadr slope)))))
(module+ test
(check-equal? (part2 "test") 336))
(module+ main
(displayln (part2 "input")))

323
2020/day03/input Normal file
View file

@ -0,0 +1,323 @@
...........#..............##...
...#....................#......
.....####...........#.#..#.#...
....##.#.......................
.......#.##......#.###.........
.#.....#.......##.......#.....#
...........##....##.#....#.....
......#.........#....#.........
..###....#.........#....#.#....
....#....#.#..#..#.........#.#.
..........................#...#
.##...........#...#.#.......#..
#....##...#.#....#.............
....#..##......##..#.#....#....
#..........#.............#..#.#
...#.####.....#..#.#.#..#...#..
..........#......#........#..#.
............#.....#..#..#....#.
.................#...#.........
..#...#...................#....
..............##...#...........
..........................#..#.
#...#...#............#...#.....
.................#..##.......#.
............#....#.............
.#......#.#...#....#...#.......
.....#.....##..##.....#.......#
.#..#..##...............#..#...
#...#...##............#........
.......#....#.......#..........
...............................
#................#...#.........
...#...#..#..#.............##.#
......#........#..............#
...#.....##.#...#...#..........
.........#..#........##.#...##.
#.........##..#.......#........
........##.#.#.................
.#....#............###....#....
...#.##....#.....##..#..#....#.
....#..#........##..#...#..##..
..........#............#.......
.........#........##....#..##..
#....#.........#.#.......#..#..
...#....#......##.#............
........#..#...............#...
..............#.....#........#.
......#..#.#........#..#..#.##.
..#........###....#.#..........
...#..#...#.#....##..#........#
........#..#..............#....
#.####.................#....##.
.#................#............
....#....#....#................
#......#........##....#...#....
......#..##..#..###...#.#.#....
.#..........##.................
...#...#....#...#.....#.....#..
............#......##.........#
..............##...............
##....#....#...#...#....#..###.
...................#.......##..
#.....##........#....#.........
...#.......#...........#.......
...............##..............
##.......#......#.....#........
#....#..#..##..#.......#..#..#.
.....#.............#.......#...
......#..#........#.......#.#..
..#...#...........#.##.........
..#................####.#..#...
......##....#.........#........
..#..#.......#...##....#......#
#.#..........#..............#.#
.#.#..............#.##...#.....
................#.....#.#......
##.........#.........#.....#...
....#.#.....................#..
..#..#..#........#.......#.....
.....#..#.#....#....#.....#....
..####....#.#.........#........
#..##...##..#.#............#..#
.#........#..##.#.....#......##
.##.##.....##....#.#...........
....#..#.#..##............#.#..
........#.#...#....#.........#.
.....#.#.#.....#....#.....##...
#...#..#....##..#..............
..#...#....#...##..#.......#...
.#....##.......................
.........#............##.#..#..
....#................#...#.#...
...................#..#...#....
#..#...................#.......
..##..............#..........##
...#.##......#.............#...
.........#.#.........#.........
...###......#.................#
..........#....##..............
.##..#....#.........#.#........
.........#.......#.......#.#...
#........#............#......#.
....................#..........
.......#...##..........#...#...
....#.#.......#.#...##..#.#....
...#..........#..............#.
........##..............#......
......#...##......#....##......
....#.....#.#.##..............#
...#...........#.#.............
...........#......#.#..........
...#.#......#......#...#...#...
..#.......................#....
...#...#..#..................#.
##.....#.....#..#..#.....#...#.
.#..#.......##.#.#.............
......##.......##............#.
.......#..#..#.......#....#.#..
......#.....##..##...#........#
.....#........#.##..........#..
#....##............#........#..
.....#..#...#............#...#.
##.#....#........#.............
.##...............##......#.#..
###..#..#.......#.#..........#.
.....#...........#...##........
..#.#.#.........#.....#....#...
.....#....##.......#..#.#......
......#.....#...#..#...##..#...
.....#....#................#...
......#....#.#...##......##.#.#
.....###.............#.........
.................#......#####..
.#.......#..........#.#....##..
..#..#.......#.....#..#......##
..........#.#.##.......##....#.
##...#...##.##......#..###.....
..#..#..#......#....#..........
..#...#....#......#....#....#.#
.#...#........#.....#......#..#
#.........#......#.##.##.......
#.##..#.............#.....#....
....#.......#..#..##...##......
...#.............#.#......#....
#.....#..........##...##.....#.
...............#........#....#.
#.....#...#..#.............##..
.#....##.#.......#.#..........#
....#....#.#.....#....#......#.
......#......#.................
.#.#..#.#.#...#...#..#.##.#..##
.............#.....#...........
............#...#..#..#.....#..
.#..........#.......#....#.....
......#..###.#...#.............
......#..........#.............
....#.................#..#.#.#.
...##.##.#....##.##............
####......#........###......#..
..#.......#.#..#.##............
.....#.....#.#.......#.....#...
.....#..........#.#............
#.....#.............#......##..
......##..........##....#......
.#..............#..........#...
......#..#...#........#..#....#
.#......#.......#..#...........
..#..#....#.#.......#....##..#.
........#.#................#...
#.......#.##.#......#...#.....#
..#...#.#.....##...............
..........#.....##.............
.......#............#........#.
...#............#......#......#
.#..#.......#...#...#..#..#....
#....#.#...#......#...#......#.
.#.......#..#.#...........#....
...##.#...#.......#..........#.
.....#..............#..#...#...
...........................#...
.............#.....#...........
....#.#..#..#...#..#...........
.....#.#.#..#.#....#.#.#.......
.......#..............#.....##.
........#..#..#.#..#...#.#.....
.....#.#...#.#.#.....#..#...#..
.....#....#.......#......#.#...
.#.#...........#........#......
.##..##......#......#......#.#.
.....#.###.#.......##.#..#.....
#.......##..#.........#....#...
.#.............#.........#.#.#.
..........#..#..#....#....#....
#....#...........##..#.....#..#
......#....#...###..#...#......
.....#....#........#....#..#...
...##..............#.##...#....
.#............#........##......
..##........#.#...........#...#
..#.#...##...#..#..........##..
.................#.......#.....
......#.....#............#.....
.#.....#.........#.#..#.#......
.............#.#.#..#.......#.#
#......#.....#..##...#.......#.
.......#.#..#...#.........#....
...#..##...#.........#.#....#..
........................#..#...
....##..##................#....
.......#..#.......#........##..
.....#....#.##....#............
.#....#............#.....#...#.
..##.....#......#......#.#....#
...#...........#...##....#.....
......#.##.#..##...##.#.#..##..
.......##....#......#....#.#...
.....####..#............#..##..
......##..##..##.........#...#.
.#.#...............#.........#.
......#......#...........#.....
.....#.......##.....#..#.......
.....##..#..#....#.#.......#...
...........###.###.##..#.#..#..
.#...............##.........#..
......##..........#..#.....##.#
.............#....#....#..##...
.#..............#........#.....
.#..#.........................#
.##..............#..........#..
..#..#.#.#.#......#............
....#...#.#.#....#........#..#.
.....#........#....#.....#.....
.#...#.#......#..#........#.##.
.......#.....#................#
.#.#........................#..
............#..#.......#.......
....##.#........#...#.#.#.#.#..
.....#.......##................
...##...#....#.....#.#.........
#...#..............#.......#...
...#.#.#.#..##....##...........
.....##...#....#.....#.........
#......#.....#....#............
....#..###....#.##.......#...#.
..................##.#......#..
.....##..............##.#....#.
.........#...#........#..#....#
.##..#.........#....#..##...#..
#.#.##................#.##.....
..#.#....#.#.......#....#......
..#.#.##.#.......#.............
..#....#.#..##.#..........#.#..
#.....#.....#.....#.........#..
#.......##.....#....##.....#...
..#...#.........##.#..##.......
..#.#.........#.......#........
#.....#.....##.#.#..#...#..#.##
.........................#.##..
..#.#..#..#..#........#......#.
..#..............#.............
.....#.......##.##.....#.......
....#...#...............#..#...
....#......#.#........##.#..#.#
....................#..#.......
.....#.......#......#.##.......
#.......##..........#.....#....
.#.......#....#.#......#.......
......#...#...............#.##.
....##.#.....#.............#.##
#..#................##...#.....
....###......#.#.........#..#..
...#...#......#...##....#...#.#
..#...#.#.##.#.................
.....##......#..#.#....#.......
##.......#......#.#..#.#.......
.#.#.#.........#...#.#..#......
#...#.#........#....#.#.....#..
....#.......##....#......##....
.....#..........#......#....#..
#...#....#...#.....#.#.........
...#..##.....##....#.....#.#...
..................#.....##.....
.....#............#............
...#.....#..#........#.#..##...
.......#.#.....................
......#...#.......#..#...#..#..
.#..#...#.....##.....#.#.#....#
....##...#.#............#..#..#
...........#............#..#...
.......#.....#................#
..#......#.#.......#.#.........
.....#..#.#.##.................
.....#..#......................
...#....#...#..#.#..#....#.....
.#............#.....#..........
#.##..#..#.......#......#.....#
.#.........#....#....#.........
...#.#.#........#.#....#...#...
#........#..#..#..........#..#.
.....#..#.....##......##..##.#.
..............#.......#..#..#..
....#........#.##.#...#........
..#.#..#....#........##.....##.
...##.....#...#.......#.#....#.
#.....#..##.##.#...##.......#..
.....#........#.#.#....#.......
.#................#####..#.#...
..........##..#..###....#......
.....#.......#..........#..#...
..#....#....................#..
#.....#..#.....#...##.#.....#.#
...#..##............#.....#....
##.#..#.......##...............
........##...#.#.....#......#..
........#...#..................
#......#................#.#....
...........#...#..#.........#..
...#.##..#.##..................
.....#......###......#..#......
..#.#.....#...#..#.##........#.
....#..........#.#.....#.......
..#..........#..........#.#....
..#.##.......#......#..........

11
2020/day03/test Normal file
View file

@ -0,0 +1,11 @@
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#