summary refs log tree commit diff stats
path: root/day16.fsx
blob: adc57ca22ebf8c949c8441aa394556e68a60e5d9 (plain) (blame)
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
let starter = "01000100010010111"

let revStr (str: string) =
    str 
    |> Seq.toArray 
    |> Array.rev 
    |> Array.map string
    |> String.concat ""

let logical_not str =
    String.map (function | '1' -> '0' | '0' -> '1' | _ -> char 0) str

let rec dragon targetLength inputStr =
    let newStr = inputStr + "0" + (inputStr |> revStr |> logical_not)
    if newStr.Length >= targetLength 
    then newStr[..targetLength-1]
    else dragon targetLength newStr

let rec checksum (inputStr: string): string =
    let parity (chars: char[]): bool =
        let a, b = chars[0], chars[1]
        a = b
    if inputStr.Length % 2 <> 0 
    then inputStr
    else
        let pairs = Seq.chunkBySize 2 inputStr 
                    |> Seq.map parity 
                    |> Seq.map (fun x -> if x then "1" else "0")
                    |> String.concat ""
        checksum pairs

// part 1
starter |> dragon 272 |> checksum |> printfn "%s"
// part 2
starter |> dragon 35651584 |> checksum |> printfn "%s"