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

6
2021/day02/day02.awk Normal file
View file

@ -0,0 +1,6 @@
/down/ {aim += $2}
/up/ {aim -= $2}
/forward/ {pos += $2; depth += aim * $2}
END {print pos * aim, pos * depth}
# one-liner
# /^d/{a+=$2}/u/{a-=$2}/f/{p+=$2;d+=a*$2}END{print p*a,p*d}

32
2021/day02/day02.c Normal file
View file

@ -0,0 +1,32 @@
#include <stdio.h>
int main() {
char instr[9] = {0};
int n = 0;
int pos = 0;
int depth = 0;
int aim = 0;
while (scanf("%s %d\n", instr, &n) == 2) {
switch (instr[0]) {
case 'd':
aim += n;
break;
case 'u':
aim -= n;
break;
case 'f':
pos += n;
depth += aim * n;
break;
default:
fprintf(stderr, "unknown instruction: %s", instr);
break;
}
}
printf("%d\n", pos * aim);
printf("%d\n", pos * depth);
return 0;
}

30
2021/day02/day02.scm Normal file
View file

@ -0,0 +1,30 @@
(import (chicken io)
srfi-1
srfi-152
matchable)
(define (read-input #!optional (port (current-input-port)))
(map (lambda (s) (let ((l (string-split s " ")))
(list (string->symbol (car l))
(string->number (cadr l)))))
(read-lines port)))
(define (move steps pos depth aim)
(match steps
(() (values pos depth aim))
((('down n) . rest) (move rest pos depth (+ aim n)))
((('up n) . rest) (move rest pos depth (- aim n)))
((('forward n) . rest) (move rest (+ pos n) (+ depth (* aim n)) aim))
(_ (error "unknown instruction" (car steps)))))
(define (part1 l)
(let-values (((pos depth aim) (move l 0 0 0)))
(* pos aim)))
(define (part2 l)
(let-values (((pos depth aim) (move l 0 0 0)))
(* pos depth)))
(let ((in (read-input)))
(print (part1 in))
(print (part2 in)))

1
2021/day02/day02_golf.c Normal file
View file

@ -0,0 +1 @@
c,n,p,d,a;main(){while(scanf("%c%*s%d\n",&c,&n)>1)c^'d'?a+=n:c^'f'?p+=n,d+=a*n:a-=n;printf("%d %d",p*a,p*d);}

1000
2021/day02/input Normal file

File diff suppressed because it is too large Load diff