summary refs log tree commit diff stats
path: root/day4.fsx
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-01-04 22:14:05 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-01-04 22:14:05 -0800
commit102086e040577a4f6b3e7d97e442ee32e12f3e27 (patch)
treec88f097b0a798bc9e1cd4ed09684216ac0f0946d /day4.fsx
downloadAdventOfCode2016-102086e040577a4f6b3e7d97e442ee32e12f3e27.tar.gz
solutions to day 8
Diffstat (limited to 'day4.fsx')
-rw-r--r--day4.fsx56
1 files changed, 56 insertions, 0 deletions
diff --git a/day4.fsx b/day4.fsx
new file mode 100644
index 0000000..dd9916a
--- /dev/null
+++ b/day4.fsx
@@ -0,0 +1,56 @@
+open System.IO
+open System.Text.RegularExpressions
+open System
+
+let lines = File.ReadLines "day4.txt" |> Seq.toList
+
+let (|Regex|_|) pattern input = 
+    let m = Regex.Match(input, pattern)
+    if m.Success then Some(List.tail [for g in m.Groups -> g.Value])
+    else None
+
+let validChecksum (info: string * int * string) =
+    let (name, idNum, checksum) = info
+    let counts = 
+        name 
+        |> String.filter ((<>) '-') 
+        |> Seq.toList 
+        |> List.countBy id
+        |> List.sortBy fst
+        |> List.sortBy (snd >> fun x -> -x)
+    let topFive = (List.map fst counts)[0..4] |> Seq.map string|> String.concat "" 
+    if topFive = checksum then idNum else 0
+
+let getInfo string = 
+        match string with
+        | Regex "([a-z\-]*)-(\d+)\[([a-z]{5})\]" [name; id; checksum] -> 
+            (name, int id, checksum)
+        | _ -> ("", 0, "")
+
+let totalChecksum =
+    List.map (getInfo >> validChecksum) >> List.sum
+
+// part 1
+totalChecksum lines |> printfn "%A"
+
+// part 2
+let decrypt (offset: int) (letter: char) : string =
+        match letter with
+        | '-' -> " "
+        | _ ->
+            int letter 
+            |> fun x -> x - (int 'a') + offset 
+            |> fun x -> x % 26 
+            |> ((+) (int 'a')) 
+            |> Convert.ToChar
+            |> string
+
+let decryptValid (info: string * int * string) = 
+    let (name, idNum, checksum) = info
+    Seq.toList name |> List.map (decrypt idNum) |> String.concat "", idNum
+
+
+let decrypted =
+    List.map (getInfo >> decryptValid) >> List.filter (fst >> fun x -> x.Contains "north")
+
+decrypted lines |> printfn "%A"