summaryrefslogtreecommitdiff
path: root/4.scm
blob: 668289ab4fb1306517f8732a992d23d2e541a001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env -S guile -s
!#
(use-modules (ice-9 regex)
             (ice-9 textual-ports)
             (srfi srfi-1))

(define (read-lines port)
  (letrec ((loop (lambda (l ls)
                   (if (eof-object? l)
                       ls
                       (loop (get-line port) (cons l ls))))))
    (reverse (loop (get-line port) '()))))

(define (split-up nums)
  (filter (lambda (x) (not (string-null? x))) (string-split nums char-set:whitespace)))

(define (score line)
  (let* ((m (string-match ": ([0-9| ]+) \\| ([0-9| ]+)" line))
         (winning (split-up (match:substring m 1)))
         (have (split-up (match:substring m 2)))
         (shared (filter (lambda (x) (member x winning)) have)))
    (if (null? shared) 0 (expt 2 (- (length shared) 1)))))

(let* ((port (open-input-file "4.txt"))
       (lines (read-lines port)))
  (close-port port)
  (display (fold + 0 (map score lines))))