Solve AOC day 3 pt 1
This commit is contained in:
@@ -9,7 +9,10 @@
|
|||||||
(load "day2.kiss")
|
(load "day2.kiss")
|
||||||
(assert (= 2150351 (apply * (simulateSubCommands (readSubCommands "src/year2021/inputs/day2.txt")))))
|
(assert (= 2150351 (apply * (simulateSubCommands (readSubCommands "src/year2021/inputs/day2.txt")))))
|
||||||
(assert (= 1842742223 (apply * (simulateSubCommands (readSubCommands "src/year2021/inputs/day2.txt") true)))))
|
(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 4)
|
||||||
(dayTodo 5)
|
(dayTodo 5)
|
||||||
(dayTodo 6)
|
(dayTodo 6)
|
||||||
|
28
projects/aoc/src/year2021/day3.kiss
Normal file
28
projects/aoc/src/year2021/day3.kiss
Normal 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))
|
1000
projects/aoc/src/year2021/inputs/day3.txt
Normal file
1000
projects/aoc/src/year2021/inputs/day3.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user