summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-02-14 14:47:31 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-02-14 14:47:31 -0800
commitd520f8941d8fb63e70d19ea5bbc9246641ef7593 (patch)
tree59485b4e0737153c4a26093fe0127b413ae6d3aa
parent45a68a69c63e5c329a19c6fce47e1315504ea76d (diff)
downloadAdventOfCode2017-d520f8941d8fb63e70d19ea5bbc9246641ef7593.tar.gz
solutions up to day 9
-rw-r--r--day6.ml41
-rw-r--r--day7.ml90
-rw-r--r--day8.fsx60
-rw-r--r--day9.fsx25
4 files changed, 216 insertions, 0 deletions
diff --git a/day6.ml b/day6.ml
new file mode 100644
index 0000000..10d5e63
--- /dev/null
+++ b/day6.ml
@@ -0,0 +1,41 @@
+#use "topfind";;
+#thread;;
+#require "core";;
+#require "stdio";;
+
+open Core
+open Stdio
+
+let memory = In_channel.read_all "day6.txt"
+             |> String.strip |> String.split ~on:'\t'
+             |> Array.of_list |> Array.map ~f:int_of_string
+
+let len = Array.length memory
+let seen = Caml.Hashtbl.create 5000
+
+let array_max xs =
+  let f index (maxInd, maxVal) value =
+    if value > maxVal then (index, value) else (maxInd, maxVal) in
+  Array.foldi ~init:(0, 0) ~f:f xs
+
+let rec redist xs n i =
+  match n with
+  | 0 -> ()
+  | _ ->
+     let pos = (i + 1) mod len in
+     xs.(pos) <- xs.(pos) + 1;
+     redist xs (n-1) pos
+
+let rec cycle count banks =
+  match Caml.Hashtbl.find_opt seen banks with
+  | Some x ->
+     Printf.printf "%d\n" count;
+     Printf.printf "%d\n" (count - x)
+  | None ->
+     Caml.Hashtbl.add seen (Array.copy banks) count;
+     let mi, mv = array_max banks in
+     banks.(mi) <- 0;
+     redist banks mv mi;
+     cycle (count+1) banks
+
+let () = cycle 0 memory
diff --git a/day7.ml b/day7.ml
new file mode 100644
index 0000000..498d732
--- /dev/null
+++ b/day7.ml
@@ -0,0 +1,90 @@
+#use "topfind";;
+#require "containers";;
+#load "str.cma";;
+
+module StrMap = Map.Make(String)
+module StrSet = Set.Make(String)
+module IntSet = Set.Make(Int)
+module Regex = Str
+
+let r = Regex.regexp {|\([a-z]+\) (\([0-9]+\))\($\| -> \(.+\)\)|}
+
+let parse_row s =
+  if Regex.string_match r s 0 then
+    let name = Regex.matched_group 1 s in
+    let weight = Regex.matched_group 2 s |> int_of_string in
+    let kids =
+      if Regex.matched_group 3 s = "" then []
+      else CCString.split ~by:", " (Regex.matched_group 4 s)
+    in
+    (name, weight, kids)
+  else ("", 0, [])
+
+let input = List.map parse_row CCIO.(with_in "day7.txt" read_lines_l)
+
+let create_map f =
+  List.fold_left f StrMap.empty
+let get_weights =
+  create_map (fun a (n, w, _) -> StrMap.add n w a)
+let get_relations =
+  create_map (fun a (n, _, k) -> StrMap.add n k a)
+
+let weights = get_weights input
+let relations = get_relations input
+
+
+let rec total_node_weight node =
+  let own_weight = StrMap.find node weights in
+  let kids = StrMap.find node relations in
+  let kids_weights = List.map total_node_weight kids in
+  own_weight + List.fold_left (+) 0 kids_weights
+
+
+let find_different_weights kids =
+  if CCList.is_empty kids then None
+  else
+    let kids_weights = List.map total_node_weight kids in
+    let w_set = IntSet.of_list kids_weights in
+    if IntSet.cardinal w_set = 1 then None
+    else
+      let min_w = IntSet.min_elt w_set in
+      let max_w = IntSet.max_elt w_set in
+      let count v =
+        kids_weights
+        |> List.filter (fun w -> w = v)
+        |> List.length in
+      let min_count = count min_w in
+      let max_count = count max_w in
+      let (wrong_w, right_w) =
+        if min_count < max_count then (min_w, max_w) else (max_w, min_w) in
+      let wrong_i, _ =
+        CCList.find_idx (fun x -> x = wrong_w) kids_weights
+        |> Option.value ~default:(0, 0) in
+      let wrong_kid = List.nth kids wrong_i in
+      Some (wrong_kid, right_w)
+
+
+let rec find_correct_weight node right_w =
+  let kids = StrMap.find node relations in
+  match find_different_weights kids with
+  | Some (wrong_kid, right_w) ->
+    find_correct_weight wrong_kid right_w
+  | None ->
+    let kids_weights = List.map total_node_weight kids in
+    let kids_total_w = List.fold_left (+) 0 kids_weights in
+    right_w - kids_total_w
+
+
+let create_set f =
+  List.fold_left f StrSet.empty
+let get_nodes =
+  create_set (fun a (n, _, _) -> StrSet.add n a)
+let get_kids =
+  create_set (fun a (_, _, k) -> StrSet.union a (StrSet.of_list k))
+
+let nodes = get_nodes input
+let kids = get_kids input
+let root = StrSet.diff nodes kids |> StrSet.choose
+
+let part_1 = root |> Printf.printf "%s\n"
+let part_2 = find_correct_weight root 0 |> Printf.printf "%d\n"
diff --git a/day8.fsx b/day8.fsx
new file mode 100644
index 0000000..5a4f423
--- /dev/null
+++ b/day8.fsx
@@ -0,0 +1,60 @@
+open System.IO
+open System.Text.RegularExpressions
+open System.Collections.Generic
+
+let (|InstRegex|_|) input =
+    let m = Regex.Match(input, "(\w+) (dec|inc) (-?\d+) if (\w+) (>|<|>=|<=|==|!=) (-?\d+)")
+    if m.Success then
+        let op = if m.Groups[2].Value = "dec" then (-) else (+)
+        let comparator: int->int->bool = 
+            match m.Groups[5].Value with
+            | ">" -> (>)
+            | "<" -> (<)
+            | ">=" -> (>=)
+            | "<=" -> (<=)
+            | "==" -> (=)
+            | "!=" -> (<>)
+            | _ -> (fun x y -> false)
+        Some((m.Groups[1].Value, op, int m.Groups[3].Value, m.Groups[4].Value, comparator, int m.Groups[6].Value))
+    else None
+
+type Instruction =
+    struct
+        val opReg: string
+        val opFun: int -> int -> int
+        val opVal: int
+        val compReg: string
+        val compFun: int -> int -> bool
+        val compVal: int
+        new(inst: string) =
+            match inst with
+            | InstRegex (target, operator, value, compared, comparator, compValue) ->
+                {
+                    opReg = target;
+                    opFun = operator;
+                    opVal = value;
+                    compReg = compared;
+                    compFun = comparator;
+                    compVal = compValue
+                }
+            | _ -> {opReg = ""; opFun = (+); opVal = 0; compReg = ""; compFun = (>); compVal = 0}
+    end
+
+let registers = new Dictionary<string, int>()
+let mutable maxValue = 0
+let executeInstruction (inst: Instruction) =
+    let mutable regVal = 0
+    registers.TryGetValue(inst.compReg, &regVal) |> ignore
+    if inst.compFun regVal inst.compVal then
+        registers.TryGetValue(inst.opReg, &regVal) |> ignore
+        registers[inst.opReg] <- inst.opFun regVal inst.opVal
+        if registers[inst.opReg] > maxValue then
+            maxValue <- registers[inst.opReg]
+        else ()
+    else ()
+
+File.ReadAllLines "day8.txt" |> Array.map (fun x -> new Instruction(x)) |> Array.iter executeInstruction
+// part 1
+registers.Values |> Seq.max |> printfn "%A"
+// part 2
+printfn "%d" maxValue
\ No newline at end of file
diff --git a/day9.fsx b/day9.fsx
new file mode 100644
index 0000000..61cba2a
--- /dev/null
+++ b/day9.fsx
@@ -0,0 +1,25 @@
+open System.IO
+open System.Text.RegularExpressions
+
+let input = (File.ReadAllText "day9.txt").Trim()
+let exclRegex = new Regex("!.")
+let garbageRegex = new Regex("<.*?>")
+
+let part1 input =
+    let stripped = garbageRegex.Replace(exclRegex.Replace(input, ""), "") |> String.filter (fun x -> x <> ',')
+    let rec sumLevels input level acc =
+        if input = "" then acc else
+
+        if input[0] = '{' then
+            sumLevels input[1..] (level + 1) (acc + level)
+        else
+            sumLevels input[1..] (level - 1) acc
+
+    sumLevels stripped 1 0|> printfn "%A"
+
+let part2 input =
+    let stripped = exclRegex.Replace(input, "")
+    garbageRegex.Matches(stripped) |> Seq.map (fun x -> x.Groups[0].Value.Length - 2) |> Seq.sum |> printfn "%d"
+
+part1 input
+part2 input
\ No newline at end of file
itter Araq <rumpf_a@web.de> 2015-09-07 12:31:34 +0200 documented NimScript' href='/ahoang/Nim/commit/doc/nims.txt?h=devel&id=adf34082f053f95257597b588ec15d5612518de3'>adf34082f ^
e6f6323e7 ^

adf34082f ^

adf34082f ^



70f5d7fc4 ^
aecfacd39 ^
adf34082f ^















9348636cb ^
9ffed0de3 ^







70f5d7fc4 ^

bb9b60604 ^




































































































































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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331