diff options
author | H.J <thechairman@thechairman.info> | 2024-06-03 12:01:32 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-06-03 12:01:32 -0400 |
commit | a679186bed8e5e284604fab7ef8ac932b66d4d51 (patch) | |
tree | 89cd10f2a7b350dad0c7fc94308e508e532db3a7 | |
parent | 7d704786ec158349f2f34590a858df6e78e844d6 (diff) | |
download | gleam_aoc-a679186bed8e5e284604fab7ef8ac932b66d4d51.tar.gz gleam_aoc-a679186bed8e5e284604fab7ef8ac932b66d4d51.zip |
gleam 2017 day 2 and 3
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_2.gleam | 47 | ||||
-rw-r--r-- | aoc2017-gleam/src/aoc_2017/day_3.gleam | 87 |
2 files changed, 134 insertions, 0 deletions
diff --git a/aoc2017-gleam/src/aoc_2017/day_2.gleam b/aoc2017-gleam/src/aoc_2017/day_2.gleam new file mode 100644 index 0000000..6a5e85d --- /dev/null +++ b/aoc2017-gleam/src/aoc_2017/day_2.gleam @@ -0,0 +1,47 @@ +import gleam/int +import gleam/list +import gleam/result +import gleam/string + +pub fn parse(input: String) { + use row <- list.map(string.split(input, "\n")) + use val <- list.map(string.split(row, "\t")) + let assert Ok(n) = int.parse(val) + n +} + +pub fn pt_1(input: List(List(Int))) { + use acc, row <- list.fold(input, 0) + acc + max(row) - min(row) +} + +pub fn pt_2(input: List(List(Int))) { + use acc, row <- list.fold(input, 0) + let assert [val] = + row |> list.combination_pairs() |> list.map(test_pair) |> result.values() + acc + val +} + +fn max(xs) { + let assert Ok(result) = list.reduce(xs, int.max) + result +} + +fn min(xs) { + let assert Ok(result) = list.reduce(xs, int.min) + result +} + +fn test_pair(tup) { + case tup { + #(a, b) if a > b -> check_divisibility(a, b) + #(b, a) -> check_divisibility(a, b) + } +} + +fn check_divisibility(a, b) { + case a % b { + 0 -> Ok(a / b) + _ -> Error(Nil) + } +} diff --git a/aoc2017-gleam/src/aoc_2017/day_3.gleam b/aoc2017-gleam/src/aoc_2017/day_3.gleam new file mode 100644 index 0000000..5672e39 --- /dev/null +++ b/aoc2017-gleam/src/aoc_2017/day_3.gleam @@ -0,0 +1,87 @@ +import gleam/dict +import gleam/int +import gleam/list + +type Direction { + Up + Down + Left + Right +} + +type State { + State( + x: Int, + y: Int, + direction: Direction, + branch_length: Int, + remaining: Int, + ) +} + +fn starting_state() -> State { + State(0, 0, Right, 1, 1) +} + +fn update_state(state: State) -> State { + case state { + State(x, y, Right, len, 0) -> State(x, y + 1, Up, len, len - 1) + State(x, y, Up, len, 0) -> State(x - 1, y, Left, len + 1, len) + State(x, y, Left, len, 0) -> State(x, y - 1, Down, len, len - 1) + State(x, y, Down, len, 0) -> State(x + 1, y, Right, len + 1, len) + State(x, y, Right, len, rem) -> State(x + 1, y, Right, len, rem - 1) + State(x, y, Up, len, rem) -> State(x, y + 1, Up, len, rem - 1) + State(x, y, Left, len, rem) -> State(x - 1, y, Left, len, rem - 1) + State(x, y, Down, len, rem) -> State(x, y - 1, Down, len, rem - 1) + } +} + +type Grid = + dict.Dict(#(Int, Int), Int) + +pub fn parse(input: String) -> Int { + let assert Ok(n) = int.parse(input) + n +} + +pub fn pt_1(input: Int) -> Int { + next_step(1, input, starting_state()) +} + +fn next_step(current: Int, target: Int, state: State) -> Int { + case current == target { + True -> int.absolute_value(state.x) + int.absolute_value(state.y) + False -> next_step(current + 1, target, update_state(state)) + } +} + +pub fn pt_2(input: Int) -> Int { + let grid: Grid = dict.from_list([#(#(0, 0), 1)]) + + add_next_cell(input, starting_state(), grid) +} + +fn neighbors(coord: #(Int, Int)) -> List(#(Int, Int)) { + let #(x, y) = coord + + use dx <- list.flat_map(list.range(-1, 1)) + use dy <- list.map(list.range(-1, 1)) + #(x + dx, y + dy) +} + +fn add_next_cell(target: Int, state: State, grid: Grid) -> Int { + let next_cell = update_state(state) + let coords = #(next_cell.x, next_cell.y) + let value = + list.fold(neighbors(coords), 0, fn(acc, coord) { + case dict.get(grid, coord) { + Ok(n) -> acc + n + _err -> acc + } + }) + + case value >= target { + True -> value + False -> add_next_cell(target, next_cell, dict.insert(grid, coords, value)) + } +} |