Skip to content

Commit

Permalink
Solution for Day 1 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Dec 4, 2024
1 parent ae9f0c5 commit 0aa5601
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ version = "1.0.0"
[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
simplifile = ">= 2.2.0 and < 3.0.0"
party = ">= 1.0.5 and < 2.0.0"

[dev-dependencies]
glacier = ">= 1.1.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 @@ -11,11 +11,13 @@ packages = [
{ name = "gleam_community_colour", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "386CB9B01B33371538672EEA8A6375A0A0ADEF41F17C86DDCB81C92AD00DA610" },
{ name = "gleam_json", version = "2.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "0A57FB5666E695FD2BEE74C0428A98B0FC11A395D2C7B4CDF5E22C5DD32C74C6" },
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
{ name = "party", version = "1.0.5", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "party", source = "hex", outer_checksum = "99456722503230EA6498B2F618C3A227269430427669A11B24C4F6655CA4CD31" },
{ name = "shellout", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "shellout", source = "hex", outer_checksum = "E2FCD18957F0E9F67E1F497FC9FF57393392F8A9BAEAEA4779541DE7A68DD7E0" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
]

[requirements]
glacier = { version = ">= 1.1.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
party = { version = ">= 1.0.5 and < 2.0.0" }
simplifile = { version = ">= 2.2.0 and < 3.0.0" }
38 changes: 38 additions & 0 deletions src/day01/part2.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import gleam/int
import gleam/io
import gleam/list
import gleam/string
import party.{do, return}
import simplifile

pub fn main() {
let assert Ok(content) = simplifile.read("inputs/day01.txt")
content
|> parse
|> process
|> int.to_string
|> io.println
}

pub fn parse(content) {
let assert Ok(rows) = string.trim(content) |> party.go(parser(), _)
rows
}

fn parser() {
let line_parser = {
use left <- do(party.digits() |> party.try(int.parse))
use _ <- do(party.whitespace())
use right <- do(party.digits() |> party.try(int.parse))
return([left, right])
}
party.sep(line_parser, by: party.string("\n"))
}

pub fn process(rows) {
let assert [lefts, rights] = list.transpose(rows)
list.fold(lefts, 0, fn(acc, left) {
let count = list.count(rights, where: fn(right) { right == left })
acc + left * count
})
}
12 changes: 12 additions & 0 deletions test/day01/part2_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import day01/part2 as solution
import glacier/should

pub fn parse_test() {
solution.parse("1 2\n3 4\n")
|> should.equal([[1, 2], [3, 4]])
}

pub fn process_test() {
solution.process([[3, 3], [5, 3], [2, 5]])
|> should.equal(3 * 2 + 5)
}

0 comments on commit 0aa5601

Please sign in to comment.