summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-02-18 15:11:04 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-02-18 15:11:04 -0800
commit74ae7d8e8b05babdc8d3972827036fa6f0b2cade (patch)
tree0e875ee58d6fdf5a568e3f82bf58f590871680ab
parentf3d54ee9e83324da6c2c8fc90ea2b5d5d749b0a5 (diff)
downloadAdventOfCode2017-74ae7d8e8b05babdc8d3972827036fa6f0b2cade.tar.gz
solutions up to day 14
-rw-r--r--day11.ml39
-rw-r--r--day12.fsx36
-rw-r--r--day13.fsx17
-rw-r--r--day14.fsx47
-rw-r--r--day14.py8
5 files changed, 147 insertions, 0 deletions
diff --git a/day11.ml b/day11.ml
new file mode 100644
index 0000000..285f755
--- /dev/null
+++ b/day11.ml
@@ -0,0 +1,39 @@
+#use "topfind";;
+#thread;;
+#require "core";;
+#require "stdio";;
+
+open Core
+open Stdio
+
+type step = {x: int; y: int; z: int}
+
+let manhattan coords =
+    (abs coords.x + abs coords.y + abs coords.z) / 2
+
+let incrementStep curr = function
+    | "n"  -> {x = curr.x;      y = curr.y + 1; z = curr.z - 1}
+    | "nw" -> {x = curr.x - 1;  y = curr.y + 1; z = curr.z}
+    | "ne" -> {x = curr.x + 1;  y = curr.y;     z = curr.z - 1}
+    | "sw" -> {x = curr.x - 1;  y = curr.y;     z = curr.z + 1}
+    | "se" -> {x = curr.x + 1;  y = curr.y - 1; z = curr.z}
+    | "s"  -> {x = curr.x;      y = curr.y - 1; z = curr.z + 1}
+    | _ -> {x=0; y=0; z=0}
+
+let part1 steps =
+    List.fold ~init:{x = 0; y = 0; z = 0} ~f:incrementStep steps
+    |> manhattan
+
+let part2 steps =
+    List.fold steps 
+              ~init:({x = 0; y = 0; z = 0}, 0) 
+              ~f:(fun (position, maxSteps) step ->
+                  let newStep = incrementStep position step in
+                  (newStep, max (manhattan newStep) maxSteps))
+    |> (fun (_, x) -> x)
+
+let input = In_channel.read_all "day11.txt" |> String.strip |> String.split ~on:',' 
+
+let () = 
+    part1 input |> Printf.printf "%d\n";
+    part2 input |> Printf.printf "%d\n"
diff --git a/day12.fsx b/day12.fsx
new file mode 100644
index 0000000..af458c2
--- /dev/null
+++ b/day12.fsx
@@ -0,0 +1,36 @@
+open System.IO
+open System.Text.RegularExpressions
+open System.Collections.Generic
+
+let getConnection line = 
+    let idRegex = new Regex("\d+")
+    let ids = idRegex.Matches(line)
+    match [for ID in ids -> ID.Value] with
+    | [] -> ("", [])
+    | head::tail -> (head, tail)
+
+let rec visit (connections: Dictionary<string, list<string>>) (visited: HashSet<string>) origin =
+    if not (visited.Add(origin)) then ()
+    else
+        for child in connections[origin] do visit connections visited child
+
+let checkInGroups (groups: seq<HashSet<string>>) (origin:string) =
+    Seq.fold (fun acc (group: HashSet<string>) -> acc || group.Contains(origin)) false groups
+
+let () = 
+    let pipes = new Dictionary<string, list<string>>()
+    File.ReadAllLines "day12.txt" |> Array.map getConnection |> Array.iter (fun (head, tail) -> pipes[head] <- tail)
+
+    let reachable = new HashSet<string>()
+    visit pipes reachable "0"
+    printfn "%A" reachable.Count
+    
+    let groups = new List<HashSet<string>>()
+    for origin in pipes.Keys do
+        if checkInGroups groups origin then ()
+        else
+            let newGroup = new HashSet<string>()
+            visit pipes newGroup origin
+            groups.Add(newGroup)
+        
+    printfn "%A" groups.Count
diff --git a/day13.fsx b/day13.fsx
new file mode 100644
index 0000000..b7c9975
--- /dev/null
+++ b/day13.fsx
@@ -0,0 +1,17 @@
+open System.IO
+
+let isCaught offset pos =
+    let time, length = pos
+    (time + offset) % ((length - 1) * 2) = 0
+
+let severity positions offset =
+    Array.filter (isCaught offset) positions |> Array.map (fun (t, n) -> t * n) |> Array.sum
+
+let notCaught positions offset =
+    Array.filter (isCaught offset) positions |> Array.length |> (fun x -> x = 0)
+
+let () =
+    let scanners = File.ReadAllLines "day13.txt" |> Array.map (fun x -> x.Split(':')) |> Array.map (fun x -> int x[0], int x[1])
+    
+    severity scanners 0 |> printfn "%d"
+    Seq.initInfinite id |> Seq.filter (notCaught scanners) |> Seq.head |> printfn "%d"
\ No newline at end of file
diff --git a/day14.fsx b/day14.fsx
new file mode 100644
index 0000000..85d696f
--- /dev/null
+++ b/day14.fsx
@@ -0,0 +1,47 @@
+open System
+open System.IO
+
+let revSublist nums pos n =
+    let c = Array.copy nums
+    for i in [0..n-1] do
+        nums.[(pos + i) % 256] <- c.[(pos + n - i - 1) % 256]
+    
+let hash nRounds input =
+    let nums = [|0..255|]
+    let mutable pos = 0
+    let mutable skipsize = 0
+
+    for _ in [1..nRounds] do 
+        for n in input do
+            revSublist nums pos n
+            pos <- pos + n + skipsize
+            skipsize <- skipsize + 1
+    nums
+
+let hexHash (input: string) =
+    input
+    |> Seq.map (fun c -> byte c)
+    |> (fun a -> Seq.append a [|17uy; 31uy; 73uy; 47uy; 23uy|])
+    |> Seq.map int
+    |> hash 64
+    |> Array.chunkBySize 16
+    |> Array.map (Array.reduce (^^^))
+    |> Array.fold (fun str digit -> str + sprintf "%02x" digit) ""
+
+let hashCount input =
+    input
+        |> Seq.countBy id 
+        |> dict
+        |> (fun x -> x['1'])
+
+let hashString input index =
+    sprintf "%s-%d" input index
+        |> hexHash 
+        |> Seq.map (fun x -> Convert.ToString(Convert.ToInt32(string x, 16), 2).PadLeft(4, '0'))
+        |> String.concat ""
+
+let () =
+    let input = "amgozmfv"
+    let hashes = [ for i in [0..127] do yield hashString input i ]
+    File.WriteAllLines("day14regions.txt", hashes)
+    List.map hashCount hashes |> List.sum |> printfn "%d"
\ No newline at end of file
diff --git a/day14.py b/day14.py
new file mode 100644
index 0000000..c5889e2
--- /dev/null
+++ b/day14.py
@@ -0,0 +1,8 @@
+import numpy as np
+from scipy.ndimage import label
+
+with open("day14regions.txt") as regions:
+    hash = np.array([list(map(int, line.strip())) for line in regions])
+    _, num_features = label(hash)
+    print(num_features)
+    
\ No newline at end of file