Mitesh / Dec 01 2020 / Published
Remix of Clojure by Nextjournal
Advent of Clojure #01
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
https://adventofcode.com/2020/day/1
(def input-data (1293 1207 1623 1675 1842 1410 85 1108 557 1217 1506 1956 1579 1614 1360 1544 1946 1666 1972 1814 1699 1778 1529 2002 1768 1173 1407 1201 1264 1739 1774 1951 1980 1428 1381 1714 884 1939 1295 1694 1168 1971 1352 1462 1828 1402 1433 1542 1144 1331 1427 1261 1663 1820 1570 1874 1486 1613 1769 1721 1753 1142 1677 2010 1640 1465 1171 534 1790 2005 1604 1891 1247 1281 1867 1403 2004 1668 1416 2001 1359 686 1965 1728 1551 1565 1128 1832 1757 1350 1808 1711 1799 1590 1989 1547 1140 1905 1368 1179 1902 1473 1908 1859 1257 1394 1244 1800 1695 1731 1474 1781 1885 1154 1990 1929 1193 1302 1831 1226 1418 1400 1435 1645 1655 1843 1227 1481 1754 1290 1685 1498 71 1286 1137 1288 1758 1987 1471 1839 1545 1682 1615 1475 1849 1985 1568 1795 1184 1863 1362 1271 1802 1944 1821 1880 1788 1733 1150 1314 1727 1434 1833 1312 1457 160 1629 1967 1505 1239 1266 1838 1687 1630 1591 1893 1450 1234 1755 1523 1533 1499 1865 1725 1444 1517 1167 1738 1519 1263 1901 1627 1644 1771 1812 1270 1497 1707 1708 1396))
0.1s
Clojure
(defn find-pair [data sum-val]
(loop [alist (sort data)
blist (reverse (sort data))]
(if (or (empty? alist) (empty? blist)) nil)
(let [sum (+ (first alist) (first blist))]
(cond
(= sum sum-val) [(first alist) (first blist)]
(> sum sum-val) (recur alist (rest blist))
(< sum sum-val) (recur (rest alist) blist)
))))
0.0s
Clojure
#user/find-pair #object [user$find_pair 0x3cbf0931 "user$find_pair@3cbf0931"]
(time (apply * (find-pair input-data 2020)))
0.4s
Clojure
793524
(defn drop-nth [n coll]
(concat (take n coll) (drop (inc n) coll)))
0.1s
Clojure
#user/drop-nth #object [user$drop_nth 0x7e072de7 "user$drop_nth@7e072de7"]
(defn find-triplet [data]
(loop [n 0]
(let [item (nth data n)
clist (drop-nth n data)
pair (find-pair clist (- 2020 item))]
(if (nil? pair)
(recur (inc n))
(conj pair item)))))
0.0s
Clojure
#user/find-triplet #object [user$find_triplet 0x5cc9ed6c "user$find_triplet@5cc9ed6c"]
(time (apply * (find-triplet (sort input-data))))
0.4s
Clojure
61515678