Solve AOC day 3.1

This commit is contained in:
2020-12-03 13:52:30 -07:00
parent 7b41a17046
commit fbf8c5d14f
7 changed files with 361 additions and 6 deletions

View File

@@ -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]

View File

@@ -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 {}

View File

@@ -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<Int> pairWithSum [sum :kiss.List<Int> numbers]
// Put the numbers in a map for random access. This gives an O(n) solution

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,6 @@
(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) ""))

View File

@@ -6,4 +6,7 @@
StringTools.trim)
(lambda [l] (< 0 l.length))))
(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt)))
(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,323 @@
.#..#.....#....##..............
...#.#...#...#.#..........#....
#...###...#.#.....#.##.#.#...#.
#.....#.#...##....#...#...#....
##.......##.#.....#........##.#
#..#....#......#..#......#...#.
#..#......#.......#............
##...#.#..#...#........#....##.
#.#.#...#...#..#........#....#.
.......#...........##......#...
##.##.##......#..#............#
..#.###..#..............#......
.##..#.....#......#.#..........
........#.........#....#....###
#..........#........#.#.#......
...##.....#..####.###..#.##....
....#...###............#..#....
...#.#...#.#...#..#.#........##
.....#...#.............#..#....
....#.#.#.##.....##.##....#....
..#....#............#.##.##..#.
.#..#..#................#...###
#..###.#..##..#............#...
.......#.#....#.##.#.##........
##...###.#....#...........###.#
...#.#....#..####.........#....
....##........#.#.#.###........
#...#..#.....#....##..#.##...#.
##....................##..#....
.#....##...........##...##...#.
.#.#..#.........#.........#.#.#
#.#..#.....#.#..#..#..#.#......
...#.............#......#....##
....#.#.......#....#...#.##...#
#.#.#..###..........#...#......
......#.....#..#..#.......##..#
.#......#......#.....#...#.....
......#..#......#.#............
..#............#..#....#.#.....
.....#..##.......#...##.###.#.#
.....#........##....#.#...##..#
..........##.#..#.#...#..#....#
#.#.#.#.##...................#.
.....#....##.....#..#...#..#...
...#....#.............#....#.#.
.........#.##..##..............
#...#.#....#..#...#.......#....
.#...#......#.##.#...#.#..###..
..#.#.#......#..#...##..##.##..
.........#.....#......##....##.
...###.......#..#........#.....
...#....#...#.#.#......##....#.
.#.....#......#...##.##..#.....
..#.##...#....####...##........
..#.#.###....#..##.......##....
.....#....##...#......#.......#
.#....#......#..............#..
.......#.#......#..#....#.#.#..
.......#.#.........###....#....
.#...#.......#.#..#..####....#.
..#...#.#......#..#.##.###..#..
..##.........#............#.#.#
#.........##.##.........#.###..
...#....#.......#..#..##.......
.#....##........##.......#..#..
...#.....#.#.##.#.#.....##.....
.#.#........#.......#.#..#..#..
.....####..##.##.#.#....#......
..#.##.#.#.#....###..#....#.#..
..##..#.#......##.#..#.........
....#..#.#.##.......#...##.....
....###.....#..###...#....###.#
..#....#.......#......#...##..#
..#..##......#....#.###..#..##.
..#..#...............#.#.#.....
...##...#.#..#.#...#......#....
#....#...#.#.#.#.#....#....#...
....##...#....#.....##..#.....#
......##.....#...##..#.......#.
......###......#....#.##..#....
.....#........#........#...#..#
.#..##.....##....#.#......#.#..
#..#.#.....#........#......#.#.
.#..#.##.....#####.#....#.#....
....##........#..........#.#...
.......#.....#.......#...#.#...
.#....#...##.###....#.#......#.
#...#...........##.#...........
#...##.......#..#........#.#..#
.....#..##..###....#.#.#....#..
..#..#.....#............#.#....
............#......#.....#.....
.#..#.....##.........#....###.#
#.........#....#....#.#..#...#.
##.#...##....#..#...#.#...#....
....###..##...................#
....##...#......#...#.#...#...#
#....#....###..........#...#..#
.....##.#....###.###....#..###.
#.....#...........#...........#
##..###.##........#..#.#..#.#..
.##...#..#.......#.#....#.....#
......##..#..#.......#.#...##..
......#..#..#.#...###..#.#....#
#.##.#..#......#...##........##
.....#..........##.....#...#...
........#....##......#......#.#
..#..#.#...#.#.#.......#......#
.#....#........#............#..
......##.....#...#.............
#......##..#.......##....##.#..
.....#..#..#...#.......#..#....
...#..##.#..#.#....##.....#..##
......#...#.#...#.#......###.#.
.#.#...#.....#..###.....#......
#..####.#....#.......##...#....
.##.......#.....#.........#....
#......##.#...............#....
.######.#...##...#...#...#..##.
....#...####....##.#..#...##...
.#...................#.#..#..#.
.#.#....##...#...#.#..#.#.#.#..
......#......#........##.#...#.
##..#...#..#.............##.#..
#.............#..........#.#...
...##.....#.............#......
......###.....#................
#.#.#....#..##.#.....#.........
.#.#........#.........#.#.##.#.
......#...##...#.#.....#....#..
#...#.........##.##.#..........
#..............#..#.......##...
#...#......#.#......#...#....#.
...#...#........#.#......#.###.
##.....#...#.#..#..#..#.......#
..#.##..##.........#...##.##...
#....#....#.....#..........#...
#.####..#..###.....#..#..#.....
..#.....#.##.##..####....#.#.#.
...#.#....#...#.......#..#.....
......###...#.#..#..#..........
.........#..#.....#.#.##......#
.......#.#....##.....##.#..#.#.
.#..#.#..#......##.###...##..#.
....###...........#.....#....#.
.#.##.....#..#.....#......##...
#..##....#..........#.##.##..#.
.###.#.#..#.#.....#..##....#.#.
..##.#....#.....##..#..........
##........#...#..#........###.#
#...#...........##.......#.#...
...###.....##.#....#...#...#...
......#....#.#.......###....#..
...#...#.......##.......###.#..
..............#.#..........##..
#.#....###..#..#.........#.....
.###.#.......#.....#....#.#....
.....###...#.#..#.#.......#....
.........#.##.#......#.#..#....
.......#....#....#.#....#..##.#
...............#...##.#..#.#..#
.....##........#..##...........
.##.#..#....#..#.#...#.........
.#.#..##.#..#......#....#.#...#
##....#.......##...........#...
..#...#.............#.#....#..#
..#......#..#.....#...##.....#.
....##...#.#...##...#..##......
.....#..#..........#...........
..##....#..#.#....#..#........#
.###....#.....#.#....#..##.....
#.......##.......#..#..#....#.#
.##..#...........#..##..##..#..
.#.................#...#....#..
.######.......#............##..
.#.........#......##.#.#.#.#.#.
.#.......#...#...#....###....#.
....#...##.#.#...#.....#.#..#..
.#..#..#...#.....###....#......
...#.##.###........#.....##....
..#....#.#.#..........#..#..#..
......#.....#...#..#..##..#.#..
#.#.......##.......#....#.....#
..#...#..#.#....#.##.##........
..#....#..##..#..##......#.....
#....#..##.....#....###........
##...#......#..###.#.....#.....
#..###....#...#...#...#......##
.....###....#......#..#..#...#.
.##......#.......##...#........
....#.#.....##.....#.....#.....
...##.#.....#..##...#...##.#...
..#...#.#....#....#...##.......
......#....#..#....#.#.........
..........#.#.#...##....#......
...#....................#..#...
...#....###....#..#.....#.....#
..#....#....#..#.#..##.#...#...
..#.##....##.....#.#........#..
#.....###..#.#.#...#..#....#...
........#..#.#..#........##....
.##....#................##.#.##
..##...#.#.#.....##..#....#....
....#..#....#..#........#..##..
...#...##....#....#..##......#.
##........#...#.....#.....#...#
.#......#....##...#.........##.
##........#...#.....#..#...#.#.
...##..#..#.....#..###.#..#....
....#..#..............#.......#
.......#.##...#......#.###.....
#........##..##....#.#.#.......
#.#..##.#.......#..##.....###..
.....##...#..#.....#...........
...#..#..#......#...#.#........
.#....#....#.#.....#.....#....#
...#..#...#..#.##.#......#.#.#.
..##....#..#..#.....#....#....#
...#....#.##.#..#.###......#...
.......#..#.....#.......#..#...
..###.#####..#..##.#.........#.
...#.......##...#.#..#.#......#
....#...#.###..#..........#....
...........#...#..##........#..
.......#...#....#....#.#..#....
.........#..........#...#....##
.##.........##..#.......##.#...
........#......###...##...#.#.#
#.#...##.##...........#...#.#..
.....###...#..##......#..#.....
#.#.....#.#....##..........#..#
#..#.......#.#.........####....
#.#...#.....#........#.....#..#
.....#..#.#.###.....#.#.###....
.###..#......##..#..#..........
#....#.#......#...#.##......#..
..#.........##.#.....#.........
...#....#.....##.#..#..##.#..#.
##.....#.#..#.#....#......#....
....###.#.....#.......#..#.#...
#.....##.....##...........#....
..........#..#......#.##...#...
#...#.###....##....#.###..###..
##........#.#...#..#.........#.
##........##.......#.....###...
.##....###........#..##...#...#
......#..##....##.....#..#.#...
.....#..##..#.......#.......#..
......#....#.......##.#........
.#.####.#..#......#..#.........
.##..#....#...##.#....#....#...
..#..#..#####.........#...#....
....#.....#.#.#.#...#.#......#.
....#...#.#..#.##...#...#......
..#...#...#...#...#..#.#.##..#.
..#......#.#.#.##.##.##..#.....
#..###......#.##...#....#.##.#.
.#.#.......##..##....##...##.#.
.##......##....##.#.......#...#
..#...#...................#....
.#...#.......######.....#.#..##
......#.##.....#.#.............
...........##.#........#..#....
#.............#.#.....#....##..
#...........#...#..###.....#...
....#.......#.#..#..#.#........
......#...##.......#..##....#..
......#.##.##..#........#.#...#
.#..#...##...................#.
.#.............#...#.#.#.#...#.
.........#.....#........#.#....
#..#...#.............##.#.....#
...#.#....#...##............#..
..#...#.##.###.#.....#......##.
...#.#..###...#.#............#.
...#....#........#.#...........
.#......#.#.#.........#.#....#.
....#..#......#.##.....#.#.....
..#..###....#....#.........###.
#..#.#....##.#....#.##..#......
#..#.....#.#.....##..#.##......
......#...#.#.............#..#.
#.#....#.#..#...#......#.#.....
..#.........#.#....#...#.......
.#..#.#...#....#...#......#...#
.......#........#.#..#..#...#..
..##.#......#..##.##.#..#..#...
.##...#....##.....#.....#...##.
#.....##.#....#.#......##..#...
.......#.#..#...#.......#.#...#
..#...#.......#...#..##........
#....##..#...#..#.#......#..#.#
##.#....#....#....#...#..#.##..
###........#.#..#..#......#....
.#......#.....#....#.#..#...#..
.#.....#.....#...##.......#..##
#..##.#..#..........#..........
...#.##.........#.#.##.#.......
.#..#...............#...#.#.#..
.....#.#.....#...####..#.....#.
.#....#.##..##...#...##.#...#.#
....#......##...#.#.#.....#.##.
#...#..#.#...#.#.....##...#....
..#..#....##..###......#..#....
.........#......##.....##....#.
.......#....#...#........###...
.....#..#..#...#...#......#....
..#..#...#.....#.....###..#.###
............#.#..#..#....#.....
...#..#...###.......#.......#..
#.........#........#.....##....
.#.#........#.....#........###.
....#.##.#...#.#.#.....#....#..
.##...#..#.......#.#...........
##...#.##...#...........#.....#
##....#.#.....##..#.......#....
##....#...#....#..#.......####.
......#...#..#.....#.#....#...#
.......#.....#..###............
#.#.#..#.....#.............#..#
.#..#.....##.....#...#.......##
..#.##........##...........#.#.
....##.#..###.#.........#...##.