-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay08.fs
93 lines (75 loc) · 3.03 KB
/
Day08.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module Day08
// problem page
// http://adventofcode.com/2017/day/8
open System.Collections.Generic
open System.IO
let problemFileName = @"Data\08.txt"
let processFile (filePath : string) =
seq {
use fileReader = new StreamReader(filePath)
while not fileReader.EndOfStream do
let line = fileReader.ReadLine()
yield line.Split([|'\t';' '|])
};;
let lines = problemFileName |> processFile
type Statement =
| IfStmt of Expression * Statement
| Inc of Expression * Expression
| Dec of Expression * Expression
and Expression =
| Register of string
| Integer of int
| Eq of Expression * Expression
| NotEq of Expression * Expression
| Lt of Expression * Expression
| Gt of Expression * Expression
| Lte of Expression * Expression
| Gte of Expression * Expression
let toStatement (line : string[]) =
let modifiedRegister = Register (line.[0])
let conditionalRegister = Register (line.[4])
let arg1 = Integer(int(line.[2]))
let arg2 = Integer(int(line.[6]))
let changeStatement = match line.[1] with
| "inc" -> Inc (modifiedRegister, arg1)
| "dec" -> Dec (modifiedRegister, arg1)
let condExpr = match line.[5] with
| "==" -> Eq (conditionalRegister, arg2)
| "!=" -> NotEq (conditionalRegister, arg2)
| ">" -> Gt (conditionalRegister, arg2)
| "<" -> Lt (conditionalRegister, arg2)
| ">=" -> Gte (conditionalRegister, arg2)
| "<=" -> Lte (conditionalRegister, arg2)
IfStmt(condExpr, changeStatement)
let statements = lines |> Seq.map (toStatement)
let registers = Dictionary<string, int>()
let getRegister name =
if registers.ContainsKey name
then registers.Item(name)
else
registers.Add (name, 0)
0
let mutable maxHeldValue = 0
let changeRegister expr (value : int) =
match expr with
| Register (name) ->
if registers.ContainsKey name then registers.[name] <- registers.[name] + value else registers.Add (name, value)
if registers.[name] > maxHeldValue then maxHeldValue <- registers.[name]
let rec getExpression expr =
match expr with
| Eq (reg, arg) -> if getExpression reg = getExpression arg then 1 else 0
| NotEq (reg, arg) -> if getExpression reg <> getExpression arg then 1 else 0
| Gt (reg, arg) -> if getExpression reg > getExpression arg then 1 else 0
| Lt (reg, arg) -> if getExpression reg < getExpression arg then 1 else 0
| Gte (reg, arg) -> if getExpression reg >= getExpression arg then 1 else 0
| Lte (reg, arg) -> if getExpression reg <= getExpression arg then 1 else 0
| Register (name) -> getRegister name
| Integer (x) -> x
let rec calcStatement statement =
match statement with
| IfStmt (condExpr, changeExpr) -> if getExpression condExpr = 1 then calcStatement changeExpr
| Inc (reg, arg) -> changeRegister reg (getExpression arg)
| Dec (reg, arg) -> changeRegister reg -(getExpression arg)
statements |> Seq.iter (calcStatement)
registers |> Seq.maxBy (fun x -> x.Value)
maxHeldValue