puzzle_5.scm (807B)
1 (define (get-partition partition chr) 2 (if (not (null? partition)) 3 (if (char=? (car partition) chr) 4 (+ (get-partition (cdr partition) chr) (expt 2 (- (length partition) 1))) 5 (get-partition (cdr partition) chr)) 6 0)) 7 8 (define (get-seat-id partition) 9 (+ (* (get-partition (string->list (substring partition 0 7)) #B) 8) 10 (get-partition (string->list (substring partition 7 10)) #R))) 11 12 (define (empty-seat-id seat-ids) 13 (if (= (+ (car seat-ids) 2) (cadr seat-ids)) 14 (+ (car seat-ids) 1) 15 (empty-seat-id (cdr seat-ids)))) 16 17 (load "read_lines.scm") 18 (define partitions (read-lines "files/5.txt" (lambda (x) x))) 19 20 (display (apply max (map (lambda (p) (get-seat-id p)) partitions))) (newline) 21 (display (empty-seat-id (sort (map (lambda (p) (get-seat-id p)) partitions) <))) (newline)