This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3.fsx
More file actions
53 lines (44 loc) · 1.25 KB
/
Day3.fsx
File metadata and controls
53 lines (44 loc) · 1.25 KB
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
open System
let input =
IO.File.ReadLines "Day3.txt"
|> Seq.map (fun s -> s |> Seq.toArray)
|> Seq.toArray
let mostCommonAtIndex index (array: char [] []) =
(array2D array).[*, index]
|> Seq.fold
(fun acc hd ->
acc
+ match hd with
| '0' -> -1
| '1' -> 1
| _ -> failwith "oh no")
0
|> fun i -> if i >= 0 then '1' else '0'
let rate =
let mostCommonInInput =
input.[0]
|> Array.mapi (fun i _ -> mostCommonAtIndex i input)
fun i ->
mostCommonInInput
|> Seq.map (fun bit -> if bit = i then '0' else '1')
|> String.Concat
|> fun s -> Convert.ToInt64(s, 2)
let problem1 =
let gamma = rate '1'
let epsilon = rate '0'
gamma * epsilon
let rate2 f =
let rec loop (index: int) (remaining: char [] []) =
match Array.tryExactlyOne remaining with
| Some final -> final
| None ->
Array.filter (fun (arr: char []) -> f arr.[index] (mostCommonAtIndex index remaining)) remaining
|> loop (index + 1)
input
|> loop 0
|> String.Concat
|> fun s -> Convert.ToInt64(s, 2)
let problem2 =
let oxygen = rate2 (=)
let CO2 = rate2 (<>)
oxygen * CO2