Completely change naming conventions of field forms and definition macros. Close #32

This commit is contained in:
2021-07-24 14:22:10 -06:00
parent 9d6a4b2054
commit f5a5cdfb40
67 changed files with 433 additions and 447 deletions

View File

@@ -1,6 +1,6 @@
(load "UtilMacros.kiss")
(defun :Void main []
(function :Void main []
(year 2018
(Solutions2018.run))
(year 2020
(Solutions2020.run)))
(Solutions2020.run)))

View File

@@ -1,4 +1,4 @@
(defun readLines [file]
(function readLines [file]
(.filter
(.map
// TODO implement escape sequences in kiss string literals
@@ -6,7 +6,7 @@
StringTools.trim)
(lambda [l] (< 0 l.length))))
(defun readParagraphLines [file]
(function readParagraphLines [file]
(.filter
(for paragraph
(.split
@@ -18,7 +18,7 @@
(lambda [lines] (< 0 lines.length))))
// TODO won't need to specify type here if last is not a quickNth
(defun :kiss.List<Int> readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt)))
(function :kiss.List<Int> readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt)))
(defun countChar [char str]
(count (str.split "") (lambda [c] ?(= c char))))
(function countChar [char str]
(count (str.split "") (lambda [c] ?(= c char))))

View File

@@ -1,6 +1,6 @@
(load "../UtilMacros.kiss")
(defun run []
(function run []
(day 1
(let [inputs
(.slice (.split (sys.io.File.getContent "src/year2018/inputs/day1.txt") "\n") 0 -1)
@@ -31,4 +31,4 @@
(dayTodo 22)
(dayTodo 23)
(dayTodo 24)
(dayTodo 25))
(dayTodo 25))

View File

@@ -1,7 +1,7 @@
(defun differences [:kiss.List<Int> ratings]
(function differences [:kiss.List<Int> ratings]
(for pair (pairs ratings) (- 0 (apply - pair))))
(defun distribution [:kiss.List<Int> numbers]
(function distribution [:kiss.List<Int> numbers]
(let [:Map<Int,Int> dist (new Map)]
(doFor num numbers
(dictSet dist num
@@ -10,7 +10,7 @@
1)))
dist))
(defun &dynamic arrangementCount [:kiss.List<Int> ratings startingIndex]
(function &dynamic arrangementCount [:kiss.List<Int> ratings startingIndex]
(if (= startingIndex (- ratings.length 1)) 1
(let [&mut :Int64 sum 0
startingRating (nth ratings startingIndex)

View File

@@ -1,7 +1,7 @@
(defun bagColor [:String bag]
(function bagColor [:String bag]
(bag.substr 0 (Math.floor (- (bag.indexOf "bag") 1))))
(defun parseRule [:String line :ParentMap parentMap :ChildMap childMap]
(function parseRule [:String line :ParentMap parentMap :ChildMap childMap]
(unless (<= 0 (line.indexOf "contain no other bags"))
(let [[containerStr contents] (line.split "contain ")
contentStrs (contents.split ", ")]
@@ -15,17 +15,17 @@
(.push (dictGet childMap (bagColor colorStr)) (bagColor containerStr))))
(dictSet parentMap (bagColor containerStr) innerMap))))
(defun findIndirectContainers [color :ChildMap childMap :Map<String,Bool> outMap]
(function findIndirectContainers [color :ChildMap childMap :Map<String,Bool> outMap]
(when (childMap.exists color)
(doFor parentColor (dictGet childMap color)
(dictSet outMap parentColor true)
(findIndirectContainers parentColor childMap outMap))))
(defun totalChildBags [bag :ParentMap parentMap]
(function totalChildBags [bag :ParentMap parentMap]
(if (parentMap.exists bag)
(begin
(localVar &mut sum 0)
(doFor =>childColor quantity (dictGet parentMap bag)
(set sum (+ sum quantity (* quantity (totalChildBags childColor parentMap)))))
sum)
0))
0))

View File

@@ -1,9 +1,9 @@
(defprop &mut accumulator 0)
(prop &mut accumulator 0)
(defmethod setBreakPoint [] (addBreakPoint instructionPointer))
(method setBreakPoint [] (addBreakPoint instructionPointer))
(defmethod nop [v :Dynamic self] (self.setBreakPoint))
(defmethod acc [v :Dynamic self] (self.setBreakPoint) (set self.accumulator (+ self.accumulator v)))
(defmethod jmp [v :Dynamic self]
(method nop [v :Dynamic self] (self.setBreakPoint))
(method acc [v :Dynamic self] (self.setBreakPoint) (set self.accumulator (+ self.accumulator v)))
(method jmp [v :Dynamic self]
(self.setBreakPoint)
(set self.instructionPointer (+ self.instructionPointer (- v 1))))
(set self.instructionPointer (+ self.instructionPointer (- v 1))))

View File

@@ -1,8 +1,8 @@
(load "BootCodeCommon.kiss")
(defvar :Map<Int,Bool> instructionsTested (new Map<Int,Bool>))
(defprop &mut forked false)
(defprop &mut forkedAt -1)
(var :Map<Int,Bool> instructionsTested (new Map<Int,Bool>))
(prop &mut forked false)
(prop &mut forkedAt -1)
(defreadermacro ["jmp" "nop"] [stream]
(let [inst
@@ -35,4 +35,4 @@
])))))
// Define the default reader LAST because default readers tend to break everything
(load "BootCodeDSL.kiss")
(load "BootCodeDSL.kiss")

View File

@@ -1,14 +1,14 @@
(defun countAnyYes [:Array<String> group]
(function countAnyYes [:Array<String> group]
(countWhereYes group (lambda [c] (< 0 c))))
(defun countAllYes [:Array<String> group]
(function countAllYes [:Array<String> group]
(countWhereYes group (lambda [c] (= c group.length))))
(defun countWhereYes [:Array<String> group predicate]
(function countWhereYes [:Array<String> group predicate]
(let [yesDict (new Map<String,Int>)]
(doFor person group
(doFor question (person.split "")
(dictSet yesDict question
(+ 1
(if (yesDict.exists question) (dictGet yesDict question) 0)))))
(count yesDict predicate)))
(count yesDict predicate)))

View File

@@ -1,27 +1,27 @@
(defprop &mut x 0)
(defprop &mut y 0)
(prop &mut x 0)
(prop &mut y 0)
// 0 is east
// 1 is south
// 2 is west
// 3 is north
(defprop &mut facing 0)
(prop &mut facing 0)
(defun fixFacing [f]
(function fixFacing [f]
(Math.floor (% (if (> 0 f) (+ 4 f) f) 4)))
(defmethod N [num]
(classMethod N [num]
(set y (+ y num)))
(defmethod S [num]
(classMethod S [num]
(set y (- y num)))
(defmethod E [num]
(classMethod E [num]
(set x (+ x num)))
(defmethod W [num]
(classMethod W [num]
(set x (- x num)))
(defmethod R [angle]
(classMethod R [angle]
(set facing (fixFacing (+ facing (/ angle 90)))))
(defmethod L [angle]
(classMethod L [angle]
(set facing (fixFacing (- facing (/ angle 90)))))
(defmethod F [num]
(classMethod F [num]
(case facing
(0 (E num))
(1 (S num))
@@ -35,4 +35,4 @@
null
`(,(ReaderExp.Symbol
(stream.expect "a ship command" (lambda [] (stream.takeChars 1))))
,(ReaderExp.Symbol (stream.expect "a number argument" (lambda [] (stream.takeUntilAndDrop #|"\n"|#)))))))
,(ReaderExp.Symbol (stream.expect "a number argument" (lambda [] (stream.takeUntilAndDrop #|"\n"|#)))))))

View File

@@ -1,6 +1,6 @@
(defun :FerrySquare floor [:Array<FerrySquare> n :SeatsChanged changed] floor)
(function :FerrySquare floor [:Array<FerrySquare> n :SeatsChanged changed] floor)
(defun :FerrySquare emptySeat [:Array<FerrySquare> n :SeatsChanged changed]
(function :FerrySquare emptySeat [:Array<FerrySquare> n :SeatsChanged changed]
// Empty seats with completely empty neighbors, fill up
(cond
((= true (apply = (for neighbor n #|neighbor != fullSeat|#)))
@@ -8,7 +8,7 @@
fullSeat)
(true emptySeat)))
(defun :FerrySquare fullSeat [:Array<FerrySquare> n :SeatsChanged changed]
(function :FerrySquare fullSeat [:Array<FerrySquare> n :SeatsChanged changed]
// Full seats with 4 or more full neighbors become empty
(cond
((<= 4 (count n (lambda [neighbor] #|neighbor == fullSeat|#)))
@@ -16,7 +16,7 @@
emptySeat)
(true fullSeat)))
(defun neighbors [x y :Array<Array<FerrySquare>> grid]
(function neighbors [x y :Array<Array<FerrySquare>> grid]
(localVar &mut n [])
(doFor xx (range (- x 1) (+ x 2))
(doFor yy (range (- y 1) (+ y 2))
@@ -25,9 +25,9 @@
(n.push (nth (nth grid yy) xx))))))
n)
(defprop &mut :Array<Array<FerrySquare>> state [])
(prop &mut :Array<Array<FerrySquare>> state [])
(defmethod simulate []
(method simulate []
(localVar changed (object changed false))
(set state
(for rowIdx (range state.length)
@@ -35,10 +35,10 @@
(for seatIdx (range row.length) ((nth row seatIdx) (neighbors seatIdx rowIdx state) changed)))))
changed.changed)
(defmethod fullSimulate []
(method fullSimulate []
(when (simulate) (fullSimulate)))
(defmethod countFullSeats []
(method countFullSeats []
(apply +
(for :Array<FerrySquare> row state
(apply +
@@ -51,4 +51,4 @@
(undefreadermacro "...")
(defreadermacro &start "" [stream]
`(state.push ,(ReaderExp.ListExp (readExpArray stream #|"\n"|#))))
`(state.push ,(ReaderExp.ListExp (readExpArray stream #|"\n"|#))))

View File

@@ -1,4 +1,4 @@
(defun readPassport [:Stream stream &opt :Map<String,String> pp]
(function 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 ":")))
@@ -8,7 +8,7 @@
(begin (stream.dropWhitespace) pp)
(begin (stream.dropWhitespace) (readPassport stream pp))))
(defun checkPassport [:Map<String,String> pp strict]
(function checkPassport [:Map<String,String> pp strict]
(doFor key ["byr" "iyr" "eyr" "hgt" "hcl" "ecl" "pid"]
(if !(pp.exists key) (return false)))
(when strict
@@ -32,8 +32,8 @@
(unless (and (= 9 pid.length) (Std.parseInt pid)) (return false))))
(return true))
(defun countValidPassports [:Stream stream &opt strict c]
(function 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))))
(countValidPassports stream strict (if (checkPassport (readPassport stream) strict) (+ c 1) c))))

View File

@@ -1,17 +1,17 @@
(defun parsePasswordCheck1 [:String ruleStr]
(function 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]
(function 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]
(function validateInputLine [:String line ruleParser]
(let [[rule password]
(line.split ": ")]
((ruleParser rule) password)))
((ruleParser rule) password)))

View File

@@ -1,6 +1,6 @@
// Airplane seating
(defun search [:Array<String> letters min max rowOrColumn]
(function search [:Array<String> letters min max rowOrColumn]
(if (= min max)
min
(let [middle (Math.floor (/ (+ min max) 2))]
@@ -11,7 +11,7 @@
(["R" "column"] (search letters (+ 1 middle) max rowOrColumn))
(otherwise (throw "invalid search call"))))))
(defun seatId [:String boardingPass]
(function seatId [:String boardingPass]
(+
(* 8 (search (.split (boardingPass.substr 0 7) "") 0 127 "row"))
(search (.split (boardingPass.substr 7) "") 0 7 "column")))
(search (.split (boardingPass.substr 7) "") 0 7 "column")))

View File

@@ -1,6 +1,6 @@
(load "../UtilMacros.kiss")
(defun run []
(function run []
(day 1
(let [p (SummingTuples.pairWithSum 2020 [1721 979 366 299 675 1456])]
(assert (and (has p 1721) (has p 299)) "pairWithSum is broken"))
@@ -156,4 +156,4 @@
(dayTodo 22)
(dayTodo 23)
(dayTodo 24)
(dayTodo 25))
(dayTodo 25))

View File

@@ -1,4 +1,4 @@
(defun :kiss.List<Int> pairWithSum [sum :kiss.List<Int> numbers]
(function :kiss.List<Int> pairWithSum [sum :kiss.List<Int> numbers]
// Put the numbers in a map for random access. This gives an O(n) solution
(localVar :Map<Int,Int> numbersMap (new Map))
(doFor number numbers
@@ -8,7 +8,7 @@
(return [number requiredForPair]))))
null)
(defun :kiss.List<Int> trioWithSum [sum :kiss.List<Int> numbers]
(function :kiss.List<Int> trioWithSum [sum :kiss.List<Int> numbers]
(doFor number numbers
(let [requiredForTrio (- sum number)
pairThatSatisfies (pairWithSum requiredForTrio numbers)]
@@ -16,7 +16,7 @@
(return [number (nth pairThatSatisfies 0) (nth pairThatSatisfies 1)]))))
null)
(defun contiguousSumTuple [sum :kiss.List<Int> numbers]
(function contiguousSumTuple [sum :kiss.List<Int> numbers]
(doFor i (range numbers.length)
(localVar &mut testSum (nth numbers i))
(doFor j (range (+ i 1) numbers.length)
@@ -26,4 +26,4 @@
(return (numbers.slice i (+ j 1))))
((> testSum sum)
(break)))))
null)
null)

View File

@@ -1,8 +1,8 @@
(defun path [:kiss.List<String> hillTile x0 y0 dx dy]
(function 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) ""))
(function 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)))
(function pathTrees [:kiss.List<String> hillTile x0 y0 dx dy] (Util.countChar "#" (pathString hillTile x0 y0 dx dy)))

View File

@@ -1,4 +1,4 @@
(defun firstOffender [preambleLength :kiss.List<Int> input]
(function firstOffender [preambleLength :kiss.List<Int> input]
(doFor idx (range preambleLength input.length)
(unless (SummingTuples.pairWithSum (nth input idx) (input.slice (- idx preambleLength) idx))
(return (nth input idx))))