Skip to content

Commit

Permalink
2024 day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Dec 1, 2024
1 parent e5882f9 commit fd2a175
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ gleam_stdlib = "~> 0.39"
gleam_json = "~> 2.0"
gladvent = "~> 2.0"
gleam_yielder = ">= 1.0.0 and < 2.0.0"
tote = ">= 1.0.2 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.2.0 and < 2.0.0"
Expand Down Expand Up @@ -53,3 +54,6 @@ gleeunit = ">= 1.2.0 and < 2.0.0"

[gladvent.2023]
1 = { pt_1 = 55123, pt_2 = 55260 }

[gladvent.2024]
1 = { pt_1 = 1579939, pt_2 = 20351745 }
2 changes: 2 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ packages = [
{ name = "snag", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "08E9EB87C413457DB1DD66CD704C6878DACC9C93B418600F63873D0CD224E756" },
{ name = "spinner", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib", "glearray", "repeatedly"], otp_app = "spinner", source = "hex", outer_checksum = "9EE43AA33BE2DA5731B8F3F170AAB59AF1A815AFA5BF615F12C1B91F3B03F157" },
{ name = "tom", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "228E667239504B57AD05EC3C332C930391592F6C974D0EFECF32FFD0F3629A27" },
{ name = "tote", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tote", source = "hex", outer_checksum = "A249892E26A53C668897F8D47845B0007EEE07707A1A03437487F0CD5A452CA5" },
]

[requirements]
Expand All @@ -32,3 +33,4 @@ gleam_json = { version = "~> 2.0" }
gleam_stdlib = { version = "~> 0.39" }
gleam_yielder = { version = ">= 1.0.0 and < 2.0.0" }
gleeunit = { version = ">= 1.2.0 and < 2.0.0" }
tote = { version = ">= 1.0.2 and < 2.0.0" }
30 changes: 30 additions & 0 deletions src/aoc_2024/day_1.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import gleam/int
import gleam/list
import gleam/result
import gleam/string
import tote/bag

pub fn parse(input: String) -> #(List(Int), List(Int)) {
input
|> string.split("\n")
|> list.map(fn(line) {
let assert Ok(#(a, b)) = string.split_once(line, " ")
let assert Ok(left) = int.parse(a)
let assert Ok(right) = int.parse(b)
#(left, right)
})
|> list.unzip
}

pub fn pt_1(input: #(List(Int), List(Int))) -> Int {
let sort_ints = list.sort(_, int.compare)
let sorted = list.zip(sort_ints(input.0), sort_ints(input.1))
use acc, #(left, right) <- list.fold(sorted, 0)
acc + int.absolute_value(left - right)
}

pub fn pt_2(input: #(List(Int), List(Int))) {
let counts = bag.from_list(input.1)
use acc, id <- list.fold(input.0, 0)
acc + { id * bag.copies(counts, id) }
}

0 comments on commit fd2a175

Please sign in to comment.