From fbf8c5d14f51cec79b8b0d4927a75043ab369394 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 3 Dec 2020 13:52:30 -0700 Subject: [PATCH] Solve AOC day 3.1 --- .../src/year2020/Passwords.kiss | 6 +- .../advent-of-code/src/year2020/Solutions.hx | 1 + .../src/year2020/Solutions.kiss | 19 +- .../advent-of-code/src/year2020/Toboggan.hx | 7 + .../advent-of-code/src/year2020/Toboggan.kiss | 6 + .../advent-of-code/src/year2020/Util.kiss | 5 +- .../src/year2020/inputs/day3-1.txt | 323 ++++++++++++++++++ 7 files changed, 361 insertions(+), 6 deletions(-) create mode 100644 projects/advent-of-code/src/year2020/Toboggan.hx create mode 100644 projects/advent-of-code/src/year2020/Toboggan.kiss create mode 100644 projects/advent-of-code/src/year2020/inputs/day3-1.txt diff --git a/projects/advent-of-code/src/year2020/Passwords.kiss b/projects/advent-of-code/src/year2020/Passwords.kiss index 1e784d55..8d9a03f9 100644 --- a/projects/advent-of-code/src/year2020/Passwords.kiss +++ b/projects/advent-of-code/src/year2020/Passwords.kiss @@ -1,17 +1,15 @@ -(defun countChar [char str] - (count (str.split "") (lambda [c] ?(= c char)))) (defun parsePasswordCheck1 [:String ruleStr] (let [[min max letter] (.split (ruleStr.replace " " "-") "-")] - (lambda [password] (<= (Std.parseInt min) (countChar letter password) (Std.parseInt max))))) + (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 (countChar letter (+ (.charAt password aIdx) (.charAt password bIdx))))))) + (lambda [password] (= 1 (Util.countChar letter (+ (.charAt password aIdx) (.charAt password bIdx))))))) (defun validateInputLine [:String line ruleParser] (let [[rule password] diff --git a/projects/advent-of-code/src/year2020/Solutions.hx b/projects/advent-of-code/src/year2020/Solutions.hx index ba417380..d0e572bd 100644 --- a/projects/advent-of-code/src/year2020/Solutions.hx +++ b/projects/advent-of-code/src/year2020/Solutions.hx @@ -5,6 +5,7 @@ import StringTools; import kiss.Prelude; import year2020.Util; import year2020.Passwords; +import year2020.Toboggan; @:build(kiss.Kiss.build("src/year2020/Solutions.kiss")) class Solutions {} diff --git a/projects/advent-of-code/src/year2020/Solutions.kiss b/projects/advent-of-code/src/year2020/Solutions.kiss index 5a0805fc..85900f49 100644 --- a/projects/advent-of-code/src/year2020/Solutions.kiss +++ b/projects/advent-of-code/src/year2020/Solutions.kiss @@ -29,7 +29,24 @@ (when (Passwords.validateInputLine "2-9 c: ccccccccc" Passwords.parsePasswordCheck2) (throw "parsePasswordCheck2 is broken")) (unless (= 673 (count (map (Util.readLines "src/year2020/inputs/day2-1.txt") (.bind Passwords.validateInputLine _ Passwords.parsePasswordCheck2)) (lambda [v] v))) - (throw "parsePasswordCheck2 is broken"))) + (throw "parsePasswordCheck2 is broken")) + + // Day 3 + (unless (= "..#.##.####" (Toboggan.pathString [ + "..##......." + "#...#...#.." + ".#....#..#." + "..#.#...#.#" + ".#...##..#." + "..#.##....." + ".#.#.#....#" + ".#........#" + "#.##...#..." + "#...##....#" + ".#..#...#.#"] 0 0 3 1)) + (throw "Toboggan.path is broken")) + (unless (= 289 (Util.countChar "#" (Toboggan.pathString (Util.readLines "src/year2020/inputs/day3-1.txt") 0 0 3 1))) + (throw "Toboggan.path is broken"))) (defun :kiss.List pairWithSum [sum :kiss.List numbers] // Put the numbers in a map for random access. This gives an O(n) solution diff --git a/projects/advent-of-code/src/year2020/Toboggan.hx b/projects/advent-of-code/src/year2020/Toboggan.hx new file mode 100644 index 00000000..ec1cf51d --- /dev/null +++ b/projects/advent-of-code/src/year2020/Toboggan.hx @@ -0,0 +1,7 @@ +package year2020; + +import kiss.Prelude; +import year2020.Util; + +@:build(kiss.Kiss.build("src/year2020/Toboggan.kiss")) +class Toboggan {} diff --git a/projects/advent-of-code/src/year2020/Toboggan.kiss b/projects/advent-of-code/src/year2020/Toboggan.kiss new file mode 100644 index 00000000..f788eadc --- /dev/null +++ b/projects/advent-of-code/src/year2020/Toboggan.kiss @@ -0,0 +1,6 @@ +(defun path [:kiss.List 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 hillTile x0 y0 dx dy] (.join (path hillTile x0 y0 dx dy) "")) \ No newline at end of file diff --git a/projects/advent-of-code/src/year2020/Util.kiss b/projects/advent-of-code/src/year2020/Util.kiss index 5587799e..3be4f8d2 100644 --- a/projects/advent-of-code/src/year2020/Util.kiss +++ b/projects/advent-of-code/src/year2020/Util.kiss @@ -6,4 +6,7 @@ StringTools.trim) (lambda [l] (< 0 l.length)))) -(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt))) \ No newline at end of file +(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt))) + +(defun countChar [char str] + (count (str.split "") (lambda [c] ?(= c char)))) \ No newline at end of file diff --git a/projects/advent-of-code/src/year2020/inputs/day3-1.txt b/projects/advent-of-code/src/year2020/inputs/day3-1.txt new file mode 100644 index 00000000..3c4bd1cc --- /dev/null +++ b/projects/advent-of-code/src/year2020/inputs/day3-1.txt @@ -0,0 +1,323 @@ +.#..#.....#....##.............. +...#.#...#...#.#..........#.... +#...###...#.#.....#.##.#.#...#. +#.....#.#...##....#...#...#.... +##.......##.#.....#........##.# +#..#....#......#..#......#...#. +#..#......#.......#............ +##...#.#..#...#........#....##. +#.#.#...#...#..#........#....#. +.......#...........##......#... +##.##.##......#..#............# +..#.###..#..............#...... +.##..#.....#......#.#.......... +........#.........#....#....### +#..........#........#.#.#...... +...##.....#..####.###..#.##.... +....#...###............#..#.... +...#.#...#.#...#..#.#........## +.....#...#.............#..#.... +....#.#.#.##.....##.##....#.... +..#....#............#.##.##..#. +.#..#..#................#...### +#..###.#..##..#............#... +.......#.#....#.##.#.##........ +##...###.#....#...........###.# +...#.#....#..####.........#.... +....##........#.#.#.###........ +#...#..#.....#....##..#.##...#. +##....................##..#.... +.#....##...........##...##...#. +.#.#..#.........#.........#.#.# +#.#..#.....#.#..#..#..#.#...... +...#.............#......#....## +....#.#.......#....#...#.##...# +#.#.#..###..........#...#...... +......#.....#..#..#.......##..# +.#......#......#.....#...#..... +......#..#......#.#............ +..#............#..#....#.#..... +.....#..##.......#...##.###.#.# +.....#........##....#.#...##..# +..........##.#..#.#...#..#....# +#.#.#.#.##...................#. +.....#....##.....#..#...#..#... +...#....#.............#....#.#. +.........#.##..##.............. +#...#.#....#..#...#.......#.... +.#...#......#.##.#...#.#..###.. +..#.#.#......#..#...##..##.##.. +.........#.....#......##....##. +...###.......#..#........#..... +...#....#...#.#.#......##....#. +.#.....#......#...##.##..#..... +..#.##...#....####...##........ +..#.#.###....#..##.......##.... +.....#....##...#......#.......# +.#....#......#..............#.. +.......#.#......#..#....#.#.#.. +.......#.#.........###....#.... +.#...#.......#.#..#..####....#. +..#...#.#......#..#.##.###..#.. +..##.........#............#.#.# +#.........##.##.........#.###.. +...#....#.......#..#..##....... +.#....##........##.......#..#.. +...#.....#.#.##.#.#.....##..... +.#.#........#.......#.#..#..#.. +.....####..##.##.#.#....#...... +..#.##.#.#.#....###..#....#.#.. +..##..#.#......##.#..#......... +....#..#.#.##.......#...##..... +....###.....#..###...#....###.# +..#....#.......#......#...##..# +..#..##......#....#.###..#..##. +..#..#...............#.#.#..... +...##...#.#..#.#...#......#.... +#....#...#.#.#.#.#....#....#... +....##...#....#.....##..#.....# +......##.....#...##..#.......#. +......###......#....#.##..#.... +.....#........#........#...#..# +.#..##.....##....#.#......#.#.. +#..#.#.....#........#......#.#. +.#..#.##.....#####.#....#.#.... +....##........#..........#.#... +.......#.....#.......#...#.#... +.#....#...##.###....#.#......#. +#...#...........##.#........... +#...##.......#..#........#.#..# +.....#..##..###....#.#.#....#.. +..#..#.....#............#.#.... +............#......#.....#..... +.#..#.....##.........#....###.# +#.........#....#....#.#..#...#. +##.#...##....#..#...#.#...#.... +....###..##...................# +....##...#......#...#.#...#...# +#....#....###..........#...#..# +.....##.#....###.###....#..###. +#.....#...........#...........# +##..###.##........#..#.#..#.#.. +.##...#..#.......#.#....#.....# +......##..#..#.......#.#...##.. +......#..#..#.#...###..#.#....# +#.##.#..#......#...##........## +.....#..........##.....#...#... +........#....##......#......#.# +..#..#.#...#.#.#.......#......# +.#....#........#............#.. +......##.....#...#............. +#......##..#.......##....##.#.. +.....#..#..#...#.......#..#.... +...#..##.#..#.#....##.....#..## +......#...#.#...#.#......###.#. +.#.#...#.....#..###.....#...... +#..####.#....#.......##...#.... +.##.......#.....#.........#.... +#......##.#...............#.... +.######.#...##...#...#...#..##. +....#...####....##.#..#...##... +.#...................#.#..#..#. +.#.#....##...#...#.#..#.#.#.#.. +......#......#........##.#...#. +##..#...#..#.............##.#.. +#.............#..........#.#... +...##.....#.............#...... +......###.....#................ +#.#.#....#..##.#.....#......... +.#.#........#.........#.#.##.#. +......#...##...#.#.....#....#.. +#...#.........##.##.#.......... +#..............#..#.......##... +#...#......#.#......#...#....#. +...#...#........#.#......#.###. +##.....#...#.#..#..#..#.......# +..#.##..##.........#...##.##... +#....#....#.....#..........#... +#.####..#..###.....#..#..#..... +..#.....#.##.##..####....#.#.#. +...#.#....#...#.......#..#..... +......###...#.#..#..#.......... +.........#..#.....#.#.##......# +.......#.#....##.....##.#..#.#. +.#..#.#..#......##.###...##..#. +....###...........#.....#....#. +.#.##.....#..#.....#......##... +#..##....#..........#.##.##..#. +.###.#.#..#.#.....#..##....#.#. +..##.#....#.....##..#.......... +##........#...#..#........###.# +#...#...........##.......#.#... +...###.....##.#....#...#...#... +......#....#.#.......###....#.. +...#...#.......##.......###.#.. +..............#.#..........##.. +#.#....###..#..#.........#..... +.###.#.......#.....#....#.#.... +.....###...#.#..#.#.......#.... +.........#.##.#......#.#..#.... +.......#....#....#.#....#..##.# +...............#...##.#..#.#..# +.....##........#..##........... +.##.#..#....#..#.#...#......... +.#.#..##.#..#......#....#.#...# +##....#.......##...........#... +..#...#.............#.#....#..# +..#......#..#.....#...##.....#. +....##...#.#...##...#..##...... +.....#..#..........#........... +..##....#..#.#....#..#........# +.###....#.....#.#....#..##..... +#.......##.......#..#..#....#.# +.##..#...........#..##..##..#.. +.#.................#...#....#.. +.######.......#............##.. +.#.........#......##.#.#.#.#.#. +.#.......#...#...#....###....#. +....#...##.#.#...#.....#.#..#.. +.#..#..#...#.....###....#...... +...#.##.###........#.....##.... +..#....#.#.#..........#..#..#.. +......#.....#...#..#..##..#.#.. +#.#.......##.......#....#.....# +..#...#..#.#....#.##.##........ +..#....#..##..#..##......#..... +#....#..##.....#....###........ +##...#......#..###.#.....#..... +#..###....#...#...#...#......## +.....###....#......#..#..#...#. +.##......#.......##...#........ +....#.#.....##.....#.....#..... +...##.#.....#..##...#...##.#... +..#...#.#....#....#...##....... +......#....#..#....#.#......... +..........#.#.#...##....#...... +...#....................#..#... +...#....###....#..#.....#.....# +..#....#....#..#.#..##.#...#... +..#.##....##.....#.#........#.. +#.....###..#.#.#...#..#....#... +........#..#.#..#........##.... +.##....#................##.#.## +..##...#.#.#.....##..#....#.... +....#..#....#..#........#..##.. +...#...##....#....#..##......#. +##........#...#.....#.....#...# +.#......#....##...#.........##. +##........#...#.....#..#...#.#. +...##..#..#.....#..###.#..#.... +....#..#..............#.......# +.......#.##...#......#.###..... +#........##..##....#.#.#....... +#.#..##.#.......#..##.....###.. +.....##...#..#.....#........... +...#..#..#......#...#.#........ +.#....#....#.#.....#.....#....# +...#..#...#..#.##.#......#.#.#. +..##....#..#..#.....#....#....# +...#....#.##.#..#.###......#... +.......#..#.....#.......#..#... +..###.#####..#..##.#.........#. +...#.......##...#.#..#.#......# +....#...#.###..#..........#.... +...........#...#..##........#.. +.......#...#....#....#.#..#.... +.........#..........#...#....## +.##.........##..#.......##.#... +........#......###...##...#.#.# +#.#...##.##...........#...#.#.. +.....###...#..##......#..#..... +#.#.....#.#....##..........#..# +#..#.......#.#.........####.... +#.#...#.....#........#.....#..# +.....#..#.#.###.....#.#.###.... +.###..#......##..#..#.......... +#....#.#......#...#.##......#.. +..#.........##.#.....#......... +...#....#.....##.#..#..##.#..#. +##.....#.#..#.#....#......#.... +....###.#.....#.......#..#.#... +#.....##.....##...........#.... +..........#..#......#.##...#... +#...#.###....##....#.###..###.. +##........#.#...#..#.........#. +##........##.......#.....###... +.##....###........#..##...#...# +......#..##....##.....#..#.#... +.....#..##..#.......#.......#.. +......#....#.......##.#........ +.#.####.#..#......#..#......... +.##..#....#...##.#....#....#... +..#..#..#####.........#...#.... +....#.....#.#.#.#...#.#......#. +....#...#.#..#.##...#...#...... +..#...#...#...#...#..#.#.##..#. +..#......#.#.#.##.##.##..#..... +#..###......#.##...#....#.##.#. +.#.#.......##..##....##...##.#. +.##......##....##.#.......#...# +..#...#...................#.... +.#...#.......######.....#.#..## +......#.##.....#.#............. +...........##.#........#..#.... +#.............#.#.....#....##.. +#...........#...#..###.....#... +....#.......#.#..#..#.#........ +......#...##.......#..##....#.. +......#.##.##..#........#.#...# +.#..#...##...................#. +.#.............#...#.#.#.#...#. +.........#.....#........#.#.... +#..#...#.............##.#.....# +...#.#....#...##............#.. +..#...#.##.###.#.....#......##. +...#.#..###...#.#............#. +...#....#........#.#........... +.#......#.#.#.........#.#....#. +....#..#......#.##.....#.#..... +..#..###....#....#.........###. +#..#.#....##.#....#.##..#...... +#..#.....#.#.....##..#.##...... +......#...#.#.............#..#. +#.#....#.#..#...#......#.#..... +..#.........#.#....#...#....... +.#..#.#...#....#...#......#...# +.......#........#.#..#..#...#.. +..##.#......#..##.##.#..#..#... +.##...#....##.....#.....#...##. +#.....##.#....#.#......##..#... +.......#.#..#...#.......#.#...# +..#...#.......#...#..##........ +#....##..#...#..#.#......#..#.# +##.#....#....#....#...#..#.##.. +###........#.#..#..#......#.... +.#......#.....#....#.#..#...#.. +.#.....#.....#...##.......#..## +#..##.#..#..........#.......... +...#.##.........#.#.##.#....... +.#..#...............#...#.#.#.. +.....#.#.....#...####..#.....#. +.#....#.##..##...#...##.#...#.# +....#......##...#.#.#.....#.##. +#...#..#.#...#.#.....##...#.... +..#..#....##..###......#..#.... +.........#......##.....##....#. +.......#....#...#........###... +.....#..#..#...#...#......#.... +..#..#...#.....#.....###..#.### +............#.#..#..#....#..... +...#..#...###.......#.......#.. +#.........#........#.....##.... +.#.#........#.....#........###. +....#.##.#...#.#.#.....#....#.. +.##...#..#.......#.#........... +##...#.##...#...........#.....# +##....#.#.....##..#.......#.... +##....#...#....#..#.......####. +......#...#..#.....#.#....#...# +.......#.....#..###............ +#.#.#..#.....#.............#..# +.#..#.....##.....#...#.......## +..#.##........##...........#.#. +....##.#..###.#.........#...##.