-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |