this repo has no description

Day 2

+159
+1
gleam.toml
··· 10 10 gleam_otp = ">= 0.12.1 and < 1.0.0" 11 11 birl = ">= 1.7.1 and < 2.0.0" 12 12 gleam_regexp = ">= 1.0.0 and < 2.0.0" 13 + gleam_community_maths = ">= 2.0.0 and < 3.0.0" 13 14 14 15 [dev-dependencies] 15 16 gleeunit = ">= 1.0.0 and < 2.0.0"
+3
manifest.toml
··· 4 4 packages = [ 5 5 { name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, 6 6 { name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" }, 7 + { name = "gleam_community_maths", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_yielder"], otp_app = "gleam_community_maths", source = "hex", outer_checksum = "C9EFF6BCF3C9D113EF9837655B0128CA8CAC295131E9F3468F93C1D78EC3E013" }, 7 8 { name = "gleam_erlang", version = "0.32.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "B18643083A0117AC5CFD0C1AEEBE5469071895ECFA426DCC26517A07F6AD9948" }, 8 9 { name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" }, 9 10 { name = "gleam_regexp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "A3655FDD288571E90EE9C4009B719FEF59FA16AFCDF3952A76A125AF23CF1592" }, 10 11 { name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" }, 12 + { name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" }, 11 13 { name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" }, 12 14 { name = "nibble", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "nibble", source = "hex", outer_checksum = "67C6BEBC1AB6D771AB893B4A7B3E66C92668C6E7774C335FEFCD545B06435FE5" }, 13 15 { name = "ranger", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "B8F3AFF23A3A5B5D9526B8D18E7C43A7DFD3902B151B97EC65397FE29192B695" }, ··· 16 18 17 19 [requirements] 18 20 birl = { version = ">= 1.7.1 and < 2.0.0" } 21 + gleam_community_maths = { version = ">= 2.0.0 and < 3.0.0" } 19 22 gleam_erlang = { version = ">= 0.27.0 and < 1.0.0" } 20 23 gleam_otp = { version = ">= 0.12.1 and < 1.0.0" } 21 24 gleam_regexp = { version = ">= 1.0.0 and < 2.0.0" }
+2
src/aoc.gleam
··· 1 1 import birl 2 2 import birl/duration 3 3 import days/day1 4 + import days/day2 4 5 import gleam/erlang 5 6 import gleam/int 6 7 import gleam/io ··· 21 22 case day { 22 23 0 -> io.println_error("Invalid day") 23 24 1 -> day1.start() 25 + 2 -> day2.start() 24 26 _ -> io.println("Tried to run day " <> int.to_string(day)) 25 27 } 26 28 birl.now()
+153
src/days/day2.gleam
··· 1 + import gleam/float 2 + import gleam/int 3 + import gleam/io 4 + import gleam/list 5 + import gleam/result 6 + import gleam/string 7 + import gleam_community/maths 8 + import simplifile 9 + import utils/utils 10 + 11 + type Range = 12 + #(Int, Int) 13 + 14 + pub fn start() -> Nil { 15 + let assert Ok(content) = simplifile.read("inputs/day2.txt") 16 + let data = parse(content) 17 + 18 + let _ = io.debug(part1(data)) 19 + let _ = io.debug(part2(data)) 20 + 21 + Nil 22 + } 23 + 24 + fn parse(data: String) -> List(Range) { 25 + data 26 + |> string.split(",") 27 + |> list.map(fn(x) { 28 + let lst = 29 + x 30 + |> string.split("-") 31 + |> list.map(int.parse) 32 + |> list.map(result.unwrap(_, 0)) 33 + 34 + #( 35 + list.first(lst) |> result.unwrap(0), 36 + list.drop(lst, 1) |> list.first |> result.unwrap(0), 37 + ) 38 + }) 39 + } 40 + 41 + fn is_made_of_part(x: Int) -> Bool { 42 + let length = 43 + maths.logarithm_10(int.to_float(x)) 44 + |> result.unwrap(0.0) 45 + |> float.floor 46 + |> float.add(1.0) 47 + |> float.round 48 + 49 + utils.create_int_range(2, length + 1) 50 + |> list.filter(fn(y) { 51 + int.to_float(length) /. int.to_float(y) 52 + == float.floor(int.to_float(length) /. int.to_float(y)) 53 + }) 54 + |> list.map(fn(l) { 55 + let half_power = 56 + { int.to_float(length) /. int.to_float(l) } 57 + |> float.floor 58 + |> float.power(10.0, _) 59 + |> result.unwrap(0.0) 60 + |> float.round() 61 + 62 + #(utils.create_int_range(0, l), half_power) 63 + }) 64 + |> list.map(fn(index) { 65 + let #(idx, half_power) = index 66 + list.map(idx, fn(y) { 67 + { 68 + int.to_float(x) 69 + /. { 70 + int.power(half_power, int.to_float(y)) 71 + |> result.unwrap(1.0) 72 + } 73 + |> float.modulo(int.to_float(half_power)) 74 + |> result.unwrap(0.0) 75 + |> float.floor 76 + |> float.round 77 + } 78 + }) 79 + }) 80 + |> list.any(fn(x) { 81 + list.all(x, fn(y) { y == { list.first(x) |> result.unwrap(0) } }) 82 + }) 83 + } 84 + 85 + fn is_symetric(x: Int) -> Bool { 86 + let length = 87 + maths.logarithm_10(int.to_float(x)) 88 + |> result.unwrap(0.0) 89 + |> float.floor 90 + |> float.add(1.0) 91 + 92 + let half_power = 93 + float.divide(length, 2.0) 94 + |> result.unwrap(0.0) 95 + |> float.floor 96 + |> float.power(10.0, _) 97 + |> result.unwrap(0.0) 98 + |> float.round() 99 + 100 + x / half_power == x % half_power 101 + } 102 + 103 + fn part1(data: List(Range)) { 104 + data 105 + |> list.map(fn(range: Range) { 106 + let #(start, end) = range 107 + utils.loop( 108 + case is_symetric(start) { 109 + True -> #(start, start) 110 + False -> #(start, 0) 111 + }, 112 + fn(x: #(Int, Int)) { 113 + let #(start, _) = x 114 + start <= end 115 + }, 116 + fn(x: #(Int, Int)) { 117 + let #(start, count) = x 118 + case is_symetric(start + 1) { 119 + True -> #(start + 1, count + start + 1) 120 + False -> #(start + 1, count) 121 + } 122 + }, 123 + fn(x: #(Int, Int)) { x }, 124 + ) 125 + }) 126 + |> list.fold(0, fn(acc, v: #(Int, Int)) { acc + v.1 }) 127 + } 128 + 129 + fn part2(data) { 130 + data 131 + |> list.map(fn(range: Range) { 132 + let #(start, end) = range 133 + utils.loop( 134 + case is_made_of_part(start) { 135 + True -> #(start, start) 136 + False -> #(start, 0) 137 + }, 138 + fn(x: #(Int, Int)) { 139 + let #(start, _) = x 140 + start <= end 141 + }, 142 + fn(x: #(Int, Int)) { 143 + let #(start, count) = x 144 + case is_made_of_part(start + 1) { 145 + True -> #(start + 1, count + start + 1) 146 + False -> #(start + 1, count) 147 + } 148 + }, 149 + fn(x: #(Int, Int)) { x }, 150 + ) 151 + }) 152 + |> list.fold(0, fn(acc, v: #(Int, Int)) { acc + v.1 }) 153 + }