Solve AOC day 3 pt 1

This commit is contained in:
2021-12-03 15:45:00 -07:00
parent 224ea5f30a
commit 3a3d69d13a
3 changed files with 1032 additions and 1 deletions

View File

@@ -9,7 +9,10 @@
(load "day2.kiss")
(assert (= 2150351 (apply * (simulateSubCommands (readSubCommands "src/year2021/inputs/day2.txt")))))
(assert (= 1842742223 (apply * (simulateSubCommands (readSubCommands "src/year2021/inputs/day2.txt") true)))))
(dayTodo 3)
(day 3
(load "day3.kiss")
(let [inputs (Util.readLines "src/year2021/inputs/day3.txt")]
(assert (= 845186 (* (rate inputs false) (rate inputs true))))))
(dayTodo 4)
(dayTodo 5)
(dayTodo 6)

View File

@@ -0,0 +1,28 @@
// Silly but fun implementation of a binary->decimal conversion
(function :Int binaryStringToDecimal [:String binary &opt :Bool invert]
(apply +
(for [idx digit] (enumerate (reversed (binary.split "")))
(*
(let [digit (Std.parseInt digit)] (if invert (- 1 digit) digit))
(^ 2 idx)))))
(assert (= 22 (binaryStringToDecimal "10110")))
(assert (= 9 (binaryStringToDecimal "10110" true)))
(function :String mostCommonBit [:String binary]
(let [&mut ones 0.0
&mut total 0.0]
(doFor digit (binary.split "")
(+= total 1)
(case digit
("1" (+= ones 1))
(otherwise)))
(Std.string (Math.round (/ ones total)))))
(function :Int rate [:Array<String> inputs :Bool epsilon]
(binaryStringToDecimal
(.join
(for idx (range .length (first inputs))
(mostCommonBit (.join (for input inputs (input.charAt idx)) "")))
"")
epsilon))

File diff suppressed because it is too large Load Diff