#!/usr/local/bin/chicken-csi -ss (import (chicken io) (chicken string)) ;; part 1: find full overlaps between pairs ;; that means just count where for a-b,x-y a= b y)) (and (<= x a) (>= y b))))) ; part 2: no overlap: a > y or x > b (define (any-overlap? pair) (let ((a (caar pair)) (b (cadar pair)) (x (caadr pair)) (y (cadadr pair))) (not (or (> a y) (> x b))))) (define (parse-line l) (let* ((ranges (string-split l ",")) (pair (map (lambda (r) (map (lambda (x) (string->number x)) (string-split r "-"))) ranges))) pair)) (define (filter pred lst) (cond ((null? lst) '()) ((pred (car lst)) (cons (car lst) (filter pred (cdr lst)))) (else (filter pred (cdr lst))))) (define (part-1 pairs) (length (filter overlaps? pairs))) (define (part-2 pairs) (length (filter any-overlap? pairs))) (define (main args) (let* ((lines (read-lines)) (pairs (map parse-line lines))) (print (part-1 pairs)) (print (part-2 pairs))))