Solve AOC2020 star 1.1

This commit is contained in:
2020-12-01 13:37:07 -07:00
parent d8078c02d7
commit 1ea1080470
4 changed files with 240 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package;
import kiss.Kiss;
import kiss.Prelude;
import kiss.List;
import year2020.Solutions as Solutions2020;
@:build(kiss.Kiss.build("src/Main.kiss"))

View File

@@ -1,4 +1,9 @@
package year2020;
import haxe.ds.Map;
import sys.io.File;
import StringTools;
import kiss.Prelude;
@:build(kiss.Kiss.build("src/year2020/Solutions.kiss"))
class Solutions { }
class Solutions {}

View File

@@ -1,4 +1,35 @@
(defun main []
// Day 1
(throw "no solutions yet")
)
// TODO implement unless
// TODO list destructuring a funcall results in double evaluation
(let [[a b] (pairWithSum 2020 [1721 979 366 299 675 1456])]
(when !(and (= 1721 a) (= 299 b))
(throw "pairWithSum is broken")))
(let [[a b] (pairWithSum 2020 (readInts "src/year2020/inputs/day1-1.txt"))]
(when !(= 545379 (* a b))
(throw "pairWithSum is broken"))))
// TODO implement .method readerexps and use them instead of let:
(defun readLines [file]
(let [content (File.getContent file)
lines (content.split #|"\n"|#)
trimmed (lines.map StringTools.trim)
filtered (lines.filter (lambda [l] (< 0 l.length)))]
filtered))
(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt)))
(defun :kiss.List<Int> pairWithSum [sum :kiss.List<Int> numbers]
(deflocal :Map<Int,Int> numbersMap (new Map))
// Put the numbers in a map for random access. This gives an O(n) solution
(doFor number numbers
// TODO implement dict-set, dict-get, set-nth, and use them
(set (nth numbersMap number) (- sum number)))
// TODO implement early return, break
(deflocal &mut pair null)
(doFor number numbers
(let [requiredForPair (nth numbersMap number)]
(when (numbersMap.exists requiredForPair)
(set pair (or pair [number requiredForPair])))))
pair)

View File

@@ -0,0 +1,200 @@
1825
1944
1802
1676
1921
1652
1710
1952
1932
1934
1823
1732
1795
1681
1706
1697
1919
1695
2007
1889
1942
961
1868
1878
1723
416
1875
1831
1890
1654
1956
1827
973
1947
1688
1680
1808
1998
1794
1552
1935
1693
1824
1711
1766
1668
1968
1884
217
2003
1869
1658
1953
1829
1984
2005
1973
428
1957
1925
1719
1797
321
1804
1971
922
1976
1863
2008
1806
1833
1809
1707
1954
1811
1815
1915
1799
1917
1664
1937
1775
1685
1756
1940
1660
1859
1916
1989
1763
1994
1716
1689
1866
1708
1670
1982
1870
1847
1627
1819
1786
1828
1640
1699
1722
1737
1882
1666
1871
1703
1770
1623
1837
1636
1655
1930
1739
1810
1805
1861
1922
1993
1896
1760
2002
1779
1633
1972
1856
1641
1718
2004
1730
1826
1923
1753
1735
660
1988
1796
1990
1720
1626
1788
1700
942
1902
1943
1758
1839
1924
938
1634
1724
1983
1683
1687
1904
1907
1757
2001
1910
1849
1781
1981
1743
1851
2009
619
1898
1891
1751
1765
1959
1888
1894
1759
389
1964
1900
1742
1672
1969
1978
1933
1906
1807
1867
1838
1960
1814
1950
1918
1726
1986
1746
2006
1949
1784