From 2f65fbd131d47fc1a36140830994c1c77b20eabc Mon Sep 17 00:00:00 2001 From: Defelo Date: Sun, 1 Dec 2024 14:48:38 +0100 Subject: [PATCH] Ruby/2024/01: add solution --- .assets/rb.svg | 948 +++++++++++++++++++++++++++++++++++++ README.md | 6 +- Ruby/2024/01.rb | 4 + Ruby/justfile | 23 + flake.nix | 4 + scripts/generate-readme.py | 22 +- 6 files changed, 1002 insertions(+), 5 deletions(-) create mode 100644 .assets/rb.svg create mode 100644 Ruby/2024/01.rb create mode 100644 Ruby/justfile diff --git a/.assets/rb.svg b/.assets/rb.svg new file mode 100644 index 0000000..59cf324 --- /dev/null +++ b/.assets/rb.svg @@ -0,0 +1,948 @@ + + + +image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index ed41109..c39c8d9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # AdventOfCode -[Advent of Code](https://adventofcode.com/) solutions in [ Rust](Rust), [ Uiua](Uiua), [ Python](Python), [ APL](APL), [ Haskell](Haskell), [ Nix](Nix) and [ Nu](Nu) +[Advent of Code](https://adventofcode.com/) solutions in [ Rust](Rust), [ Uiua](Uiua), [ Python](Python), [ APL](APL), [ Haskell](Haskell), [ Nix](Nix), [ Nu](Nu) and [ Ruby](Ruby) ### Global Leaderboard Placement |Year|Rank|Score|Top% (Leaderboard)|Top% (All participants)| @@ -9,10 +9,10 @@ |[2021](https://adventofcode.com/2021/leaderboard)|**136**|438|13.41% (of 1014)|0.0532% (of ≥255548)| |[2020](https://adventofcode.com/2020/leaderboard)|**621**|46|65.23% (of 952)|0.3146% (of ≥197402)| -## [2024](https://adventofcode.com/2024) ([ Rust](Rust/2024): 1/25 | [ Haskell](Haskell/2024): 1/25 | [ Uiua](Uiua/2024): 1/25 | [ Nix](Nix/2024): 1/25) +## [2024](https://adventofcode.com/2024) ([ Rust](Rust/2024): 1/25 | [ Haskell](Haskell/2024): 1/25 | [ Uiua](Uiua/2024): 1/25 | [ Nix](Nix/2024): 1/25 | [ Ruby](Ruby/2024): 1/25) |Mo|Tu|We|Th|Fr|Sa|Su| |-|-|-|-|-|-|-| -|||||||[**1**](https://adventofcode.com/2024/day/1) [](Rust/2024/01.rs "Rust solution for 2024/01") [](Haskell/2024/01.hs "Haskell solution for 2024/01") [](https://uiua.org/pad?src=JnNjCgrCsOKKn-KNieKKnOKKnOKLleKIqeKJoEBcbixAIC4K4oipLyviioMo4pmtw5fiip7iirg9fOKMtS3iiKnijYYpCg== "Uiua solution for 2024/01") [](Nix/2024/01.nix "Nix solution for 2024/01")| +|||||||[**1**](https://adventofcode.com/2024/day/1) [](Rust/2024/01.rs "Rust solution for 2024/01") [](Haskell/2024/01.hs "Haskell solution for 2024/01") [](https://uiua.org/pad?src=JnNjCgrCsOKKn-KNieKKnOKKnOKLleKIqeKJoEBcbixAIC4K4oipLyviioMo4pmtw5fiip7iirg9fOKMtS3iiKnijYYpCg== "Uiua solution for 2024/01") [](Nix/2024/01.nix "Nix solution for 2024/01") [](Ruby/2024/01.rb "Ruby solution for 2024/01")| |[**2**](https://adventofcode.com/2024/day/2)|[**3**](https://adventofcode.com/2024/day/3)|[**4**](https://adventofcode.com/2024/day/4)|[**5**](https://adventofcode.com/2024/day/5)|[**6**](https://adventofcode.com/2024/day/6)|[**7**](https://adventofcode.com/2024/day/7)|[**8**](https://adventofcode.com/2024/day/8)| |[**9**](https://adventofcode.com/2024/day/9)|[**10**](https://adventofcode.com/2024/day/10)|[**11**](https://adventofcode.com/2024/day/11)|[**12**](https://adventofcode.com/2024/day/12)|[**13**](https://adventofcode.com/2024/day/13)|[**14**](https://adventofcode.com/2024/day/14)|[**15**](https://adventofcode.com/2024/day/15)| |[**16**](https://adventofcode.com/2024/day/16)|[**17**](https://adventofcode.com/2024/day/17)|[**18**](https://adventofcode.com/2024/day/18)|[**19**](https://adventofcode.com/2024/day/19)|[**20**](https://adventofcode.com/2024/day/20)|[**21**](https://adventofcode.com/2024/day/21)|[**22**](https://adventofcode.com/2024/day/22)| diff --git a/Ruby/2024/01.rb b/Ruby/2024/01.rb new file mode 100644 index 0000000..283fcb2 --- /dev/null +++ b/Ruby/2024/01.rb @@ -0,0 +1,4 @@ +left, right = File.read(ARGV[0]).lines.map { _1.split.map &:to_i }.transpose + +puts left.sort.zip(right.sort).map { _1.reduce(&:-).abs }.sum +puts left.map { _1 * right.count(_1) }.sum diff --git a/Ruby/justfile b/Ruby/justfile new file mode 100644 index 0000000..0797180 --- /dev/null +++ b/Ruby/justfile @@ -0,0 +1,23 @@ +alias r := run +alias e := example +alias t := test +alias ty := test-year +alias ta := test-all + +_default: + @just --list + +run year day: + ruby {{year}}/{{day}}.rb ../.cache/{{year}}/{{trim_start_match(day, "0")}} + +example year day ex: + ruby {{year}}/{{day}}.rb ../examples/{{year}}/{{trim_start_match(day, "0")}}/{{ex}} + +test year day: + @diff <(just run {{year}} {{day}}) <(cat ../.cache/{{year}}/{{trim_start_match(day, "0")}}.{1,2}) + +test-year year: + @set -e; for day in $(ls {{year}}); do just test {{year}} $(basename $day .rb); done + +test-all: + @set -e; for year in *; do [[ -d $year ]] && [[ "$year" != "lib" ]] || continue; just test-year $year; done diff --git a/flake.nix b/flake.nix index 8a6309f..1d99a3f 100644 --- a/flake.nix +++ b/flake.nix @@ -153,6 +153,10 @@ # Nushell nushell + + # Ruby + ruby + solargraph ]; PYTHONPATH = "."; LIBCLANG_PATH = with pkgs; lib.makeLibraryPath [llvmPackages.clang-unwrapped.lib]; diff --git a/scripts/generate-readme.py b/scripts/generate-readme.py index f32ecc1..a717f6c 100644 --- a/scripts/generate-readme.py +++ b/scripts/generate-readme.py @@ -5,8 +5,26 @@ LATEST_YEAR = 2024 -names = {"rs": "Rust", "hs": "Haskell", "py": "Python", "apl": "APL", "ua": "Uiua", "nix": "Nix", "nu": "Nu"} -exts = {"rs": [".rs"], "hs": [".hs"], "py": [".py"], "apl": [".apl"], "ua": [".ua"], "nix": [".nix"], "nu": [".nu"]} +names = { + "rs": "Rust", + "hs": "Haskell", + "py": "Python", + "apl": "APL", + "ua": "Uiua", + "nix": "Nix", + "nu": "Nu", + "rb": "Ruby", +} +exts = { + "rs": [".rs"], + "hs": [".hs"], + "py": [".py"], + "apl": [".apl"], + "ua": [".ua"], + "nix": [".nix"], + "nu": [".nu"], + "rb": [".rb"], +} logos = {k.name.split(".")[0]: str(k) for k in Path(".assets").iterdir()}