my solutions to advent of code
aoc advent-of-code
at main 1.2 kB view raw
1import gleam/int.{to_string} 2import gleam/io.{println} 3import gleam/list 4import gleam/result.{unwrap} 5import gleam/string.{split, trim} 6import simplifile.{read} 7 8pub fn get_mem_size(str, cur_size, i) { 9 case string.slice(str, i, 2) { 10 "" -> cur_size 11 "\\\\" | "\\\"" -> get_mem_size(str, cur_size + 1, i + 2) 12 "\\x" -> get_mem_size(str, cur_size + 1, i + 4) 13 _ -> get_mem_size(str, cur_size + 1, i + 1) 14 } 15} 16 17pub fn get_mem_size_helper(str: String) { 18 get_mem_size(string.slice(str, 1, string.length(str) - 2), 0, 0) 19} 20 21pub fn get_encoded_size(str, cur_size, i) { 22 let char = string.slice(str, i, 1) 23 case char { 24 "" -> cur_size + 2 25 "\\" | "\"" -> get_encoded_size(str, cur_size + 2, i + 1) 26 _ -> get_encoded_size(str, cur_size + 1, i + 1) 27 } 28} 29 30pub fn main() { 31 let input = 32 read(from: "../input.txt") 33 |> unwrap("") 34 |> trim() 35 |> split("\n") 36 37 let result_part_1 = 38 input 39 |> list.fold(0, fn(total, str) { 40 total + { string.length(str) - get_mem_size_helper(str) } 41 }) 42 println(result_part_1 |> to_string) 43 44 let result_part_2 = 45 input 46 |> list.fold(0, fn(total, str) { 47 total + { get_encoded_size(str, 0, 0) - string.length(str) } 48 }) 49 println(result_part_2 |> to_string) 50}