aoc2026

Advent of code 2026 solutions in Gleam
Log | Files | Refs | README

day1.gleam (988B)


      1 import gleam/order
      2 import gleam/dict
      3 import gleam/int
      4 import gleam/list
      5 import gleam/string
      6 import gleam/option.{Some,None}
      7 
      8 pub fn part1(in: String) -> String {
      9 	parse_in(in)
     10 	|> count_freq()
     11 	|> dict.to_list
     12 	|> list.sort(fn (tup1, tup2) {
     13 		order.negate(int.compare(tup1.1, tup2.1))
     14 	})
     15 	|> list.take(3)
     16 	|> list.map(fn (tup) {tup.0})
     17 	|> list.fold(1, int.multiply)
     18 	|> int.to_string
     19 }
     20 
     21 pub fn part2(in: String) -> String {
     22 	parse_in(in)
     23 	|> count_freq()
     24 	|> dict.to_list
     25 	|> list.sort(fn (tup1, tup2) {
     26 		int.compare(tup1.1, tup2.1)
     27 	})
     28 	|> list.take(3)
     29 	|> list.map(fn (tup) {tup.0})
     30 	|> list.fold(1, int.multiply)
     31 	|> int.to_string
     32 }
     33 
     34 fn parse_in(in: String) -> List(Int) {
     35 	string.split(in, "\n") 
     36 	|> list.map(fn (tok) { 
     37 		let assert Ok(num) = int.parse(tok)
     38 		num
     39 	})
     40 }
     41 
     42 fn count_freq(in: List(Int)) -> dict.Dict(Int, Int) {
     43 	list.fold(in, dict.new(), fn (acc, num) {
     44 		dict.upsert(acc, num, fn (maybe_ct) {
     45 			case maybe_ct {
     46 				Some(ct) -> ct+1
     47 				None -> 1
     48 			}
     49 		})
     50 	})
     51 }