summary refs log tree commit diff stats
path: root/day1.scm
blob: ca79a09b2ae0e0aab253313f9cf6b97acbdb6ee8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env gosh
(use file.util)

(define (read-input filename)
  (map string->number (file->string-list filename)))

(define (part1 input)
  (fold + 0 (map (lambda (x) (- (floor (/ x 3)) 2)) input)))

(define (calculate-fuel weight acc)
  (if (< weight 9)
    acc
    (let ((fuel-weight (- (floor (/ weight 3)) 2)))
      (calculate-fuel fuel-weight (+ acc fuel-weight)))))

(define (part2 input)
  (fold + 0 (map (lambda (x) (calculate-fuel x 0)) input)))

(define (main args)
  (let ((input (read-input "inputs/day1.txt")))
    (print (part1 input))
    (print (part2 input))))