Day 6 part 2

This commit is contained in:
Wesley Moore 2022-12-06 17:30:01 +10:00
parent 1e8e3308fc
commit d2f60b0427
No known key found for this signature in database

View file

@ -4,25 +4,31 @@ import gleam/string
import gleam/list
pub fn pt_1(input: String) -> Int {
input
|> tune(4)
}
pub fn pt_2(input: String) -> Int {
input
|> tune(14)
}
fn tune(input: String, window: Int) -> Int {
assert Ok(header) =
input
|> string.trim_right
|> string.to_graphemes
|> list.index_map(fn(i, el) { #(i + 1, el) })
|> list.window(4)
|> list.find(is_unique)
|> list.window(window)
|> list.find(is_unique(_, window))
assert Ok(index) = list.last(header)
index.0
}
pub fn pt_2(input: String) -> Int {
todo
}
fn is_unique(chars: List(#(Int, String))) -> Bool {
fn is_unique(chars: List(#(Int, String)), window: Int) -> Bool {
chars
|> list.map(fn(pair) { pair.1 })
|> list.unique
|> list.length == 4
|> list.length == window
}