aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Day03/Program.fs
blob: f62d906af2de2cf5a07cc8bfc19544bd59eba241 (plain)
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
open System.IO

let priority item =
    if 'a' <= item && item <= 'z' then
        int item - int 'a' + 1
    elif 'A' <= item && item <= 'Z' then
        int item - int 'A' + 27
    else
        failwithf "Invalid item: %c" item

let cutInHalf xs =
    let half = Seq.length xs / 2
    [ Seq.take half xs; Seq.skip half xs ]

let solution1 =
    Seq.sumBy (
        cutInHalf
        >> Seq.map Set
        >> Set.intersectMany
        >> Seq.exactlyOne
        >> priority
    )

let solution2 =
    Seq.chunkBySize 3
    >> Seq.sumBy (
        Seq.map Set
        >> Set.intersectMany
        >> Seq.exactlyOne
        >> priority
    )

let test = File.ReadLines "test.txt"
assert (solution1 test = 157)
assert (solution2 test = 70)

let input = File.ReadAllLines "input.txt"
printfn "%d" <| solution1 input
printfn "%d" <| solution2 input