add days 1 and 2

This commit is contained in:
Sijmen 2021-12-09 20:04:48 +01:00
parent 1a9a998460
commit c9bf66960a
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
3 changed files with 87 additions and 9 deletions

View file

@ -1,11 +1,11 @@
# Advent of Code 2021 # Advent of Code 2021
| Day | Chicken Scheme | Scala | Python | Nim | | Day | CHICKEN | Scala | Python | Nim |
|-----|----------------|--------|--------|-----| |-----|---------|--------|--------|-----|
| 1 | ✅ (to commit) | | | | | 1 | ✅ | | | |
| 2 | ✅ (to commit) | | | | | 2 | ✅ | | | |
| 3 | part 1 | part 1 | ✅ | | | 3 | part 1 | part 1 | ✅ | |
| 4 | | | ✅ | | | 4 | | | ✅ | |
| 5 | | | ✅ | | | 5 | | | ✅ | |
| 6 | | | | ✅ | | 6 | | | | ✅ |
| 7 | ✅ | | | | | 7 | ✅ | | | |

30
day01.scm Executable file
View file

@ -0,0 +1,30 @@
(import (chicken io))
(import (chicken format))
(import srfi-1)
(define (parse inputs)
(let ((line (read-line)))
(if (not (eof-object? line))
(parse (cons (string->number line) inputs))
inputs)))
(define (inc-if cond num)
(if cond (+ num 1) num))
(define (part1 inputs previous counter)
(if (not (null? inputs))
(let ((head (car inputs)))
(part1 (cdr inputs) head (inc-if (<= head previous) counter)))
counter))
(define (part2 inputs previous counter)
(if (not (eq? inputs '()))
(let ((sum (apply + (take-right! inputs 3))))
(part2 (cdr inputs) sum (inc-if (<= sum previous) counter)))
counter))
(let ((inputs (parse '())))
(format #t "inputs = ~A~%" inputs)
(format #t "part1 = ~A~%" (part1 inputs 0 0))
(format #t "part2 = ~A~%" (part2 inputs 0 0)))

48
day02.scm Executable file
View file

@ -0,0 +1,48 @@
(import (chicken io))
(import (chicken format))
(import (chicken string))
(import (chicken condition))
(import srfi-1)
(define (parse-line line)
(let-values (((command offset) (apply values (string-split line))))
(list command (string->number offset))))
(define (parse inputs)
(map parse-line (read-lines)))
(define (part1-direction head tail h d)
(let-values (((command offset) (apply values head)))
(cond ((equal? command "forward")
(part1 tail (+ h offset) d))
((equal? command "down")
(part1 tail h (+ d offset)))
((equal? command "up")
(part1 tail h (- d offset)))
(else (abort (format "did not expect command '~A'" command))))))
(define (part1 directions h d)
(if (not (null? directions))
(part1-direction (car directions) (cdr directions) h d)
(* h d)))
(define (part2-direction head tail horiz depth aim)
(let-values (((command offset) (apply values head)))
(cond ((equal? (car command) "forward")
(part2 tail (+ horiz offset) (+ depth (* aim offset)) aim))
((equal? command "down")
(part2 tail horiz depth (+ aim offset)))
((equal? command "up")
(part2 tail horiz depth (- aim offset)))
(else (abort (format "did not expect command '~A'" command))))))
(define (part2 directions horiz depth aim)
(if (not (null? directions))
(part2-direction (car directions) (cdr directions) horiz depth aim)
(* horiz depth)))
(let ((directions (parse '())))
(format #t "part1 = ~A~%" (part1 directions 0 0))
(format #t "part2 = ~A~%" (part2 directions 0 0 0)))