<jupyter_start><jupyter_text>Day 3, Binary Diagnostic Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.<jupyter_code>let divide source = ((0,0), source) ||> List.fold (fun (x,y) char -> match char with '1' -> (x + 1, y) | '0' -> (x, y + 1) | _ -> (x,y)) 

let significate (x,y) = if x >= y then ['1';'0'] else ['0';'1']

let toNumber str = System.Convert.ToInt32(str, 2)

let toString source = source |> Array.ofSeq |> (fun s -> new System.String(s))
["00100";"11110";"10110";"10111";"10101";"01111";"00111";"11100";"10000";"11001";"00010";"01010"]
    |> List.map (fun l -> l.ToCharArray() |> Array.toList)
    |> List.transpose
    |> List.map divide //Item1: 1's count, Item2: 0's count
    |> List.map significate // 0: mcb, 1: lcb
    |> List.transpose // 0: Gamma, 1: epsilon
    |> List.map (fun l -> l |> toString |> toNumber)
    |> List.fold (*) 1
System.IO.File.ReadLines("../Data/Day3.txt") |> Seq.toList
    |> List.map (fun l -> l.ToCharArray() |> Array.toList)
    |> List.transpose
    |> List.map divide //Item1: 1's count, Item2: 0's count
    |> List.map significate // 0: mcb, 1: lcb
    |> List.transpose // 0: Gamma, 1: epsilon
    |> List.map (fun l -> l |> toString |> toNumber)
    |> List.fold (*) 1<jupyter_output><empty_output><jupyter_text>Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together>To find the life support rating from sample, multiply the oxygen generator rating (23) by the CO2 scrubber rating (10) to get 230.<jupyter_code>// let expandBits = ["00100";"11110";"10110";"10111";"10101";"01111";"00111";"11100";"10000";"11001";"00010";"01010"] |> List.map (fun l -> l.ToCharArray() |> Array.toList)
let expandBits = System.IO.File.ReadLines("../Data/Day3.txt") |> Seq.toList |> List.map (fun l -> l.ToCharArray() |> Array.toList)
let inline mcb l = l |> List.head
let inline lcb l = l |> List.tail |> List.head

let rec reduceBits op i bits =
    if Seq.length bits = 1 then bits
    else
        let rb = bits |> List.transpose |> List.item i |> divide |> significate |> op
        bits |> List.where (fun l -> l[i] = rb) |> reduceBits op (i+1)

let inline reduceBy op = reduceBits op 0

let o2 = expandBits |> reduceBy mcb |> List.map (fun l -> l |> toString |> toNumber) |> List.head
let co2 = expandBits |> reduceBy lcb |> List.map (fun l -> l |> toString |> toNumber) |> List.head

o2 * co2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1604491126403477, 'fraction_numerical': 0.06157189424121695, 'mean_word_length': 3.7294520547945207, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '431980', 'license': 'MIT', 'n_tokens_mistral': 1084, 'n_tokens_neox': 958, 'n_words': 315, 'path': 'Notebooks/Day3.ipynb', 'repo_name': 'jbudbo/Advent2021', 'size': '2761'}