advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

2.scm (721B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
(import (chicken io)
	(chicken string))

(define txtlines (call-with-input-file "input" read-lines))
(define splitlines (map (lambda (s) (string-split s " ")) txtlines))

(define aim 0)
(define (getvec-line splitline)
  (cond
    ((string=? (car splitline) "forward")
     (let ((p (string->number (cadr splitline)))) (list p (* p aim))))
    ((string=? (car splitline) "down") (set! aim (+ aim (string->number (cadr splitline)))) (list 0 0))
    ((string=? (car splitline) "up") (set! aim (+ aim (* -1 (string->number (cadr splitline))))) (list 0 0))
    (else (list 0 0))))

(define (move lines)
  (if (null? lines) '(0 0)
    (map + (getvec-line (car lines)) (move (cdr lines)))))

(print (apply * (move splitlines)))