aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/days/day03.gleam
diff options
context:
space:
mode:
authorTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-12-22 18:31:14 +0100
committerTomasz Chojnacki <tomaszchojnacki2001@gmail.com>2023-12-22 18:31:14 +0100
commit7a5f1983f9189422ad5e12afde11d11bec30a3f1 (patch)
tree46a02028e2712beaad7cf0886696ff7cd37798cf /aoc-2020-gleam/src/days/day03.gleam
parentd8e183f02f67522d94deafa328e19b3081ca41be (diff)
downloadgleam_aoc2020-7a5f1983f9189422ad5e12afde11d11bec30a3f1.tar.gz
gleam_aoc2020-7a5f1983f9189422ad5e12afde11d11bec30a3f1.zip
Solve part 1 of day 20
Diffstat (limited to 'aoc-2020-gleam/src/days/day03.gleam')
-rw-r--r--aoc-2020-gleam/src/days/day03.gleam25
1 files changed, 6 insertions, 19 deletions
diff --git a/aoc-2020-gleam/src/days/day03.gleam b/aoc-2020-gleam/src/days/day03.gleam
index a3ab7b0..6a5ca5c 100644
--- a/aoc-2020-gleam/src/days/day03.gleam
+++ b/aoc-2020-gleam/src/days/day03.gleam
@@ -3,11 +3,11 @@ import gleam/io
import gleam/int
import gleam/result as res
import gleam/string as str
-import gleam/function as fun
import gleam/iterator as iter
import gleam/set.{type Set}
import ext/intx
import ext/iteratorx as iterx
+import util/grid
import util/input_util
import util/pos2.{type Pos2}
@@ -24,20 +24,7 @@ type Area {
fn parse_area(from text: String) -> Area {
let lines = str.split(text, on: "\n")
- let trees =
- list.index_fold(over: lines, from: set.new(), with: fn(prev, line, y) {
- line
- |> str.to_graphemes
- |> list.index_map(with: fn(grapheme, x) {
- case grapheme {
- "#" -> Ok(#(x, y))
- _ -> Error(Nil)
- }
- })
- |> list.filter_map(with: fun.identity)
- |> set.from_list
- |> set.union(prev)
- })
+ let trees = grid.parse_grid(text, with: fn(x, y) { #(x, y) })
let assert Ok(cycle) =
lines
|> list.first
@@ -47,12 +34,12 @@ fn parse_area(from text: String) -> Area {
Area(trees, cycle, height)
}
-fn has_tree(in area: Area, at pos: Pos2) -> Bool {
- set.contains(area.trees, #(pos.0 % area.cycle, pos.1))
+fn has_tree(in area: Area, at t: Pos2) -> Bool {
+ set.contains(area.trees, #(pos2.x(t) % area.cycle, pos2.y(t)))
}
-fn is_valid(pos: Pos2, in area: Area) -> Bool {
- intx.is_between(pos.1, 0, and: area.height - 1)
+fn is_valid(p: Pos2, in area: Area) -> Bool {
+ intx.is_between(pos2.y(p), 0, and: area.height - 1)
}
fn tree_count(in area: Area, with slope: Pos2) -> Int {