Skip to content

Instantly share code, notes, and snippets.

@vijfhoek
Created December 3, 2021 00:10
Show Gist options
  • Save vijfhoek/cdad979f4e3f74100d866919974e7552 to your computer and use it in GitHub Desktop.
Save vijfhoek/cdad979f4e3f74100d866919974e7552 to your computer and use it in GitHub Desktop.
(import (chicken io))
(import (chicken format))
(import (chicken string))
(import srfi-1)
(define (parse-line line)
(let ((words (string-split line)))
(list (car words) (string->number (car (cdr words))))))
(define (parse inputs)
(let ((line (read-line)))
(if (not (eof-object? line))
(cons (parse-line line) (parse inputs))
inputs)))
(define (part1-direction head tail h d)
(let ((command (car head))
(offset (car (cdr 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 (format #t "que~%")))))
(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 ((command (car head))
(offset (car (cdr head))))
(cond ((equal? 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 (format #t "que~%")))))
(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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment