Skip to content

Commit

Permalink
2024-03 * ** Gleam
Browse files Browse the repository at this point in the history
  • Loading branch information
Janiczek committed Dec 3, 2024
1 parent df9ad30 commit 866eb40
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ version = "1.0.0"
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
gladvent = ">= 2.0.2 and < 3.0.0"
gleam_otp = ">= 0.14.1 and < 1.0.0"
gleam_regexp = ">= 1.0.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
2 changes: 2 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages = [
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
{ name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" },
{ name = "gleam_package_interface", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "CF3BFC5D0997750D9550D8D73A90F4B8D71C6C081B20ED4E70FFBE1E99AFC3C2" },
{ name = "gleam_regexp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "A3655FDD288571E90EE9C4009B719FEF59FA16AFCDF3952A76A125AF23CF1592" },
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
{ name = "glearray", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "B99767A9BC63EF9CC8809F66C7276042E5EFEACAA5B25188B552D3691B91AC6D" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
Expand All @@ -28,5 +29,6 @@ packages = [
[requirements]
gladvent = { version = ">= 2.0.2 and < 3.0.0" }
gleam_otp = { version = ">= 0.14.1 and < 1.0.0" }
gleam_regexp = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
69 changes: 69 additions & 0 deletions src/aoc_2024/day_3.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import extra
import gleam/int
import gleam/list
import gleam/regexp
import gleam/string

pub type Op {
Mul(Int, Int)
Do
Dont
}

fn from_match(match: regexp.Match) -> Op {
case match.content {
"mul(" <> rest -> {
let assert Ok(strs) =
rest
|> string.drop_end(1)
|> string.split_once(",")
Mul(extra.yolo_int(strs.0), extra.yolo_int(strs.1))
}
"do()" -> Do
"don't()" -> Dont
_ -> panic as "Bad input"
}
}

pub fn parse(input: String) -> List(Op) {
let assert Ok(regex) =
regexp.from_string("mul\\(\\d+,\\d+\\)|do\\(\\)|don't\\(\\)")

regex
|> regexp.scan(input)
|> list.map(from_match)
}

pub fn pt_1(ops: List(Op)) {
ops
|> list.map(fn(op) {
case op {
Mul(x, y) -> x * y
Do -> 0
Dont -> 0
}
})
|> int.sum
}

type State {
State(sum: Int, can_mult: Bool)
}

pub fn pt_2(ops: List(Op)) {
let result =
ops
|> list.fold(State(0, True), fn(acc, op) {
case op {
Mul(x, y) ->
case acc.can_mult {
True -> State(..acc, sum: acc.sum + x * y)
False -> acc
}
Do -> State(..acc, can_mult: True)
Dont -> State(..acc, can_mult: False)
}
})

result.sum
}

0 comments on commit 866eb40

Please sign in to comment.