mirror of
https://github.com/wezm/advent-of-code.git
synced 2025-01-22 18:40:47 +00:00
Get day 8 part 1 working
This commit is contained in:
parent
6f59eeb7ec
commit
83c8bbc5dd
2 changed files with 32 additions and 35 deletions
2022
|
@ -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,38 +16,34 @@ 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 =
|
trees_by_row
|
||||||
2 * list.length(trees_by_row) + 2 * list.length(trees_by_col) - 4
|
|> iterator.from_list
|
||||||
debug(outside_trees)
|
|> zip(left)
|
||||||
|
|> zip(right)
|
||||||
let inside_trees =
|
|> zip(top)
|
||||||
trees_by_row
|
|> zip(bottom)
|
||||||
|> iterator.from_list
|
|> iterator.fold(0, process_row)
|
||||||
|> zip(left)
|
|
||||||
|> zip(right)
|
|
||||||
|> zip(top)
|
|
||||||
|> zip(bottom)
|
|
||||||
|> 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(
|
||||||
case acc {
|
#(None, 0),
|
||||||
// Edge tree
|
fn(acc, tree) {
|
||||||
#(None, _) -> #(Some(tree), 10)
|
case acc {
|
||||||
// First tree in from edge
|
// Edge tree
|
||||||
#(Some(prev), 10) -> #(Some(tree), prev)
|
#(None, _) -> #(Some(tree), -1)
|
||||||
// Other tree
|
// First tree in from edge
|
||||||
#(Some(prev), max) -> case prev > max {
|
#(Some(prev), -1) -> #(Some(tree), prev)
|
||||||
True -> #(Some(tree), prev)
|
// Other tree
|
||||||
False -> #(Some(tree), max)
|
#(Some(prev), max) ->
|
||||||
|
case prev > max {
|
||||||
|
True -> #(Some(tree), prev)
|
||||||
|
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]
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue