aboutsummaryrefslogtreecommitdiff
path: root/aoc-2022-dotnet/Day20/Program.fs
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-27 23:15:58 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2022-12-27 23:15:58 +0100
commit4e6b4839f632cbd843da0f5fe39db00edc16f94c (patch)
tree4d3805a369cd945b56ae2b14dbea5e1a3c45ba62 /aoc-2022-dotnet/Day20/Program.fs
parent1d5b4ebc2683a4bc085fd0cfd23da524f32334d0 (diff)
downloadgleam_aoc2020-4e6b4839f632cbd843da0f5fe39db00edc16f94c.tar.gz
gleam_aoc2020-4e6b4839f632cbd843da0f5fe39db00edc16f94c.zip
Finish day 20
Diffstat (limited to 'aoc-2022-dotnet/Day20/Program.fs')
-rw-r--r--aoc-2022-dotnet/Day20/Program.fs47
1 files changed, 47 insertions, 0 deletions
diff --git a/aoc-2022-dotnet/Day20/Program.fs b/aoc-2022-dotnet/Day20/Program.fs
new file mode 100644
index 0000000..9453a8d
--- /dev/null
+++ b/aoc-2022-dotnet/Day20/Program.fs
@@ -0,0 +1,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