this repo has no description
1
fork

Configure Feed

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

Day 5 and 6

estym a7081cb4 4d26f39c

+192
+4
src/aoc.gleam
··· 4 4 import days/day2 5 5 import days/day3 6 6 import days/day4 7 + import days/day5 8 + import days/day6 7 9 import gleam/erlang 8 10 import gleam/int 9 11 import gleam/io ··· 27 29 2 -> day2.start() 28 30 3 -> day3.start() 29 31 4 -> day4.start() 32 + 5 -> day5.start() 33 + 6 -> day6.start() 30 34 _ -> io.println("Tried to run day " <> int.to_string(day)) 31 35 } 32 36 birl.now()
+89
src/days/day5.gleam
··· 1 + import gleam/int 2 + import gleam/io 3 + import gleam/list 4 + import gleam/result 5 + import gleam/set 6 + import gleam/string 7 + import simplifile 8 + import utils/utils 9 + 10 + pub fn start() -> Nil { 11 + let assert Ok(content) = simplifile.read("inputs/day5.txt") 12 + let data = parse(content) 13 + 14 + let _ = io.debug(part1(data)) 15 + let _ = io.debug(part2(data)) 16 + 17 + Nil 18 + } 19 + 20 + type Range { 21 + Range(start: Int, end: Int) 22 + } 23 + 24 + type Input { 25 + Input(fresh: List(Range), ids: List(Int)) 26 + } 27 + 28 + fn parse(data: String) -> Input { 29 + let assert [ranges, identifiers] = string.split(data, "\n\n") 30 + 31 + let fresh = 32 + string.split(ranges, "\n") 33 + |> list.map(fn(range) { 34 + let assert [start, end] = 35 + string.split(range, "-") 36 + |> list.map(int.parse) 37 + |> list.map(result.unwrap(_, 0)) 38 + 39 + Range(start, end) 40 + }) 41 + 42 + let ids = 43 + string.split(identifiers, "\n") 44 + |> list.map(int.parse) 45 + |> list.map(result.unwrap(_, 0)) 46 + 47 + Input(fresh, ids) 48 + } 49 + 50 + fn part1(data: Input) { 51 + list.count(data.ids, fn(x) { 52 + list.any(data.fresh, fn(y) { y.start <= x && x <= y.end }) 53 + }) 54 + } 55 + 56 + fn find_to_merge(lst: List(Range), range: Range) { 57 + list.filter(lst, fn(x) { 58 + { x.start <= range.start && x.end >= range.start } 59 + || { x.start <= range.end && x.end >= range.end } 60 + || { x.start <= range.start && x.end >= range.end } 61 + || { range.start <= x.start && range.end >= x.end } 62 + }) 63 + } 64 + 65 + fn part2(data: Input) { 66 + list.fold(data.fresh, list.new(), fn(acc, range) { 67 + let to_merge = [range, ..find_to_merge(acc, range)] 68 + let min = 69 + list.fold(to_merge, 9_223_372_036_854_775_807, fn(min, r) { 70 + case r.start < min { 71 + True -> r.start 72 + False -> min 73 + } 74 + }) 75 + 76 + let max = 77 + list.fold(to_merge, 0, fn(max, r) { 78 + case r.end > max { 79 + True -> r.end 80 + False -> max 81 + } 82 + }) 83 + 84 + list.filter(acc, fn(x) { !list.contains(to_merge, x) }) 85 + |> list.prepend(Range(min, max)) 86 + }) 87 + |> io.debug 88 + |> list.fold(0, fn(acc, r) { acc + r.end - r.start + 1 }) 89 + }
+99
src/days/day6.gleam
··· 1 + import gleam/int 2 + import gleam/io 3 + import gleam/list 4 + import gleam/result 5 + import gleam/string 6 + import simplifile 7 + 8 + type Operand { 9 + Plus 10 + Mult 11 + } 12 + 13 + type Input { 14 + Input(numbers: List(Int), op: Operand) 15 + } 16 + 17 + pub fn start() -> Nil { 18 + let assert Ok(content) = simplifile.read("inputs/day6.txt") 19 + let data = parse_p1(content) 20 + let _ = io.debug(process(data)) 21 + 22 + let data = parse_p2(content) 23 + let _ = io.debug(process(data)) 24 + 25 + Nil 26 + } 27 + 28 + fn parse_p1(data: String) { 29 + string.split(data, "\n") 30 + |> list.map(fn(x) { 31 + string.split(x, " ") 32 + |> list.filter(fn(y) { !string.is_empty(y) }) 33 + }) 34 + |> list.transpose 35 + |> list.map(fn(x) { 36 + list.fold(x, Input([], Plus), fn(acc, v) { 37 + case int.parse(v) { 38 + Ok(i) -> Input(list.prepend(acc.numbers, i), acc.op) 39 + Error(_) -> 40 + case v { 41 + "+" -> Input(acc.numbers, Plus) 42 + "*" -> Input(acc.numbers, Mult) 43 + _ -> acc 44 + } 45 + } 46 + }) 47 + }) 48 + } 49 + 50 + fn parse_p2(data: String) -> List(Input) { 51 + let lines = string.split(data, "\n") 52 + let operands = 53 + list.last(lines) 54 + |> result.unwrap("") 55 + |> string.to_graphemes 56 + |> list.index_fold([], fn(acc, x, index) { 57 + case x { 58 + "+" -> [#(Plus, index), ..acc] 59 + "*" -> [#(Mult, index), ..acc] 60 + _ -> acc 61 + } 62 + }) 63 + |> list.prepend(#( 64 + Plus, 65 + { list.last(lines) |> result.unwrap("") |> string.length } + 1, 66 + )) 67 + |> list.reverse 68 + 69 + list.window_by_2(operands) 70 + |> list.fold([], fn(acc, ops) { 71 + let #(#(op, idx), #(_, idx2)) = ops 72 + [ 73 + Input( 74 + list.map(list.take(lines, list.length(lines) - 1), fn(l) { 75 + string.drop_start(l, idx) 76 + |> string.to_graphemes() 77 + |> list.take(idx2 - idx - 1) 78 + }) 79 + |> list.transpose 80 + |> list.map(list.filter(_, fn(y) { y != " " })) 81 + |> list.map(string.join(_, "")) 82 + |> list.map(int.parse) 83 + |> list.map(result.unwrap(_, 0)), 84 + op, 85 + ), 86 + ..acc 87 + ] 88 + }) 89 + } 90 + 91 + fn process(data: List(Input)) { 92 + list.map(data, fn(x) { 93 + case x.op { 94 + Plus -> list.fold(x.numbers, 0, int.add) 95 + Mult -> list.fold(x.numbers, 1, int.multiply) 96 + } 97 + }) 98 + |> list.fold(0, int.add) 99 + }