Skip to content

Instantly share code, notes, and snippets.

@Alwinfy
Created December 12, 2023 07:52
Show Gist options
  • Save Alwinfy/63eebee21e076159decb554a99df4b55 to your computer and use it in GitHub Desktop.
Save Alwinfy/63eebee21e076159decb554a99df4b55 to your computer and use it in GitHub Desktop.
(define (slurp f . args)
(let loop ()
(define head (apply f args))
(if (eof-object? head)
'()
(cons head (loop)))))
(define (classify ch)
(case ch
[(#\#) #t]
[(#\.) #f]
[(#\?) 'unknown]))
(define (truthy? ch) (or (eq? 'unknown ch) ch))
(define (falsy? ch) (or (eq? 'unknown ch) (not ch)))
(define (copies n x) (if (zero? n) '() (cons x (copies (sub1 n) x))))
(define (repeat n ls)
(if (zero? n)
'()
(append ls (repeat (sub1 n) ls))))
(define (parse-line line)
(let ([data (string-split line " ")])
(list
(map classify (string->list (first data)))
(map string->number (string-split (second data) ",")))))
(define (search data runs)
(caar
(let loop ([data (append data '(#f))])
(if (null? data)
(append (map (const '(0)) runs) '((1)))
(let ([past-data (loop (cdr data))])
(map
(lambda (ts fs past)
(cons (+ ts fs) past))
(append
(map
(lambda (d pd)
(let loop ((data data) (d d) (pd pd))
(cond
[(null? data) 0]
[(zero? d)
(if (falsy? (car data))
(car pd)
0)]
[(truthy? (car data))
(loop (cdr data) (sub1 d) (cdr pd))]
[else 0])))
runs
(cdr past-data))
'(0))
(if (falsy? (car data))
(map car past-data)
(copies (+ 1 (length runs)) 0))
past-data))))))
(define ((dupe n) data runs)
(list
(cdr (repeat n (cons 'unknown data)))
(repeat n runs)))
(define data (map parse-line (slurp read-line (open-input-file "day12.in"))))
(println (apply + (map (curry apply search) data)))
(define data2 (map (curry apply (dupe 5)) data))
(println (apply + (map (curry apply search) data2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment