-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
46 lines (37 loc) · 1.08 KB
/
run.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
46
{-# LANGUAGE BangPatterns #-}
import AoC
import Data.Bits (xor)
import Data.Bool (bool)
import System.IO (hSetBuffering, stdout, BufferMode(NoBuffering))
dragon :: [Bool] -> [Bool]
dragon a =
let b = map not $ reverse a
c = a ++ [False] ++ b
in c
bits :: [Bool] -> String
bits = map (bool '0' '1')
unbits :: String -> [Bool]
unbits = map f
where f '0' = False
f '1' = True
fill :: Int -> [Bool] -> [Bool]
fill len start =
let n = ceiling (logBase 2 $ (fromIntegral (len + 1) / fromIntegral (length start + 1))) - 1
a = iterateN n dragon start
m = len - (2^n * (length start + 1))
b = map not $ take m $ reverse a
in a ++ [False] ++ b
checksum :: Int -> [Bool] -> [Bool]
checksum len bs
| odd len = bs
| otherwise =
let go (x:y:rest) = (not $ x `xor` y):(go rest)
go [] = []
in checksum (len `div` 2) (go bs)
solve len start = bits . checksum len $ fill len (unbits start)
part1 = solve 272 "01111010110010011"
part2 = solve 35651584 "01111010110010011"
main = do
hSetBuffering stdout NoBuffering
putStrLn part1
putStrLn part2