Skip to content

Commit

Permalink
Day 3 Dusted
Browse files Browse the repository at this point in the history
  • Loading branch information
dalanmiller committed Dec 5, 2024
1 parent 90e20b8 commit 68d48ef
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions 2024/gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ file_streams = ">= 1.2.2 and < 2.0.0"
simplifile = ">= 2.2.0 and < 3.0.0"
gleave = ">= 1.0.0 and < 2.0.0"
filepath = ">= 1.1.0 and < 2.0.0"
gleam_regexp = ">= 1.0.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
Expand Down
2 changes: 2 additions & 0 deletions 2024/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
packages = [
{ name = "file_streams", version = "1.2.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "file_streams", source = "hex", outer_checksum = "E8659E84092A720CB8D737FE6E529B9B7597E92B4B2A70A917E8590FBEAD0AE6" },
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ 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 = "gleave", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "gleave", source = "hex", outer_checksum = "EBEB0DF9C764A6CB22623FF6F03A0BC978D75225303F3BBDEEB705A2DD700D0D" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
Expand All @@ -13,6 +14,7 @@ packages = [
[requirements]
file_streams = { version = ">= 1.2.2 and < 2.0.0" }
filepath = { version = ">= 1.1.0 and < 2.0.0" }
gleam_regexp = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleave = { version = ">= 1.0.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
Expand Down
100 changes: 100 additions & 0 deletions 2024/src/day3.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import gleam/int
import gleam/io
import gleam/list
import gleam/option.{None, Some}
import gleam/regexp
import gleam/string
import utils.{read_file}

pub fn main() {
let input = case read_file("day3.input") {
Ok(s) -> s
_ -> "dunno"
}

io.debug(part1(input))
io.debug(part2(input))
}

fn process(list: List(String), signal: Bool, acc: List(Int)) -> List(Int) {
case list {
[] -> acc
[head, ..rest] -> {
case int.parse(head) {
Ok(v) -> {
let new_acc = case signal {
True -> {
list.append(acc, [v])
}
False -> {
acc
}
}
process(rest, signal, new_acc)
}
Error(_) -> {
case head {
"do" -> process(rest, True, acc)
"don't" -> process(rest, False, acc)
_ -> process(rest, signal, acc)
}
}
}
}
}
}

fn filter_mem(l: List(String)) -> List(Int) {
process(l, True, [])
}

fn mult_pairs(l: List(Int)) -> List(Int) {
case l {
[a, b, ..rest] -> {
list.append([a * b], mult_pairs(rest))
}
_ -> [0]
}
}

pub fn part1(input: String) -> Int {
let assert Ok(re) = regexp.from_string("mul\\((\\d+),(\\d+)\\)")

input
|> string.split("\n")
|> list.map(fn(line: String) {
regexp.scan(re, line)
|> list.flat_map(fn(match: regexp.Match) { match.submatches })
})
|> list.flat_map(fn(sml) {
list.map(sml, fn(sm) {
case sm {
Some(s) -> {
case int.parse(s) {
Ok(v) -> v
Error(_) -> 1
}
}
None -> 1
}
})
})
// |> io.debug
|> mult_pairs()
|> int.sum()
}

pub fn part2(input: String) -> Int {
let assert Ok(re) = regexp.from_string("mul\\((\\d+),(\\d+)\\)|(do(?:n't)?)*")

input
|> string.split("\n")
|> list.flat_map(fn(line: String) {
regexp.scan(re, line)
|> list.flat_map(fn(match: regexp.Match) { match.submatches })
})
|> option.values
|> filter_mem()
|> mult_pairs()
|> int.sum()
}
Loading

0 comments on commit 68d48ef

Please sign in to comment.