summaryrefslogtreecommitdiff
path: root/report-repair/rr.scm
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2024-06-09 00:16:27 -0600
committerbd <bdunahu@operationnull.com>2024-06-09 00:16:27 -0600
commitcd47b52a406ebed5b3f82e51168ead27af3795d9 (patch)
tree78ad89d67dc77e49636634c1237edf6389c5b2d8 /report-repair/rr.scm
parent6b40eb033d9c0bda4006f23426c34ef8fda4a0d5 (diff)
AoC 2020.1 p1
Diffstat (limited to 'report-repair/rr.scm')
-rw-r--r--report-repair/rr.scm65
1 files changed, 65 insertions, 0 deletions
diff --git a/report-repair/rr.scm b/report-repair/rr.scm
new file mode 100644
index 0000000..b327d3c
--- /dev/null
+++ b/report-repair/rr.scm
@@ -0,0 +1,65 @@
+#!/run/current-system/profile/bin/guile \
+-e main -s
+!#
+(define-module (rr)
+ #:use-module (srfi srfi-1)
+ #:use-module (ice-9 exceptions)
+ #:export (rr
+ multiply-pair
+ return-equivalent-pair
+ equivalent-pair?
+ return-friendship-pair
+ report->pairs))
+
+
+(define (rr str)
+ (multiply-pair
+ (return-equivalent-pair
+ (report->pairs str))))
+
+(define (multiply-pair pair)
+ (* (car pair)
+ (cdr pair)))
+
+(define (return-equivalent-pair pairs)
+ "Given PAIRS friendship pairs, returns the
+ones that are found to be friends. Throws
+friendship-exception if no friends are found.
+
+We do NOT care if there are multiple pairs.
+Return the first one."
+ (let loop ((pair (car pairs)) (pairs (cdr pairs)))
+ (if (null? pairs)
+ (raise-exception
+ (make-exception-with-message
+ "Concerning lack of friendship!"))
+ (if (equivalent-pair? pair pairs)
+ pair
+ (loop (car pairs) (cdr pairs))))))
+
+(define (equivalent-pair? pair pairs)
+ "Given friendship pair PAIR and a list
+of other PAIRS, determines if PAIR is
+contained in PAIRS, irrespective of order"
+ (let ((r-pair (cons (cdr pair)
+ (car pair))))
+ (any (lambda (pair)
+ (equal? r-pair pair))
+ pairs)))
+
+(define (report->pairs str)
+ "Given a report, convert it to a
+list of friendship pairs."
+ (let ((lst (string-split
+ (string-trim-both str char-set:whitespace)
+ char-set:whitespace)))
+ (map (lambda (str)
+ (return-friendship-pair
+ (string->number str)))
+ lst)))
+
+(define (return-friendship-pair num)
+ "Given NUM, returns a cons pair with
+CAR as the original number and CDR as
+the companion number."
+ (cons num (- 2020 num)))