-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day21.fs
62 lines (45 loc) · 1.44 KB
/
Day21.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace Adventofcode2021
module Day21 =
[<Literal>]
let InputFile = "Day21Input.txt"
type Player = { Id: int; Pos: int; Score: int }
let parseLine (s: string) =
let words = s.Split(' ')
{ Id = int words[1]
Pos = int words[4]
Score = 0 }
type Dice = { Rolls: int; Last: int }
let rollDice dice =
let e1 = dice.Last + 1
let e2 = dice.Last + 2
let e3 = dice.Last + 3
let dice' = { Rolls = dice.Rolls + 3; Last = e3 }
(dice', e1 + e2 + e3)
let calcPos p toMove =
let p' = ((p + toMove) % 10)
if p' = 0 then 10 else p'
let makeRound player dice =
let (dice', r) = rollDice dice
let pos = calcPos player.Pos r
let player' =
{ player with
Score = player.Score + pos
Pos = pos }
(player', dice')
let rec game p1 p2 dice =
let (p1', dice') = makeRound p1 dice
if p1'.Score >= 1000 then
p2.Score * dice'.Rolls
else
let (p2', dice'') = makeRound p2 dice'
if p2'.Score >= 1000 then
p1'.Score * dice''.Rolls
else
game p1' p2' dice''
let day21 () =
let dice = { Rolls = 0; Last = 0 }
let players =
InputFile
|> System.IO.File.ReadAllLines
|> Array.map parseLine
game players[0] players[1] dice