my solutions to advent of code
aoc advent-of-code
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

i think this is more readable

partially.dead.aylac.top 37a51686 fd8c61e2

verified
+37 -53
+37 -53
2015/7/gleam/src/main.gleam
··· 13 13 Dict(String, Int) 14 14 15 15 pub type Operation { 16 + Set(n: String) 16 17 Not(var: String) 17 18 And(var1: String, var2: String) 18 19 Or(var1: String, var2: String) 19 - Rshift(var: String, n: Int) 20 - Lshift(var: String, n: Int) 21 - Set(n: String) 22 - OpError 20 + Rshift(var1: String, var2: String) 21 + Lshift(var1: String, var2: String) 23 22 } 24 23 25 - pub type GetCircRes { 26 - GetCircRes(value: Int, cache: CircuitCache) 24 + pub type GetRes { 25 + GetRes(value: Int, cache: CircuitCache) 27 26 } 28 27 29 28 pub fn get_circuit_var( 30 29 circuit: Circuit, 31 30 name: String, 32 31 cache: CircuitCache, 33 - ) -> GetCircRes { 32 + ) -> GetRes { 34 33 let get = fn(name, cache) { get_circuit_var(circuit, name, cache) } 35 34 36 35 let parsed_name = int.base_parse(name, 10) 37 - let GetCircRes(value, cache) = case result.is_ok(parsed_name) { 38 - True -> GetCircRes(parsed_name |> unwrap(0), cache) 36 + case result.is_ok(parsed_name) { 37 + True -> GetRes(parsed_name |> unwrap(0), cache) 39 38 False -> { 40 39 case dict.get(cache, name) { 41 - Ok(value) -> GetCircRes(value, cache) 42 - Error(_) -> { 43 - case dict.get(circuit, name) |> unwrap(OpError) { 44 - Not(var) -> { 45 - let GetCircRes(value, cache) = get(var, cache) 46 - GetCircRes(int.bitwise_not(value), cache) 47 - } 48 - And(var1, var2) -> { 49 - let GetCircRes(value1, cache) = get(var1, cache) 50 - let GetCircRes(value2, cache) = get(var2, cache) 51 - GetCircRes(int.bitwise_and(value1, value2), cache) 52 - } 53 - Or(var1, var2) -> { 54 - let GetCircRes(value1, cache) = get(var1, cache) 55 - let GetCircRes(value2, cache) = get(var2, cache) 56 - GetCircRes(int.bitwise_or(value1, value2), cache) 57 - } 58 - Rshift(var, n) -> { 59 - let GetCircRes(value, cache) = get(var, cache) 60 - GetCircRes(int.bitwise_shift_right(value, n), cache) 40 + Ok(value) -> GetRes(value, cache) 41 + Error(Nil) -> { 42 + // will crash if it gets bad data 43 + let assert Ok(op) = dict.get(circuit, name) 44 + 45 + let #(value1, value2, cache) = case op { 46 + Not(v) | Set(v) -> { 47 + let GetRes(value, cache) = get(v, cache) 48 + #(value, 0, cache) 61 49 } 62 - Lshift(var, n) -> { 63 - let GetCircRes(value, cache) = get(var, cache) 64 - GetCircRes(int.bitwise_shift_left(value, n), cache) 50 + And(v1, v2) | Or(v1, v2) | Rshift(v1, v2) | Lshift(v1, v2) -> { 51 + let GetRes(value1, cache) = get(v1, cache) 52 + let GetRes(value2, cache) = get(v2, cache) 53 + #(value1, value2, cache) 65 54 } 66 - Set(var) -> get(var, cache) 67 - OpError -> GetCircRes(0, cache) 55 + } 56 + 57 + let res = case op { 58 + Set(_) -> value1 59 + Not(_) -> int.bitwise_not(value1) 60 + And(_, _) -> int.bitwise_and(value1, value2) 61 + Or(_, _) -> int.bitwise_or(value1, value2) 62 + Rshift(_, _) -> int.bitwise_shift_right(value1, value2) 63 + Lshift(_, _) -> int.bitwise_shift_left(value1, value2) 68 64 } 65 + GetRes(res, dict.insert(cache, name, res)) 69 66 } 70 67 } 71 68 } 72 69 } 73 - GetCircRes(value, dict.insert(cache, name, value)) 74 70 } 75 71 76 72 pub fn main() { ··· 86 82 dict.insert(circ, target, And(var1, var2)) 87 83 [var1, "OR", var2, "->", target] -> 88 84 dict.insert(circ, target, Or(var1, var2)) 89 - [var, "RSHIFT", n, "->", target] -> 90 - dict.insert( 91 - circ, 92 - target, 93 - Rshift(var, int.base_parse(n, 10) |> unwrap(0)), 94 - ) 95 - [var, "LSHIFT", n, "->", target] -> 96 - dict.insert( 97 - circ, 98 - target, 99 - Lshift(var, int.base_parse(n, 10) |> unwrap(0)), 100 - ) 85 + [var1, "RSHIFT", var2, "->", target] -> 86 + dict.insert(circ, target, Rshift(var1, var2)) 87 + [var1, "LSHIFT", var2, "->", target] -> 88 + dict.insert(circ, target, Lshift(var1, var2)) 101 89 [v, "->", target] -> dict.insert(circ, target, Set(v)) 102 90 103 91 _ -> circ ··· 107 95 let result_part_1 = get_circuit_var(input, "a", dict.new()).value 108 96 println(result_part_1 |> to_string) 109 97 110 - let result_part_2 = 111 - get_circuit_var( 112 - dict.insert(input, "b", Set(int.to_string(result_part_1))), 113 - "a", 114 - dict.new(), 115 - ).value 98 + let input_part_2 = dict.insert(input, "b", Set(int.to_string(result_part_1))) 99 + let result_part_2 = get_circuit_var(input_part_2, "a", dict.new()).value 116 100 println(result_part_2 |> to_string) 117 101 }