Skip to content

Commit

Permalink
replace list.at and upgrade stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed May 26, 2024
1 parent 462ee7a commit 67bdcd7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ gleam = "~> 1.0"
# links = [{ title = "Website", href = "https://gleam.run" }]

[dependencies]
gleam_stdlib = "~> 0.36.0"
gleam_stdlib = "~> 0.36"
gleam_json = "~> 1.0"

[dev-dependencies]
Expand Down
6 changes: 3 additions & 3 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ packages = [
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ name = "gleam_package_interface", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "52A721BCA972C8099BB881195D821AAA64B9F2655BECC102165D5A1097731F01" },
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "glearray", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "908154F695D330E06A37FAB2C04119E8F315D643206F8F32B6A6C14A8709FFF4" },
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
{ name = "glearray", version = "0.2.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "9C207E05F38D724F464FA921378DB3ABC2B0A2F5821116D8BC8B2CACC68930D5" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
{ name = "glint", version = "1.0.0-rc2", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], otp_app = "glint", source = "hex", outer_checksum = "FD5C47CE237CA67121F3946ADE7C630750BB67F5E8A4717D2DF5B5EE758CCFDB" },
{ name = "parallel_map", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "parallel_map", source = "hex", outer_checksum = "653714A9FD63EACD1A9D0A6582A972B0EC109AE275CDDD2E99CFC3DFAFAB9225" },
Expand All @@ -28,5 +28,5 @@ packages = [
[requirements]
gladvent = { version = "~> 0.7" }
gleam_json = { version = "~> 1.0" }
gleam_stdlib = { version = "~> 0.36.0" }
gleam_stdlib = { version = "~> 0.36" }
gleeunit = { version = "~> 1.0" }
2 changes: 1 addition & 1 deletion src/aoc_2020/day_10.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn setup(l: List(Int)) -> List(Int) {
}

fn last_or_zero(l: List(Int)) -> Int {
let assert Ok(last) = list.at(l, list.length(l) - 1)
let assert Ok(last) = list.last(l)
last
}

Expand Down
52 changes: 26 additions & 26 deletions src/aoc_2020/day_16.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,35 @@ pub fn parse(input: String) -> #(Rules, Ticket, List(Ticket)) {

pub fn pt_1(input: #(Rules, Ticket, List(Ticket))) -> Int {
let #(rules, _your_ticket, other_tickets) = input

other_tickets
|> iterator.from_list
|> iterator.map(match_nums_with_rules(_, rules))
|> iterator.filter(is_invalid)
|> iterator.fold(0, fn(acc, ticket_map) {
map.fold(ticket_map, acc, fn(acc, name, satisfied) {
case set.size(satisfied) {
0 -> acc + name
_ -> acc
}
})
})
let tickets =
other_tickets
|> iterator.from_list
|> iterator.map(match_nums_with_rules(_, rules))
|> iterator.filter(is_invalid)

use acc, ticket_map <- iterator.fold(tickets, 0)
use acc, name, satisfied <- map.fold(ticket_map, acc)
case set.size(satisfied) {
0 -> acc + name
_ -> acc
}
}

pub fn pt_2(input: #(Rules, Ticket, List(Ticket))) -> Int {
let #(rules, your_ticket, other_tickets) = input

let lists_and_matches = only_valid_tickets(other_tickets, rules)

iterator.range(0, map.size(rules) - 1)
|> iterator.map(fn(c) { #(c, potential_column_names(lists_and_matches, c)) })
|> iterator.to_list
|> list.sort(fn(a, b) {
int.compare(set.size(pair.second(a)), set.size(pair.second(b)))
})
list.range(0, map.size(rules) - 1)
|> list.map(fn(c) { #(c, potential_column_names(lists_and_matches, c)) })
|> list.sort(fn(a, b) { int.compare(set.size(a.1), set.size(b.1)) })
|> most_constrained_variable(map.new())
|> map.filter(fn(_name, val) { string.contains(val, "departure") })
|> map.fold(1, fn(acc, column, _rule_name) {
let assert Ok(x) = list.at(your_ticket, column)
let assert Ok(x) =
your_ticket
|> list.drop(column)
|> list.first
acc * x
})
}
Expand Down Expand Up @@ -155,12 +154,13 @@ fn potential_column_names(
matches: List(#(List(Int), RulesSatisfied)),
column: Int,
) -> Set(String) {
list.fold(matches, set.from_list(rule_names), fn(acc, match) {
let #(l, satisfied) = match
let assert Ok(key) = list.at(l, column)
let assert Ok(rules_satisfied) = map.get(satisfied, key)
set.intersection(acc, rules_satisfied)
})
use acc, #(l, satisfied) <- list.fold(matches, set.from_list(rule_names))
let assert Ok(key) =
l
|> list.drop(column)
|> list.first
let assert Ok(rules_satisfied) = map.get(satisfied, key)
set.intersection(acc, rules_satisfied)
}

// note: requires list to be sorted in increasing order by number of potential names for that column
Expand Down
13 changes: 11 additions & 2 deletions src/aoc_2020/day_2.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@ fn is_valid_sled_policy(policy: Policy) -> Bool {

fn is_valid_toboggan_policy(policy: Policy) -> Bool {
let extract_left_and_right = fn(l) {
use at_left <- result.try(list.at(l, policy.left - 1))
use at_right <- result.try(list.at(l, policy.right - 1))
use at_left <- result.try(
l
|> list.drop(policy.left - 1)
|> list.first,
)

use at_right <- result.try(
l
|> list.drop(policy.right - 1)
|> list.first,
)
Ok([at_left, at_right])
}

Expand Down
2 changes: 1 addition & 1 deletion src/aoc_2020/day_5.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn parse(input: String) -> List(Int) {
}

pub fn pt_1(input: List(Int)) -> Int {
list.at(input, list.length(input) - 1)
list.last(input)
|> result.unwrap(0)
}

Expand Down
2 changes: 1 addition & 1 deletion src/aoc_2020/day_9.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ fn sum_until(
fn sum_min_and_max(l: List(Int)) -> Result(Int, Nil) {
let l = list.sort(l, int.compare)
use min <- result.try(list.first(l))
use max <- result.try(list.at(l, list.length(l) - 1))
use max <- result.try(list.last(l))
Ok(min + max)
}

0 comments on commit 67bdcd7

Please sign in to comment.