Skip to content

Commit

Permalink
2024 - Gleam - Day 01 - Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoVIII committed Jan 6, 2025
1 parent 4ceafb3 commit 23e77b1
Show file tree
Hide file tree
Showing 22 changed files with 297 additions and 15 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/2024-gleam.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Tests - 2024 - Gleam

on:
push:
paths:
- ".github/workflows/2024-gleam.yml"
- "2024/puzzles/**"
- "2024/solutions/gleam/**"
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: erlef/setup-beam@v1
with:
otp-version: "27.1.2"
gleam-version: "1.7.0"
rebar3-version: "3"
# elixir-version: "1"
- name: Run tests
run: ./2024/solutions/gleam/test-all.sh
5 changes: 2 additions & 3 deletions .github/workflows/2024-roc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ on:
push:
paths:
- ".github/workflows/2024-roc.yml"
- "2024/inputs/**"
- "2024/outputs/**"
- "2024/solutions/fsharp/**"
- "2024/puzzles/**"
- "2024/solutions/roc/**"
pull_request:

jobs:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions 2024/solutions/gleam/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
"name": "Ubuntu",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:jammy",
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers-extra/features/gleam:1": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "./.devcontainer/post-create.sh",
// Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions": [
"editorconfig.editorconfig",
"gleam.gleam",
"github.copilot"
]
}
}
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
9 changes: 9 additions & 0 deletions 2024/solutions/gleam/.devcontainer/post-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -euo pipefail

echo "deb http://binaries2.erlang-solutions.com/ubuntu/ jammy-esl-erlang-25 contrib" | sudo tee -a /etc/apt/sources.list
wget https://binaries2.erlang-solutions.com/GPG-KEY-pmanager.asc
sudo apt-key add GPG-KEY-pmanager.asc
rm GPG-KEY-pmanager.asc
sudo apt update
sudo apt install -y esl-erlang
Empty file.
3 changes: 3 additions & 0 deletions 2024/solutions/gleam/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true,
}
3 changes: 3 additions & 0 deletions 2024/solutions/gleam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# README

To create a new day, run `gleam new day<day>`. After that delete the .github folder and the README.
4 changes: 4 additions & 0 deletions 2024/solutions/gleam/day01/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
22 changes: 22 additions & 0 deletions 2024/solutions/gleam/day01/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name = "day01"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
argv = ">= 1.0.0 and < 2.0.0"
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
simplifile = ">= 2.2.0 and < 3.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
testbase = { path = "../testbase" }
18 changes: 18 additions & 0 deletions 2024/solutions/gleam/day01/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ name = "gleam_stdlib", version = "0.52.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "50703862DF26453B277688FFCDBE9DD4AC45B3BD9742C0B370DB62BC1629A07D" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
{ name = "testbase", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], source = "local", path = "../testbase" },
]

[requirements]
argv = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
simplifile = { version = ">= 2.2.0 and < 3.0.0" }
testbase = { path = "../testbase" }
51 changes: 51 additions & 0 deletions 2024/solutions/gleam/day01/src/day01.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import argv
import gleam/int
import gleam/io
import gleam/list
import gleam/string
import simplifile

type Data =
List(#(Int, Int))

pub fn parse(input: String) -> Data {
string.trim(input)
|> string.split("\n")
|> list.map(fn(line) {
let assert [x, y] =
string.trim(line)
|> string.split(" ")
|> list.map(fn(x) {
let assert Ok(number) = int.parse(x)
number
})
#(x, y)
})
}

pub fn part1(data: Data) {
let #(list1, list2) = list.unzip(data)
let list1_sorted = list.sort(list1, int.compare)
let list2_sorted = list.sort(list2, int.compare)
list.zip(list1_sorted, list2_sorted)
|> list.map(fn(pair) {
let #(x, y) = pair
int.absolute_value(x - y)
})
|> list.fold(0, int.add)
}

pub fn part2(_x) {
42
}

pub fn main() {
case argv.load().arguments {
[input_file] -> {
let assert Ok(input) = simplifile.read(input_file)
let data = parse(input)
io.println("Part 1: " <> { part1(data) |> int.to_string })
}
_ -> io.println("Usage: gleam run -- <input-file>")
}
}
22 changes: 22 additions & 0 deletions 2024/solutions/gleam/day01/test/day01_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import gleam/int
import gleam/option.{Some}
import gleeunit

import day01
import testbase

pub fn testset_test() {
let config =
testbase.Config(
day: "01",
part1: Some(fn(input) {
day01.parse(input) |> day01.part1 |> int.to_string
}),
part2: option.None,
)
testbase.testset_test(config)
}

pub fn main() {
gleeunit.main()
}
19 changes: 19 additions & 0 deletions 2024/solutions/gleam/test-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -euo pipefail

for file in $(find . -name "gleam.toml")
do
dir=$(dirname $file)
# Skip the testbase directory and all directories containing "build" in the path
if [ "$dir" == *"/testbase" ] || [[ "$dir" == *"/build/"* ]]; then
continue
fi
echo "Running tests in $dir"
cd "$dir"
gleam deps download
gleam test
gleam format --check src test
cd -
echo ""
done
echo "Success!"
4 changes: 4 additions & 0 deletions 2024/solutions/gleam/testbase/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
20 changes: 20 additions & 0 deletions 2024/solutions/gleam/testbase/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name = "testbase"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
gleeunit = ">= 1.0.0 and < 2.0.0"
simplifile = ">= 2.2.0 and < 3.0.0"

[dev-dependencies]
14 changes: 14 additions & 0 deletions 2024/solutions/gleam/testbase/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
{ name = "gleam_stdlib", version = "0.52.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "50703862DF26453B277688FFCDBE9DD4AC45B3BD9742C0B370DB62BC1629A07D" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
]

[requirements]
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
simplifile = { version = ">= 2.2.0 and < 3.0.0" }
56 changes: 56 additions & 0 deletions 2024/solutions/gleam/testbase/src/testbase.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import gleam/io
import gleam/list
import gleam/option.{type Option, None, Some}
import gleam/string
import gleeunit/should
import simplifile

pub type Config {
Config(
day: String,
part1: Option(fn(String) -> String),
part2: Option(fn(String) -> String),
)
}

fn get_output_filename(input_filename) {
string.replace(input_filename, each: "-input", with: "-output")
}

pub fn testset_test(config: Config) {
let basepath = "../../../puzzles/" <> config.day <> "/"
let assert Ok(entries) = simplifile.read_directory(basepath)
list.filter(entries, fn(entry) { string.ends_with(entry, "-input.txt") })
|> list.each(fn(input_file) {
let assert Ok(input) = simplifile.read(basepath <> input_file)
let output_file = get_output_filename(input_file)
let assert Ok(output) = simplifile.read(basepath <> output_file)
let assert [part1_expected, part2_expected] =
output
|> string.trim
|> string.split("\n")
|> list.map(fn(line) {
let trimmed = string.trim(line)
case trimmed {
"-" -> None
x -> Some(x)
}
})
case config.part1, part1_expected {
Some(part1), Some(expected) -> {
let actual = part1(input)
io.println("Part 1: " <> actual)
should.equal(actual, expected)
}
_, _ -> Nil
}
case config.part2, part2_expected {
Some(part2), Some(expected) -> {
let actual = part2(input)
io.println("Part 2: " <> actual)
should.equal(actual, expected)
}
_, _ -> Nil
}
})
}
12 changes: 0 additions & 12 deletions 2024/solutions/roc/.github/dependabot.yml

This file was deleted.

0 comments on commit 23e77b1

Please sign in to comment.