More convenient folder name

This commit is contained in:
2020-12-04 17:05:20 -07:00
parent 2cacce20d8
commit 3ea6ec7d47
21 changed files with 0 additions and 0 deletions

9
projects/aoc/src/Main.hx Normal file
View File

@@ -0,0 +1,9 @@
package;
import kiss.Kiss;
import kiss.Prelude;
import kiss.List;
import year2020.Solutions as Solutions2020;
@:build(kiss.Kiss.build("src/Main.kiss"))
class Main {}

View File

@@ -0,0 +1,2 @@
(defun main []
(Solutions2020.main))

View File

@@ -0,0 +1,9 @@
package year2020;
import kiss.Prelude;
import kiss.Stream;
using StringTools;
@:build(kiss.Kiss.build("src/year2020/Passports.kiss"))
class Passports {}

View File

@@ -0,0 +1,39 @@
(defun readPassport [:Stream stream &opt :Map<String,String> pp]
(set pp (or pp (new Map<String,String>)))
(when (stream.isEmpty) (return pp))
(let [key (stream.expect "passport key" (lambda [] (stream.takeUntilAndDrop ":")))
value (stream.expect "passport value" (lambda [] (stream.takeUntilOneOf [" " #|"\n"|#])))]
(dict-set pp key value))
(if (= #|"\n\n"|# (try (stream.expect "paragraph break" (lambda [] (stream.peekChars 2))) (catch [e] "")))
(begin (stream.dropWhitespace) pp)
(begin (stream.dropWhitespace) (readPassport stream pp))))
(defun checkPassport [:Map<String,String> pp strict]
(doFor key ["byr" "iyr" "eyr" "hgt" "hcl" "ecl" "pid"]
(if !(pp.exists key) (return false)))
(when strict
(unless (<= 1920 (Std.parseInt (dict-get pp "byr")) 2002) (return false))
(unless (<= 2010 (Std.parseInt (dict-get pp "iyr")) 2020) (return false))
(unless (<= 2020 (Std.parseInt (dict-get pp "eyr")) 2030) (return false))
(let [hgt (dict-get pp "hgt")
[min max] (cond ((hgt.endsWith "cm") [150 193]) ((hgt.endsWith "in") [59 76]) (true (return false)))]
(unless (<= min (Std.parseInt hgt) max) (return false)))
(let [hcl (dict-get pp "hcl")]
(unless (and
(hcl.startsWith "#")
(= hcl.length 7)
(apply = (.concat [true]
(for c (.split (hcl.substr 1) "")
(<= 0 (.indexOf (.split "0123456789abcdef" "") c))))))
(return false)))
(let [ecl (dict-get pp "ecl")]
(unless (<= 0 (.indexOf (.split "amb blu brn gry grn hzl oth" " ") ecl)) (return false)))
(let [pid (dict-get pp "pid")]
(unless (and (= 9 pid.length) (Std.parseInt pid)) (return false))))
(return true))
(defun countValidPassports [:Stream stream &opt strict c]
(unless c (set c 0))
(if (stream.isEmpty)
c
(countValidPassports stream strict (if (checkPassport (readPassport stream) strict) (+ c 1) c))))

View File

@@ -0,0 +1,10 @@
package year2020;
import kiss.Prelude;
import kiss.Operand;
import year2020.Util;
using StringTools;
@:build(kiss.Kiss.build("src/year2020/Passwords.kiss"))
class Passwords {}

View File

@@ -0,0 +1,17 @@
(defun parsePasswordCheck1 [:String ruleStr]
(let [[min max letter]
(.split (ruleStr.replace " " "-") "-")]
(lambda [password] (<= (Std.parseInt min) (Util.countChar letter password) (Std.parseInt max)))))
(defun parsePasswordCheck2 [:String ruleStr]
(let [[a b letter]
(.split (ruleStr.replace " " "-") "-")
aIdx (- (Std.parseInt a) 1)
bIdx (- (Std.parseInt b) 1)]
(lambda [password] (= 1 (Util.countChar letter (+ (.charAt password aIdx) (.charAt password bIdx)))))))
(defun validateInputLine [:String line ruleParser]
(let [[rule password]
(line.split ": ")]
((ruleParser rule) password)))

View File

@@ -0,0 +1,14 @@
package year2020;
import haxe.ds.Map;
import StringTools;
import kiss.Prelude;
import kiss.Stream;
import year2020.Util;
import year2020.SummingTuples;
import year2020.Passwords;
import year2020.Toboggan;
import year2020.Passports;
@:build(kiss.Kiss.build("src/year2020/Solutions.kiss"))
class Solutions {}

View File

@@ -0,0 +1,49 @@
(defun main []
// Day 1
(let [p (SummingTuples.pairWithSum 2020 [1721 979 366 299 675 1456])]
(assert (and (has p 1721) (has p 299)) "pairWithSum is broken"))
(let [[a b] (SummingTuples.pairWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))]
(assert (= 545379 (* a b)) "pairWithSum is broken"))
(let [t (SummingTuples.trioWithSum 2020 [1721 979 366 299 675 1456])]
(assert (and (has t 675) (has t 366) (has t 979))
"trioWithSum is broken"))
(let [[a b c] (SummingTuples.trioWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))]
(assert (= 257778836 (* a b c))
"trioWithSum is broken"))
// Day 2
(assert (Passwords.validateInputLine "1-3 a: abcde" Passwords.parsePasswordCheck1))
(assert !(Passwords.validateInputLine "1-3 b: cdefg" Passwords.parsePasswordCheck1))
(assert (Passwords.validateInputLine "2-9 c: ccccccccc" Passwords.parsePasswordCheck1))
(assert (= 655 (count (map (Util.readLines "src/year2020/inputs/day2-1.txt") (.bind Passwords.validateInputLine _ Passwords.parsePasswordCheck1)) (lambda [v] v))))
(assert (Passwords.validateInputLine "1-3 a: abcde" Passwords.parsePasswordCheck2))
(assert !(Passwords.validateInputLine "1-3 b: cdefg" Passwords.parsePasswordCheck2))
(assert !(Passwords.validateInputLine "2-9 c: ccccccccc" Passwords.parsePasswordCheck2))
(assert (= 673 (count (map (Util.readLines "src/year2020/inputs/day2-1.txt") (.bind Passwords.validateInputLine _ Passwords.parsePasswordCheck2)) (lambda [v] v))))
// Day 3
(deflocal exampleHillTile [
"..##......."
"#...#...#.."
".#....#..#."
"..#.#...#.#"
".#...##..#."
"..#.##....."
".#.#.#....#"
".#........#"
"#.##...#..."
"#...##....#"
".#..#...#.#"])
(assert (= "..#.##.####" (Toboggan.pathString exampleHillTile 0 0 3 1)))
(assert (= 2 (Toboggan.pathTrees exampleHillTile 0 0 1 1)))
(assert (= 3 (Toboggan.pathTrees exampleHillTile 0 0 5 1)))
(assert (= 4 (Toboggan.pathTrees exampleHillTile 0 0 7 1)))
(assert (= 2 (Toboggan.pathTrees exampleHillTile 0 0 1 2)))
(assert (= 289 (Util.countChar "#" (Toboggan.pathString (Util.readLines "src/year2020/inputs/day3-1.txt") 0 0 3 1))))
(assert (= 5522401584 (let [hillTile (Util.readLines "src/year2020/inputs/day3-1.txt")]
(apply * (for args [[0 0 1 1] [0 0 3 1] [0 0 5 1] [0 0 7 1] [0 0 1 2]] (apply (Toboggan.pathTrees.bind hillTile) args))))))
// Day 4
(assert (= 2 (Passports.countValidPassports (new Stream "src/year2020/inputs/day4-example.txt"))))
(assert (= 250 (Passports.countValidPassports (new Stream "src/year2020/inputs/day4-1.txt"))))
(assert (= 158 (Passports.countValidPassports (new Stream "src/year2020/inputs/day4-1.txt") "strict"))))

View File

@@ -0,0 +1,6 @@
package year2020;
import kiss.Prelude;
@:build(kiss.Kiss.build("src/year2020/SummingTuples.kiss"))
class SummingTuples {}

View File

@@ -0,0 +1,17 @@
(defun :kiss.List<Int> pairWithSum [sum :kiss.List<Int> numbers]
// Put the numbers in a map for random access. This gives an O(n) solution
(deflocal :Map<Int,Int> numbersMap (new Map))
(doFor number numbers
(dict-set numbersMap number (- sum number))
(let [requiredForPair (dict-get numbersMap number)]
(when (numbersMap.exists requiredForPair)
(return [number requiredForPair]))))
null)
(defun :kiss.List<Int> trioWithSum [sum :kiss.List<Int> numbers]
(doFor number numbers
(let [requiredForTrio (- sum number)
pairThatSatisfies (pairWithSum requiredForTrio numbers)]
(when pairThatSatisfies
(return [number (nth pairThatSatisfies 0) (nth pairThatSatisfies 1)]))))
null)

View File

@@ -0,0 +1,7 @@
package year2020;
import kiss.Prelude;
import year2020.Util;
@:build(kiss.Kiss.build("src/year2020/Toboggan.kiss"))
class Toboggan {}

View File

@@ -0,0 +1,8 @@
(defun path [:kiss.List<String> hillTile x0 y0 dx dy]
(if (>= y0 .length hillTile)
[]
(.concat [(.charAt (nth hillTile y0) (Math.floor (% x0 .length (nth hillTile y0))))] (path hillTile (+ x0 dx) (+ y0 dy) dx dy))))
(defun pathString [:kiss.List<String> hillTile x0 y0 dx dy] (.join (path hillTile x0 y0 dx dy) ""))
(defun pathTrees [:kiss.List<String> hillTile x0 y0 dx dy] (Util.countChar "#" (pathString hillTile x0 y0 dx dy)))

View File

@@ -0,0 +1,9 @@
package year2020;
import sys.io.File;
import kiss.Prelude;
using StringTools;
@:build(kiss.Kiss.build("src/year2020/Util.kiss"))
class Util {}

View File

@@ -0,0 +1,12 @@
(defun readLines [file]
(.filter
(.map
// TODO implement escape sequences in kiss string literals
(.split (.replace (File.getContent file) #|"\r"|# "") #|"\n"|#)
StringTools.trim)
(lambda [l] (< 0 l.length))))
(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt)))
(defun countChar [char str]
(count (str.split "") (lambda [c] ?(= c char))))

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

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,323 @@
.#..#.....#....##..............
...#.#...#...#.#..........#....
#...###...#.#.....#.##.#.#...#.
#.....#.#...##....#...#...#....
##.......##.#.....#........##.#
#..#....#......#..#......#...#.
#..#......#.......#............
##...#.#..#...#........#....##.
#.#.#...#...#..#........#....#.
.......#...........##......#...
##.##.##......#..#............#
..#.###..#..............#......
.##..#.....#......#.#..........
........#.........#....#....###
#..........#........#.#.#......
...##.....#..####.###..#.##....
....#...###............#..#....
...#.#...#.#...#..#.#........##
.....#...#.............#..#....
....#.#.#.##.....##.##....#....
..#....#............#.##.##..#.
.#..#..#................#...###
#..###.#..##..#............#...
.......#.#....#.##.#.##........
##...###.#....#...........###.#
...#.#....#..####.........#....
....##........#.#.#.###........
#...#..#.....#....##..#.##...#.
##....................##..#....
.#....##...........##...##...#.
.#.#..#.........#.........#.#.#
#.#..#.....#.#..#..#..#.#......
...#.............#......#....##
....#.#.......#....#...#.##...#
#.#.#..###..........#...#......
......#.....#..#..#.......##..#
.#......#......#.....#...#.....
......#..#......#.#............
..#............#..#....#.#.....
.....#..##.......#...##.###.#.#
.....#........##....#.#...##..#
..........##.#..#.#...#..#....#
#.#.#.#.##...................#.
.....#....##.....#..#...#..#...
...#....#.............#....#.#.
.........#.##..##..............
#...#.#....#..#...#.......#....
.#...#......#.##.#...#.#..###..
..#.#.#......#..#...##..##.##..
.........#.....#......##....##.
...###.......#..#........#.....
...#....#...#.#.#......##....#.
.#.....#......#...##.##..#.....
..#.##...#....####...##........
..#.#.###....#..##.......##....
.....#....##...#......#.......#
.#....#......#..............#..
.......#.#......#..#....#.#.#..
.......#.#.........###....#....
.#...#.......#.#..#..####....#.
..#...#.#......#..#.##.###..#..
..##.........#............#.#.#
#.........##.##.........#.###..
...#....#.......#..#..##.......
.#....##........##.......#..#..
...#.....#.#.##.#.#.....##.....
.#.#........#.......#.#..#..#..
.....####..##.##.#.#....#......
..#.##.#.#.#....###..#....#.#..
..##..#.#......##.#..#.........
....#..#.#.##.......#...##.....
....###.....#..###...#....###.#
..#....#.......#......#...##..#
..#..##......#....#.###..#..##.
..#..#...............#.#.#.....
...##...#.#..#.#...#......#....
#....#...#.#.#.#.#....#....#...
....##...#....#.....##..#.....#
......##.....#...##..#.......#.
......###......#....#.##..#....
.....#........#........#...#..#
.#..##.....##....#.#......#.#..
#..#.#.....#........#......#.#.
.#..#.##.....#####.#....#.#....
....##........#..........#.#...
.......#.....#.......#...#.#...
.#....#...##.###....#.#......#.
#...#...........##.#...........
#...##.......#..#........#.#..#
.....#..##..###....#.#.#....#..
..#..#.....#............#.#....
............#......#.....#.....
.#..#.....##.........#....###.#
#.........#....#....#.#..#...#.
##.#...##....#..#...#.#...#....
....###..##...................#
....##...#......#...#.#...#...#
#....#....###..........#...#..#
.....##.#....###.###....#..###.
#.....#...........#...........#
##..###.##........#..#.#..#.#..
.##...#..#.......#.#....#.....#
......##..#..#.......#.#...##..
......#..#..#.#...###..#.#....#
#.##.#..#......#...##........##
.....#..........##.....#...#...
........#....##......#......#.#
..#..#.#...#.#.#.......#......#
.#....#........#............#..
......##.....#...#.............
#......##..#.......##....##.#..
.....#..#..#...#.......#..#....
...#..##.#..#.#....##.....#..##
......#...#.#...#.#......###.#.
.#.#...#.....#..###.....#......
#..####.#....#.......##...#....
.##.......#.....#.........#....
#......##.#...............#....
.######.#...##...#...#...#..##.
....#...####....##.#..#...##...
.#...................#.#..#..#.
.#.#....##...#...#.#..#.#.#.#..
......#......#........##.#...#.
##..#...#..#.............##.#..
#.............#..........#.#...
...##.....#.............#......
......###.....#................
#.#.#....#..##.#.....#.........
.#.#........#.........#.#.##.#.
......#...##...#.#.....#....#..
#...#.........##.##.#..........
#..............#..#.......##...
#...#......#.#......#...#....#.
...#...#........#.#......#.###.
##.....#...#.#..#..#..#.......#
..#.##..##.........#...##.##...
#....#....#.....#..........#...
#.####..#..###.....#..#..#.....
..#.....#.##.##..####....#.#.#.
...#.#....#...#.......#..#.....
......###...#.#..#..#..........
.........#..#.....#.#.##......#
.......#.#....##.....##.#..#.#.
.#..#.#..#......##.###...##..#.
....###...........#.....#....#.
.#.##.....#..#.....#......##...
#..##....#..........#.##.##..#.
.###.#.#..#.#.....#..##....#.#.
..##.#....#.....##..#..........
##........#...#..#........###.#
#...#...........##.......#.#...
...###.....##.#....#...#...#...
......#....#.#.......###....#..
...#...#.......##.......###.#..
..............#.#..........##..
#.#....###..#..#.........#.....
.###.#.......#.....#....#.#....
.....###...#.#..#.#.......#....
.........#.##.#......#.#..#....
.......#....#....#.#....#..##.#
...............#...##.#..#.#..#
.....##........#..##...........
.##.#..#....#..#.#...#.........
.#.#..##.#..#......#....#.#...#
##....#.......##...........#...
..#...#.............#.#....#..#
..#......#..#.....#...##.....#.
....##...#.#...##...#..##......
.....#..#..........#...........
..##....#..#.#....#..#........#
.###....#.....#.#....#..##.....
#.......##.......#..#..#....#.#
.##..#...........#..##..##..#..
.#.................#...#....#..
.######.......#............##..
.#.........#......##.#.#.#.#.#.
.#.......#...#...#....###....#.
....#...##.#.#...#.....#.#..#..
.#..#..#...#.....###....#......
...#.##.###........#.....##....
..#....#.#.#..........#..#..#..
......#.....#...#..#..##..#.#..
#.#.......##.......#....#.....#
..#...#..#.#....#.##.##........
..#....#..##..#..##......#.....
#....#..##.....#....###........
##...#......#..###.#.....#.....
#..###....#...#...#...#......##
.....###....#......#..#..#...#.
.##......#.......##...#........
....#.#.....##.....#.....#.....
...##.#.....#..##...#...##.#...
..#...#.#....#....#...##.......
......#....#..#....#.#.........
..........#.#.#...##....#......
...#....................#..#...
...#....###....#..#.....#.....#
..#....#....#..#.#..##.#...#...
..#.##....##.....#.#........#..
#.....###..#.#.#...#..#....#...
........#..#.#..#........##....
.##....#................##.#.##
..##...#.#.#.....##..#....#....
....#..#....#..#........#..##..
...#...##....#....#..##......#.
##........#...#.....#.....#...#
.#......#....##...#.........##.
##........#...#.....#..#...#.#.
...##..#..#.....#..###.#..#....
....#..#..............#.......#
.......#.##...#......#.###.....
#........##..##....#.#.#.......
#.#..##.#.......#..##.....###..
.....##...#..#.....#...........
...#..#..#......#...#.#........
.#....#....#.#.....#.....#....#
...#..#...#..#.##.#......#.#.#.
..##....#..#..#.....#....#....#
...#....#.##.#..#.###......#...
.......#..#.....#.......#..#...
..###.#####..#..##.#.........#.
...#.......##...#.#..#.#......#
....#...#.###..#..........#....
...........#...#..##........#..
.......#...#....#....#.#..#....
.........#..........#...#....##
.##.........##..#.......##.#...
........#......###...##...#.#.#
#.#...##.##...........#...#.#..
.....###...#..##......#..#.....
#.#.....#.#....##..........#..#
#..#.......#.#.........####....
#.#...#.....#........#.....#..#
.....#..#.#.###.....#.#.###....
.###..#......##..#..#..........
#....#.#......#...#.##......#..
..#.........##.#.....#.........
...#....#.....##.#..#..##.#..#.
##.....#.#..#.#....#......#....
....###.#.....#.......#..#.#...
#.....##.....##...........#....
..........#..#......#.##...#...
#...#.###....##....#.###..###..
##........#.#...#..#.........#.
##........##.......#.....###...
.##....###........#..##...#...#
......#..##....##.....#..#.#...
.....#..##..#.......#.......#..
......#....#.......##.#........
.#.####.#..#......#..#.........
.##..#....#...##.#....#....#...
..#..#..#####.........#...#....
....#.....#.#.#.#...#.#......#.
....#...#.#..#.##...#...#......
..#...#...#...#...#..#.#.##..#.
..#......#.#.#.##.##.##..#.....
#..###......#.##...#....#.##.#.
.#.#.......##..##....##...##.#.
.##......##....##.#.......#...#
..#...#...................#....
.#...#.......######.....#.#..##
......#.##.....#.#.............
...........##.#........#..#....
#.............#.#.....#....##..
#...........#...#..###.....#...
....#.......#.#..#..#.#........
......#...##.......#..##....#..
......#.##.##..#........#.#...#
.#..#...##...................#.
.#.............#...#.#.#.#...#.
.........#.....#........#.#....
#..#...#.............##.#.....#
...#.#....#...##............#..
..#...#.##.###.#.....#......##.
...#.#..###...#.#............#.
...#....#........#.#...........
.#......#.#.#.........#.#....#.
....#..#......#.##.....#.#.....
..#..###....#....#.........###.
#..#.#....##.#....#.##..#......
#..#.....#.#.....##..#.##......
......#...#.#.............#..#.
#.#....#.#..#...#......#.#.....
..#.........#.#....#...#.......
.#..#.#...#....#...#......#...#
.......#........#.#..#..#...#..
..##.#......#..##.##.#..#..#...
.##...#....##.....#.....#...##.
#.....##.#....#.#......##..#...
.......#.#..#...#.......#.#...#
..#...#.......#...#..##........
#....##..#...#..#.#......#..#.#
##.#....#....#....#...#..#.##..
###........#.#..#..#......#....
.#......#.....#....#.#..#...#..
.#.....#.....#...##.......#..##
#..##.#..#..........#..........
...#.##.........#.#.##.#.......
.#..#...............#...#.#.#..
.....#.#.....#...####..#.....#.
.#....#.##..##...#...##.#...#.#
....#......##...#.#.#.....#.##.
#...#..#.#...#.#.....##...#....
..#..#....##..###......#..#....
.........#......##.....##....#.
.......#....#...#........###...
.....#..#..#...#...#......#....
..#..#...#.....#.....###..#.###
............#.#..#..#....#.....
...#..#...###.......#.......#..
#.........#........#.....##....
.#.#........#.....#........###.
....#.##.#...#.#.#.....#....#..
.##...#..#.......#.#...........
##...#.##...#...........#.....#
##....#.#.....##..#.......#....
##....#...#....#..#.......####.
......#...#..#.....#.#....#...#
.......#.....#..###............
#.#.#..#.....#.............#..#
.#..#.....##.....#...#.......##
..#.##........##...........#.#.
....##.#..###.#.........#...##.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in