aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Day03/Program.fs
blob: 512ef401fe8e73ec7c4184b27643bae34b3a26a5 (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
module Day03

open System.IO
open Common

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 solution1 =
    Seq.sumBy (
        Util.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