From 00daf031374f5b3671df643f81d0506c5fc02278 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Tue, 13 Dec 2022 23:21:54 -0500 Subject: [PATCH] day13: Add part 2 --- 2022/13/sol.scm | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/2022/13/sol.scm b/2022/13/sol.scm index 87ba9d7..cfeabe0 100755 --- 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)))) -- libgit2 1.8.1