-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay16.hs
45 lines (35 loc) · 1014 Bytes
/
Day16.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Main where
import Utilities
type Bits = [Bool]
stringToBits :: String -> Bits
stringToBits = map (== '1')
bitsToString :: Bits -> String
bitsToString bs = [if b then '1' else '0' | b <- bs]
expansion :: Bits -> Bits
expansion bs = bs ++ concat (zipWith (:) bits (cycle [bs', bs]))
where
bs' = reverse (map not bs)
-- A038189 (ones in positions A091067)
bits :: Bits
bits = [oddPart n `mod` 4 == 3 | n <- [1..]]
oddPart :: Int -> Int
oddPart = fst . twos
-- (n, k) such that n is zero or odd and n*2^k == x
twos :: Int -> (Int, Int)
twos 0 = (0, 0)
twos x = splitPower 0 x
where
splitPower k n
| k `seq` odd n = (n, k)
| otherwise = splitPower (k+1) (n `div` 2)
solve :: Int -> String -> String
solve size = bitsToString .
take n . times k (pairWith (==)) . expansion . stringToBits
where
(n, k) = twos size
main :: IO ()
main = do
s <- readFile "input/16.txt"
let input = head (lines s)
putStrLn (solve 272 input)
putStrLn (solve 35651584 input)