mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Day 10 part 2
This commit is contained in:
parent
301a86bd86
commit
e507bba147
1 changed files with 32 additions and 0 deletions
|
@ -23,7 +23,20 @@ pub fn pt_1(input: String) -> Int {
|
|||
|> int.sum
|
||||
}
|
||||
|
||||
pub const display_width = 40
|
||||
|
||||
pub fn pt_2(input: String) -> Int {
|
||||
string.split(input, "\n")
|
||||
|> list.fold([Trace(1, 0)], execute)
|
||||
|> list.reverse
|
||||
|> list.window_by_2
|
||||
|> list.fold([], inflate)
|
||||
|> list.fold([], render)
|
||||
|> list.reverse
|
||||
|> list.sized_chunk(display_width)
|
||||
|> list.map(string.join(_, ""))
|
||||
|> string.join("\n")
|
||||
|> io.println
|
||||
2
|
||||
}
|
||||
|
||||
|
@ -81,3 +94,22 @@ fn matches(want: Int, a: Trace, b: Trace) -> Option(Int) {
|
|||
fn signal_strength(pair: #(Int, Int)) -> Int {
|
||||
pair.0 * pair.1
|
||||
}
|
||||
|
||||
fn inflate(out: List(Trace), pair: #(Trace, Trace)) -> List(Trace) {
|
||||
let #(prev, this) = pair
|
||||
let inflated =
|
||||
list.range(prev.cycles, this.cycles - 1)
|
||||
|> list.map(fn(i) { Trace(x: prev.x, cycles: i) })
|
||||
list.append(out, inflated)
|
||||
}
|
||||
|
||||
fn render(display: List(String), trace: Trace) -> List(String) {
|
||||
let x = trace.x
|
||||
case
|
||||
list.range(x - 1, x + 1)
|
||||
|> list.contains(trace.cycles % display_width)
|
||||
{
|
||||
True -> ["#", ..display]
|
||||
False -> [" ", ..display]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue