my solutions to advent of code
aoc advent-of-code
at main 1.4 kB view raw
1import gleam/dict.{type Dict} 2import gleam/int 3import gleam/io 4import gleam/list 5import gleam/result 6import gleam/set.{type Set} 7import gleam/string 8import simplifile as file 9 10type Results = 11 Set(String) 12 13type Combinations = 14 Dict(String, Results) 15 16pub fn main() { 17 let assert Ok(input) = file.read(from: "../input.txt") 18 as "Input file not found" 19 let input = input |> string.trim |> string.split("\n") |> list.reverse 20 let combinations: Combinations = 21 input 22 |> list.drop(2) 23 |> list.map(fn(v) { 24 case v |> string.split(" ") { 25 [origin, _, result] -> #(origin, result) 26 _ -> #("", "") 27 } 28 }) 29 |> list.fold(dict.new(), fn(acc, v) { 30 acc 31 |> dict.get(v.0) 32 |> result.unwrap(set.new()) 33 |> set.insert(v.1) 34 |> dict.insert(acc, v.0, _) 35 }) 36 let molecule = { 37 let molecule = 38 input 39 |> list.first 40 |> result.unwrap("") 41 |> string.to_graphemes 42 |> list.fold(#(list.new(), ""), fn(acc, letter) { 43 case letter == string.lowercase(letter) { 44 True -> #(acc.0, acc.1 <> letter) 45 False -> #(list.append(acc.0, [acc.1]), letter) 46 } 47 }) 48 molecule.0 |> list.drop(1) |> list.append([molecule.1]) 49 } 50 let part_1_res = { 51 list.range(1, list.length(molecule)) |> list.fold(set.new(), fn(acc, v) { 52 53 }) 54 } 55}