commit 00daf031374f5b3671df643f81d0506c5fc02278 (patch)
parent 8b19bd2b4aa8fbc54f7a34e766a9f5dfbee81323
Author: Alex Karle <alex@alexkarle.com>
Date: Tue, 13 Dec 2022 23:21:54 -0500
day13: Add part 2
Diffstat:
1 file changed, 26 insertions(+), 3 deletions(-)
diff --git a/2022/13/sol.scm b/2022/13/sol.scm
@@ -2,6 +2,7 @@
(import (chicken string)
(chicken port)
(chicken irregex)
+ (chicken sort)
(chicken io))
(define (parse line)
@@ -31,7 +32,7 @@
((eq? g #t) #t)
(else "Bad result")))))))
-(define (parse-lines lines)
+(define (parse-pairs lines)
(let loop ((lines lines))
(if (null? lines)
'()
@@ -39,6 +40,14 @@
(right (parse (cadr lines)))) ; NOTE: not robust to bad input... (cdddr)
(cons (list left right) (loop (cdddr lines)))))))
+(define (parse-all lines)
+ (let loop ((lines (append lines '("[[2]]" "[[6]]"))))
+ (if (null? lines)
+ '()
+ (if (equal? (car lines) "")
+ (loop (cdr lines))
+ (cons (parse (car lines)) (loop (cdr lines)))))))
+
(define (score pairs)
(let loop ((sum 0) (i 1) (ps pairs))
(if (null? ps)
@@ -47,7 +56,21 @@
(loop (+ i sum) (add1 i) (cdr ps))
(loop sum (add1 i) (cdr ps))))))
+(define (index lst obj)
+ ;; why 1 indexed, advent of code??
+ (let loop ((i 1) (items lst))
+ (cond ((null? items) #f)
+ ((equal? (car items) obj) i)
+ (else (loop (add1 i) (cdr items))))))
+
+(define (signal all)
+ (let ((sorted (sort all (lambda (x y) (good? (list x y))))))
+ (* (index sorted '((2)))
+ (index sorted '((6))))))
+
(define (main args)
(let* ((lines (read-lines))
- (pairs (parse-lines lines)))
- (print (score pairs))))
+ (pairs (parse-pairs lines))
+ (all (parse-all lines)))
+ (print (score pairs))
+ (print (signal all))))