This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
generated from mazharenko/aoc-agent-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.fs
48 lines (42 loc) · 1.56 KB
/
day04.fs
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
48
module impl.day04
open Farkle
open Farkle.Builder
type Card = { Winning: Set<int>; Our: Set<int> }
let parse input =
let number = Terminals.int "Number"
let numbers = many1 number
let card = "Card" ||= [
!& "Card" .>> number .>> ":" .>>.
numbers .>> "|" .>>. numbers
=> (fun winning our -> { Winning = Set.ofList winning; Our = Set.ofList our })
]
let parser = RuntimeFarkle.build card
let parseLine line =
RuntimeFarkle.parseUnsafe parser line
Pattern1.read parseLine input
let solve1 input =
input
|> Array.map (fun card -> Set.intersect card.Winning card.Our)
|> Array.map (fun matchingNumbers -> pown 2 (Set.count matchingNumbers - 1))
|> Array.sum
let rec private calculate2 (currentCount, list) =
match list with
| [] -> currentCount
// don't expect to have any matching numbers in the last game
| [(count, _)] -> count + currentCount
// having headCount current cards,
// determine matches,
// take matches number of the following cards,
// increase their number by headCount,
// repeat
| (headCount, head)::tail ->
let matches = Set.intersect head.Winning head.Our |> Set.count
let cardsToCopy, rest = List.splitAt matches tail
let newTail =
(cardsToCopy |> List.map(fun (count, card) -> count+headCount, card)) @ rest
(calculate2 (currentCount + headCount, newTail))
let solve2 (input : Card[]) : int =
input
|> Array.map (fun card -> 1,card)
|> Array.toList
|> (fun x -> calculate2 (0, x))