Get day 8 part 1 working

This commit is contained in:
Wesley Moore 2022-12-09 08:04:18 +10:00
parent 6f59eeb7ec
commit 83c8bbc5dd
No known key found for this signature in database
2 changed files with 32 additions and 35 deletions

View file

@ -1,10 +1,8 @@
import gleam/io.{debug}
import gleam/int import gleam/int
import gleam/iterator.{zip} import gleam/iterator.{zip}
import gleam/result
import gleam/string import gleam/string
import gleam/list import gleam/list
import gleam/option.{Some, None} import gleam/option.{None, Some}
pub fn pt_1(input: String) -> Int { pub fn pt_1(input: String) -> Int {
let trees_by_row = let trees_by_row =
@ -18,28 +16,27 @@ pub fn pt_1(input: String) -> Int {
let left = let left =
list.map(trees_by_row, scan_max) list.map(trees_by_row, scan_max)
|> iterator.from_list |> iterator.from_list
// debug(left |> iterator.to_list)
let right = let right =
trees_by_row trees_by_row
|> list.map(list.reverse) |> list.map(list.reverse)
|> list.map(scan_max) |> list.map(scan_max)
|> list.reverse |> list.map(list.reverse)
|> iterator.from_list |> iterator.from_list
let top = let top =
list.map(trees_by_col, scan_max) list.map(trees_by_col, scan_max)
|> list.transpose
|> iterator.from_list |> iterator.from_list
let bottom = let bottom =
trees_by_col trees_by_col
|> list.map(list.reverse) |> list.map(list.reverse)
|> list.map(scan_max) |> list.map(scan_max)
|> list.reverse |> list.map(list.reverse)
|> list.transpose
|> iterator.from_list |> iterator.from_list
let outside_trees =
2 * list.length(trees_by_row) + 2 * list.length(trees_by_col) - 4
debug(outside_trees)
let inside_trees =
trees_by_row trees_by_row
|> iterator.from_list |> iterator.from_list
|> zip(left) |> zip(left)
@ -47,9 +44,6 @@ pub fn pt_1(input: String) -> Int {
|> zip(top) |> zip(top)
|> zip(bottom) |> zip(bottom)
|> iterator.fold(0, process_row) |> iterator.fold(0, process_row)
debug(inside_trees)
outside_trees + inside_trees
} }
pub fn pt_2(input: String) -> Int { pub fn pt_2(input: String) -> Int {
@ -69,19 +63,23 @@ fn parse_int(text: String) -> Int {
pub fn scan_max(numbers: List(Int)) -> List(Int) { pub fn scan_max(numbers: List(Int)) -> List(Int) {
numbers numbers
|> list.scan(#(None, 0), fn(acc, tree) { |> list.scan(
#(None, 0),
fn(acc, tree) {
case acc { case acc {
// Edge tree // Edge tree
#(None, _) -> #(Some(tree), 10) #(None, _) -> #(Some(tree), -1)
// First tree in from edge // First tree in from edge
#(Some(prev), 10) -> #(Some(tree), prev) #(Some(prev), -1) -> #(Some(tree), prev)
// Other tree // Other tree
#(Some(prev), max) -> case prev > max { #(Some(prev), max) ->
case prev > max {
True -> #(Some(tree), prev) True -> #(Some(tree), prev)
False -> #(Some(tree), max) False -> #(Some(tree), max)
} }
} }
}) },
)
|> list.map(fn(tup) { tup.1 }) |> list.map(fn(tup) { tup.1 })
} }
@ -106,7 +104,6 @@ fn count_if_visible(
record: #(#(#(#(Int, Int), Int), Int), Int), record: #(#(#(#(Int, Int), Int), Int), Int),
) -> Int { ) -> Int {
let #(#(#(#(tree, left), right), top), bottom) = record let #(#(#(#(tree, left), right), top), bottom) = record
debug(#(tree, left, right, top, bottom))
case case
[left, right, top, bottom] [left, right, top, bottom]

View file

@ -8,7 +8,7 @@ pub fn main() {
pub fn scan_max_test() { pub fn scan_max_test() {
day_8.scan_max([2, 5, 5, 1, 2]) day_8.scan_max([2, 5, 5, 1, 2])
|> should.equal([10, 2, 5, 5, 5]) |> should.equal([-1, 2, 5, 5, 5])
} }
// 30373 // 30373