-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.fs
68 lines (53 loc) · 1.25 KB
/
day24.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
63
64
65
66
67
68
module AoC2021.Day24
open AoC2021.Utils
type Register =
| W
| X
| Y
| Z
type Input = { Data: int64 array; Ptr: int }
type Argument =
| N of int64
| R of Register
let stor (mem: int64 array) r v =
match r with
| W -> mem.[0] <- v
| X -> mem.[1] <- v
| Y -> mem.[2] <- v
| Z -> mem.[3] <- v
let load (mem: int64 array) r =
match r with
| W -> mem.[0]
| X -> mem.[1]
| Y -> mem.[2]
| Z -> mem.[3]
let inp mem r data =
stor mem r data.Data.[data.Ptr]
{ data with Ptr = data.Ptr + 1 }
let operation op mem r arg =
let v =
match arg with
| N n -> n
| R r' -> load mem r'
stor mem r (op (load mem r) v)
let add = operation (+)
let mul = operation (*)
let div = operation (/)
let mod' = operation (%)
let equals a b =
match a = b with
| false -> 0L
| true -> 1L
let eql = operation equals
let day24 fn () =
let input = readInput fn
let serialNumbers =
Seq.unfold
(fun i ->
if i <= 11_111_111_111_111L then
None
else
Some(string i |> Seq.map charToL |> List.ofSeq, i - 1L))
100_000_000_000_000L
|> Seq.filter (List.contains 0L >> not)
0L