mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 18:29:55 +00:00
Day 5 part 2
This commit is contained in:
parent
086ee442fa
commit
40f1374b12
1 changed files with 27 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
||||||
import gleam/io.{debug}
|
import gleam/io.{debug}
|
||||||
import gleam/int
|
import gleam/int
|
||||||
|
import gleam/function.{identity}
|
||||||
import gleam/list.{map}
|
import gleam/list.{map}
|
||||||
import gleam/map.{Map}
|
import gleam/map.{Map}
|
||||||
import gleam/iterator
|
import gleam/iterator
|
||||||
|
@ -11,6 +12,26 @@ type Movement {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pt_1(input: String) -> Int {
|
pub fn pt_1(input: String) -> Int {
|
||||||
|
let #(stack_count, stacks, movements) = load_input(input)
|
||||||
|
execute_movements(stacks, movements, list.reverse)
|
||||||
|
|> top_of_stacks(stack_count - 1, [])
|
||||||
|
|> string.concat
|
||||||
|
|> io.println
|
||||||
|
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pt_2(input: String) -> Int {
|
||||||
|
let #(stack_count, stacks, movements) = load_input(input)
|
||||||
|
execute_movements(stacks, movements, identity)
|
||||||
|
|> top_of_stacks(stack_count - 1, [])
|
||||||
|
|> string.concat
|
||||||
|
|> io.println
|
||||||
|
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_input(input: String) -> #(Int, Map(Int, List(String)), List(Movement)) {
|
||||||
let lines = string.split(input, on: "\n")
|
let lines = string.split(input, on: "\n")
|
||||||
assert Ok(first_line) = list.first(lines)
|
assert Ok(first_line) = list.first(lines)
|
||||||
let stack_count = { string.length(first_line) + 1 } / 4
|
let stack_count = { string.length(first_line) + 1 } / 4
|
||||||
|
@ -31,16 +52,7 @@ pub fn pt_1(input: String) -> Int {
|
||||||
|> iterator.map(parse_movement)
|
|> iterator.map(parse_movement)
|
||||||
|> iterator.to_list
|
|> iterator.to_list
|
||||||
|
|
||||||
execute_movements(parsed, movements)
|
#(stack_count, parsed, movements)
|
||||||
|> top_of_stacks(stack_count - 1, [])
|
|
||||||
|> string.concat
|
|
||||||
|> io.println
|
|
||||||
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pt_2(input: String) -> Int {
|
|
||||||
todo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_stacks(
|
fn parse_stacks(
|
||||||
|
@ -79,16 +91,19 @@ fn parse_movement(line: String) -> Movement {
|
||||||
fn execute_movements(
|
fn execute_movements(
|
||||||
stacks: Map(Int, List(String)),
|
stacks: Map(Int, List(String)),
|
||||||
movements: List(Movement),
|
movements: List(Movement),
|
||||||
|
process: fn(List(String)) -> List(String),
|
||||||
) -> Map(Int, List(String)) {
|
) -> Map(Int, List(String)) {
|
||||||
case movements {
|
case movements {
|
||||||
[] -> stacks
|
[] -> stacks
|
||||||
[m, ..ms] -> execute_movements(execute_movement(stacks, m), ms)
|
[m, ..ms] ->
|
||||||
|
execute_movements(execute_movement(stacks, m, process), ms, process)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute_movement(
|
fn execute_movement(
|
||||||
stacks: Map(Int, List(String)),
|
stacks: Map(Int, List(String)),
|
||||||
movement: Movement,
|
movement: Movement,
|
||||||
|
process: fn(List(String)) -> List(String),
|
||||||
) -> Map(Int, List(String)) {
|
) -> Map(Int, List(String)) {
|
||||||
assert Ok(from) = map.get(stacks, movement.from)
|
assert Ok(from) = map.get(stacks, movement.from)
|
||||||
assert Ok(to) = map.get(stacks, movement.to)
|
assert Ok(to) = map.get(stacks, movement.to)
|
||||||
|
@ -97,7 +112,7 @@ fn execute_movement(
|
||||||
// update the stacks
|
// update the stacks
|
||||||
stacks
|
stacks
|
||||||
|> map.insert(movement.from, remaining)
|
|> map.insert(movement.from, remaining)
|
||||||
|> map.insert(movement.to, list.append(list.reverse(to_move), to))
|
|> map.insert(movement.to, list.append(process(to_move), to))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn top_of_stacks(
|
fn top_of_stacks(
|
||||||
|
|
Loading…
Reference in a new issue