Initial commit
This commit is contained in:
commit
f242d2b0df
420 changed files with 62521 additions and 0 deletions
30
2021/day02/day02.scm
Normal file
30
2021/day02/day02.scm
Normal 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)))
|
Loading…
Add table
Add a link
Reference in a new issue