Login
7 branches 0 tags
Ben (Win10) Moved more over to use bytecode 32beda8 3 years ago 561 Commits
nujel / tests / fast / day3.nuj
#!/usr/bin/env nujel

[def example-data "00100\n11110\n10110\n10111\n10101\n01111\n00111\n11100\n10000\n11001\n00010\n01010"]
[def input-data [file/read "tests/fast/day3.dat"]]

[defn read-data [lines]
       [map [split lines "\n"]
            [fn [line]
                [car [read [cat "#b" line]]]]]]

[defn count-bits/single [number state bit-count]
       [def i 0]
       [while [< i bit-count]
              [def mask [ash 1 i]]
              [when [zero? [logand number mask]]
                    [array/set! state i [+ 1 [ref state i]]]]
              [++ i]]]

[defn count-bits [numbers bit-count]
       [def count 0]
       [def state [array/allocate bit-count]]
       [array/fill! state 0]
       [while numbers
              [count-bits/single [car numbers] state bit-count]
              [cdr! numbers]
              [++ count]]
       @[:bit-count bit-count :count count :zeroes state]]

[defn calc-γε [state]
       [def i 0]
       [def γ 0]
       [def ε 0]
       [def threshold [ash [ref state :count] -1]]
       [def arr [ref state :zeroes]]
       [while [< i [ref state :bit-count]]
              [if [< [ref arr i] threshold]
                  [set! γ [logior γ [ash 1 i]]]
                  [set! ε [logior ε [ash 1 i]]]]
              [++ i]]

       @[:γ γ :ε ε :power-consumption [* ε γ]]]

[def result [calc-γε [count-bits [read-data example-data] 5]]]
[when [!= [ref result :power-consumption] 198]
      [throw [list :wrong-result "Wrong result" result]]]

[def result [calc-γε [count-bits [read-data input-data] 12]]]
[when [!= [ref result :power-consumption] 4103154]
      [throw [list :wrong-result "Wrong result" result]]]