aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Day20/Program.fs
blob: 9453a8d0c412efc2ca6c42d383e8b324b3f03cf5 (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
40
41
42
43
44
45
46
47
module Day20

open System.IO
open FSharpPlus.Math.Generic
open Common

let mix listWithIds =
    let cycle = List.length listWithIds - 1

    let move xs idToMove =
        let oldIndex = List.findIndex (fst >> (=) idToMove) xs
        let element = xs[oldIndex]
        let newIndex = int <| remE (int64 oldIndex + snd element) cycle

        xs
        |> List.removeAt oldIndex
        |> List.insertAt newIndex element

    seq { 0..cycle } |> Seq.fold move listWithIds

let nthAfterZero n xs =
    xs
    |> List.item (remE (List.findIndex (snd >> (=) 0L) xs + n) (List.length xs))
    |> snd

let groveCoords multiplier rounds input =
    let mixed =
        input
        |> Seq.map (int64 >> (*) multiplier)
        |> Seq.indexed
        |> List.ofSeq
        |> Util.composition rounds mix

    nthAfterZero 1000 mixed
    + nthAfterZero 2000 mixed
    + nthAfterZero 3000 mixed

let solution1 = groveCoords 1 1
let solution2 = groveCoords 811589153 10

let test = File.ReadLines("test.txt")
assert (solution1 test = 3)
assert (solution2 test = 1623178306)

let input = File.ReadLines("input.txt")
printfn "%d" <| solution1 input
printfn "%d" <| solution2 input