diff options
author | lamp | 2023-12-04 18:36:47 +0000 |
---|---|---|
committer | lamp | 2023-12-04 18:36:47 +0000 |
commit | 843d10f9a8ff2f1d9b22acae18f0ee2ae0c8d9fd (patch) | |
tree | 248d8150d521b65455e6b260cf580d1d4a4bbf4c /4.scm | |
parent | 8714ae3ac6eb5c77fdf5103685a926e3b482612b (diff) |
add day 4 solutions
Diffstat (limited to '4.scm')
-rwxr-xr-x | 4.scm | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -0,0 +1,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)))) |