summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--iterative-add/iterative-add-test.scm20
-rw-r--r--iterative-add/iterative-add.scm15
-rw-r--r--report-repair/README.org71
-rw-r--r--report-repair/input.txt200
-rwxr-xr-xreport-repair/main.scm19
-rw-r--r--report-repair/rr-test.scm83
-rw-r--r--report-repair/rr.scm65
7 files changed, 473 insertions, 0 deletions
diff --git a/iterative-add/iterative-add-test.scm b/iterative-add/iterative-add-test.scm
new file mode 100644
index 0000000..d318570
--- /dev/null
+++ b/iterative-add/iterative-add-test.scm
@@ -0,0 +1,20 @@
+;; -*- compile-command: "guile -L . iterative-add-test.scm"; -*-
+(use-modules (srfi srfi-64)
+ (iterative-add))
+
+
+(test-begin "harness")
+
+(test-equal "add 0 to 1"
+ 1
+ (iterative-add-generator 1))
+
+(test-equal "add 1 to 2"
+ 3
+ (iterative-add-generator 2))
+
+(test-equal "add 2 to 6"
+ 8
+ (iterative-add-generator 6))
+
+(test-end "harness")
diff --git a/iterative-add/iterative-add.scm b/iterative-add/iterative-add.scm
new file mode 100644
index 0000000..45c6cf9
--- /dev/null
+++ b/iterative-add/iterative-add.scm
@@ -0,0 +1,15 @@
+(define-module (iterative-add)
+ #:export (iterative-add-generator))
+
+(define iterative-add
+ (let ((prev-num 0))
+ (lambda (num)
+ (let ((result
+ (+ num prev-num)))
+ (set! prev-num num)
+ result))))
+
+(define (iterative-add-generator num)
+ "Adds the previously received
+number to the current one."
+ (iterative-add num))
diff --git a/report-repair/README.org b/report-repair/README.org
new file mode 100644
index 0000000..32faec1
--- /dev/null
+++ b/report-repair/README.org
@@ -0,0 +1,71 @@
+See: https://adventofcode.com/2020/day/1
+** Part 1
+*** Purpose
+Given a list of numbers (report):
+
+#+begin_example
+1721
+979
+366
+299
+675
+1456
+#+end_example
+
+This program takes two entries that sum to 2020 (in the above example, it is 1721 and 299) and multiplies them together.
+
+*** Method
+
+To do this, the program takes the full list of numbers and subtracts them from 2020:
+
+#+begin_example
+299
+1041
+1654
+1721
+1345
+564
+#+end_example
+
+It stores this as a list of cons pairs:
+
+#+begin_example
+'((299 . 1721)
+ (1041 . 979)
+ (1654 . 366)
+ (1721 . 299)
+ (1345 . 675)
+ (564 . 1456))
+#+end_example
+
+Now, we find a pair that is equivalent on the basis of containing the same elements, and complete the multiplication.
+
+** Part 2
+*** Purpose
+Of course, we now need to find three numbers in the list that meet the same criteria...
+
+Say we have a list:
+
+#+begin_example
+979
+366
+675
+#+end_example
+
+We calculate a list of key-value pairs---each number added to every other number...
+
+#+begin_example
+'((((979 366) . 1345)
+ ((979 675) . 1654)
+ ((366 675) . 1041))
+#+end_example
+
+Then, we perform the same operation as we did in the first part:
+
+#+begin_example
+'((((979 366 675) . 1345)
+ ((979 675 366) . 1654)
+ ((366 675 979) . 1041)))
+#+end_example
+
+And we simply check if all three values in the car position are in the original set of numbers.
diff --git a/report-repair/input.txt b/report-repair/input.txt
new file mode 100644
index 0000000..fc2d3b1
--- /dev/null
+++ b/report-repair/input.txt
@@ -0,0 +1,200 @@
+1941
+1887
+1851
+1874
+1612
+1960
+1971
+1983
+1406
+1966
+1554
+1892
+1898
+1926
+1081
+1992
+1073
+1603
+177
+1747
+1063
+1969
+1659
+1303
+1759
+1853
+1107
+1818
+1672
+1352
+2002
+1838
+1985
+1860
+1141
+1903
+1334
+1489
+1178
+1823
+1499
+1951
+1225
+1503
+1417
+1724
+1165
+1339
+1816
+1504
+1588
+1997
+1946
+1324
+1771
+1982
+1272
+1367
+1439
+1252
+1902
+1940
+1333
+1750
+1512
+1538
+1168
+2001
+1797
+1233
+972
+1306
+1835
+1825
+1822
+1880
+1732
+1785
+1727
+1275
+1355
+1793
+1485
+1297
+1932
+1519
+1587
+1382
+1914
+1745
+1087
+1996
+1746
+1962
+1573
+2008
+1868
+1278
+1386
+1238
+1242
+1170
+1476
+1161
+1754
+1807
+1514
+1189
+1916
+1884
+1535
+1217
+1911
+1861
+1493
+1409
+1783
+1222
+1955
+1673
+1502
+607
+2010
+1846
+1819
+1500
+1799
+1475
+1146
+1608
+1806
+1660
+1618
+1904
+978
+1762
+1925
+1185
+1154
+1239
+1843
+1986
+533
+1509
+1913
+287
+1707
+1115
+1699
+1859
+1077
+1915
+1412
+1360
+1646
+1973
+1627
+1755
+1748
+1769
+1886
+1422
+1686
+950
+100
+1372
+1068
+1370
+1428
+1870
+1108
+190
+1891
+1794
+1228
+1128
+1365
+1740
+1888
+1460
+1758
+1906
+1917
+1989
+1251
+1866
+1560
+1921
+1777
+1102
+1850
+1498
+683
+1840
+1800
+1112
+1908
+1442
+1082
+1071
diff --git a/report-repair/main.scm b/report-repair/main.scm
new file mode 100755
index 0000000..cdfed63
--- /dev/null
+++ b/report-repair/main.scm
@@ -0,0 +1,19 @@
+#!/run/current-system/profile/bin/guile \
+-e main -s
+!#
+
+(use-modules (rr)
+ ((ice-9 rdelim))
+ (ice-9 binary-ports))
+
+
+(define (stdin-to-str)
+ (let loop ((result ""))
+ (let ((line (read-line)))
+ (if (eof-object? line)
+ result
+ (loop (string-append result line " "))))))
+
+(define (main args)
+ (display (rr (stdin-to-str)))
+ (newline))
diff --git a/report-repair/rr-test.scm b/report-repair/rr-test.scm
new file mode 100644
index 0000000..7d75c09
--- /dev/null
+++ b/report-repair/rr-test.scm
@@ -0,0 +1,83 @@
+;; -*- compile-command: "guile -L . rr-test.scm"; -*-
+(use-modules (srfi srfi-64)
+ (rr))
+
+(test-begin "harness")
+
+
+(test-equal "friendship 1"
+ '(1 . 2019)
+ (return-friendship-pair 1))
+
+(test-equal "friendship 1596"
+ '(1596 . 424)
+ (return-friendship-pair 1596))
+
+(test-equal "small report"
+ '((0 . 2020)
+ (1 . 2019)
+ (2 . 2018)
+ (3 . 2017)
+ (4 . 2016)
+ (5 . 2015))
+ (report->pairs "0 1 2 3 4 5"))
+
+(test-assert "equivalent pair"
+ (equivalent-pair?
+ '(1 . 4)
+ '((5 . 6)
+ (1 . 5)
+ (3 . 7)
+ (4 . 1)
+ (6 . 2))))
+
+(test-equal "no equivalent pair"
+ #f
+ (equivalent-pair?
+ '(1 . 4)
+ '((5 . 6)
+ (1 . 5)
+ (3 . 7)
+ (6 . 2))))
+
+(test-equal "found equivalent pair"
+ '(1 . 5)
+ (return-equivalent-pair
+ '((5 . 6)
+ (1 . 5)
+ (3 . 7)
+ (5 . 1)
+ (6 . 2))))
+
+(test-error "no equivalent pairs"
+ #t
+ (return-equivalent-pair
+ '((5 . 6)
+ (1 . 5)
+ (3 . 7)
+ (6 . 2))))
+
+(test-equal "multiply pair 1"
+ 8
+ (multiply-pair (cons 1 8)))
+
+(test-equal "multiply pair 2"
+ 24
+ (multiply-pair (cons 3 8)))
+
+(test-equal "task-complete 1"
+ 1020099
+ (rr "1009 237 791 478 1537 1011 1628"))
+
+(test-equal "task-complete 2"
+ 514579
+ (rr "1721
+979
+366
+299
+675
+1456
+"))
+
+
+(test-end "harness")
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)))