-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2017_08.res
128 lines (108 loc) · 3.18 KB
/
Solution_2017_08.res
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
let data = Node.Fs.readFileSync("data/2017_08_real.txt", #utf8)
type register = Register(string)
type operation =
| Increase
| Decrease
exception ParsingError({input: string})
let parseInt = (s) => {
switch Belt.Int.fromString(s) {
| Some(x) => x
| None => raise(ParsingError({input: s}))
}
}
let parseOperation = (s) => {
switch s {
| "inc" => Increase
| "dec" => Decrease
| _ => raise(ParsingError({input: s}))
}
}
type comparisonOperand =
| Equals
| NotEquals
| LessThan
| LessThanOrEquals
| GreaterThan
| GreaterThanOrEquals
let parseOperand = (s) => {
switch s {
| "==" => Equals
| "!=" => NotEquals
| "<" => LessThan
| "<=" => LessThanOrEquals
| ">" => GreaterThan
| ">=" => GreaterThanOrEquals
| _ => raise(ParsingError({input: s}))
}
}
type instruction = {
register: register,
operation: operation,
howMuch: int,
comparisonRegister: register,
comparisonOperand: comparisonOperand,
compareWith: int,
}
let parseInstruction = (s) => {
let c = Js.String.split(" if ", s)
let a = Js.String.split(" ", c[0])
let b = Js.String.split(" ", c[1])
switch (a, b) {
| ([r, o, hm], [cr, co, cw]) => {
register: Register(r),
operation: parseOperation(o),
howMuch: parseInt(hm),
comparisonRegister: Register(cr),
comparisonOperand: parseOperand(co),
compareWith: parseInt(cw),
}
| _ => raise(ParsingError({input: s}))
}
}
let applyInstruction = (m, i) => {
let register = Belt.Map.getWithDefault(m, i.register, 0)
let comparisonRegister = Belt.Map.getWithDefault(m, i.comparisonRegister, 0)
let co = switch i.comparisonOperand {
| Equals => \"=="
| NotEquals => \"!="
| LessThan => \"<"
| LessThanOrEquals => \"<="
| GreaterThan => \">"
| GreaterThanOrEquals => \">="
}
let o = switch i.operation {
| Increase => \"+"
| Decrease => \"-"
}
let b = co(comparisonRegister, i.compareWith)
if b {
let newValue = o(register, i.howMuch)
Belt.Map.set(m, i.register, newValue)
} else {
m
}
}
let maxValue = (m) => {
let v = Belt.Map.valuesToArray(m)
Js.Math.maxMany_int(v)
}
let applyInstruction2 = ((m, highest), i) => {
let newM = applyInstruction(m, i)
let newHighest = maxValue(newM)
let max = Js.Math.max_int(highest, newHighest)
(newM, max)
}
let instructionStrings = Js.String.split("\n", data)
let instructions = Belt.Array.map(instructionStrings, parseInstruction)
module RegisterCmp = Belt.Id.MakeComparable({
type t = register
let cmp = (a, b) => Pervasives.compare(a, b)
})
let emptyRegisters = Belt.Map.make(~id=module(RegisterCmp))
let resultingRegisters = Js.Array2.reduce(instructions, applyInstruction, emptyRegisters)
let part1 = maxValue(resultingRegisters)
Js.log(`Part 1: ${Belt.Int.toString(part1)}`)
assert(part1 == 6061)
let (_, part2) = Js.Array2.reduce(instructions, applyInstruction2, (emptyRegisters, 0))
Js.log(`Part 2: ${Belt.Int.toString(part2)}`)
assert(part1 == 6696)