mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Day 3 part 2
This commit is contained in:
parent
c52761fb45
commit
2e17bfb034
1 changed files with 25 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
||||||
import gleam/int
|
import gleam/int
|
||||||
import gleam/set
|
import gleam/set
|
||||||
import gleam/list.{map}
|
import gleam/list.{map}
|
||||||
|
import gleam/iterator
|
||||||
import gleam/string
|
import gleam/string
|
||||||
|
|
||||||
type Rucksack =
|
type Rucksack =
|
||||||
|
@ -8,15 +9,21 @@ type Rucksack =
|
||||||
|
|
||||||
pub fn pt_1(input: String) -> Int {
|
pub fn pt_1(input: String) -> Int {
|
||||||
string.split(input, on: "\n")
|
string.split(input, on: "\n")
|
||||||
|> list.map(string.to_graphemes)
|
|> map(string.to_graphemes)
|
||||||
|> list.map(split_in_half)
|
|> map(split_in_half)
|
||||||
|> list.map(common_item)
|
|> map(common_item)
|
||||||
|> list.map(priority)
|
|> map(priority)
|
||||||
|> int.sum
|
|> int.sum
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pt_2(input: String) -> Int {
|
pub fn pt_2(input: String) -> Int {
|
||||||
todo
|
string.split(input, on: "\n")
|
||||||
|
|> map(string.to_graphemes)
|
||||||
|
|> iterator.from_list
|
||||||
|
|> iterator.sized_chunk(3)
|
||||||
|
|> iterator.map(common_item_pt2)
|
||||||
|
|> iterator.map(priority)
|
||||||
|
|> iterator.fold(0, fn(sum, prio) { sum + prio })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn split_in_half(l: List(String)) -> Rucksack {
|
fn split_in_half(l: List(String)) -> Rucksack {
|
||||||
|
@ -35,6 +42,19 @@ fn common_item(rucksack: Rucksack) -> String {
|
||||||
common
|
common
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn common_item_pt2(sacks: List(List(String))) -> String {
|
||||||
|
assert Ok(inter) =
|
||||||
|
sacks
|
||||||
|
|> map(set.from_list)
|
||||||
|
|> list.reduce(set.intersection)
|
||||||
|
assert 1 = set.size(inter)
|
||||||
|
assert Ok(common) =
|
||||||
|
inter
|
||||||
|
|> set.to_list
|
||||||
|
|> list.first
|
||||||
|
common
|
||||||
|
}
|
||||||
|
|
||||||
// Lowercase item types a through z have priorities 1 through 26.
|
// Lowercase item types a through z have priorities 1 through 26.
|
||||||
// Uppercase item types A through Z have priorities 27 through 52.
|
// Uppercase item types A through Z have priorities 27 through 52.
|
||||||
// I can't find a way to get a codepoint or match on a range with Gleam so this
|
// I can't find a way to get a codepoint or match on a range with Gleam so this
|
||||||
|
|
Loading…
Reference in a new issue