Basic structure; day 1, 2

This commit is contained in:
bluepython508
2024-12-03 10:34:33 +00:00
commit b24b64a22c
12 changed files with 2164 additions and 0 deletions

2
.envrc Normal file
View File

@@ -0,0 +1,2 @@
use flake
dotenv_if_exists

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/.env
/.precomp/
/.direnv/

41
Justfile Normal file
View File

@@ -0,0 +1,41 @@
default:
just --list
_curl day url *args:
curl --cookie "session=$AOC_SESSION" --user-agent "AOC curl scripts by bluepython508" {{args}} https://adventofcode.com/2024/day/{{trim_start_match(day, '0')}}/{{url}}
day := `date +'%d'`
new day=day:
#!/usr/bin/env bash
set -euxo pipefail
mkdir day-{{day}}/
just _curl {{day}} input -o day-{{day}}/input
cat > day-{{day}}/main.raku <<'MAIN'
use lib $*PROGRAM.parent.parent;
use Lib;
grammar Solution does AOC {
token TOP { }
method part1(::?CLASS:U: $v) {
}
method part2(::?CLASS:U: $v) {
}
}
Solution.main()
MAIN
run day=day:
raku day-{{day}}/main.raku day-{{day}}/input
test day=day:
#!/usr/bin/env bash
for f in day-{{day}}/example-*.in; do
raku day-{{day}}/main.raku $f
done
watch day=day:
watchexec just test {{day}}

11
Lib.rakumod Normal file
View File

@@ -0,0 +1,11 @@
role AOC {
method part1(::?CLASS:U: $v) { ... }
method part2(::?CLASS:U: $v) { ... }
method main(::?CLASS:U:) {
my \parsed = self.parse(slurp(@*ARGS[0])).made;
say self.part1(parsed);
say self.part2(parsed);
}
}

6
day-01/example-1 Normal file
View File

@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

1000
day-01/input Normal file

File diff suppressed because it is too large Load Diff

21
day-01/main.raku Normal file
View File

@@ -0,0 +1,21 @@
use lib $*PROGRAM.parent.parent;
use Lib;
grammar Solution does AOC {
token TOP { (^^ <line> \n)+ { make ([Z] $/[0]>><line>>>.made).List; }}
token line { <num> <.ws> <num> { make $<num>>>.made }}
token num { \d+ { make val($/.Str) }}
method part1(::?CLASS:U: (@l, @r)) {
[+] (@l.sort <<->> @r.sort)>>.abs
}
method part2(::?CLASS:U: (@l, @r)) {
my $bag = @r.Bag;
[+] @l.map(-> $l { $l * $bag{$l} })
}
}
Solution.main()

6
day-02/example-1.in Normal file
View File

@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9

1000
day-02/input Normal file

File diff suppressed because it is too large Load Diff

24
day-02/main.raku Normal file
View File

@@ -0,0 +1,24 @@
use lib $*PROGRAM.parent.parent;
use Lib;
sub safe($report) {
my $diff = $report.rotor(2=>-1).map({ $_[1] - $_[0] }).List;
so (1 <= $diff.all <= 3 or -3 <= $diff.all <= -1)
}
grammar Solution does AOC {
token TOP { (^^ <report> \n)+ { make $/[0]>><report>>>.made; } }
token report { (<num> \h*)+ { make $/[0]>><num>>>.made;} }
token num { \d+ { make val($/.Str) } }
method part1(::?CLASS:U: $v) {
$v.map(&safe).Bag{True};
}
method part2(::?CLASS:U: $v) {
$v.map({so safe(.combinations(.elems - 1).any)}).Bag{True}
}
}
Solution.main()

39
flake.lock generated Normal file
View File

@@ -0,0 +1,39 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1730200266,
"narHash": "sha256-l253w0XMT8nWHGXuXqyiIC/bMvh1VRszGXgdpQlfhvU=",
"path": "/nix/store/m68ikm8045fj7ys7qvgr608z9l70hh1k-source",
"rev": "807e9154dcb16384b1b765ebe9cd2bba2ac287fd",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"systems": "systems"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"id": "systems",
"type": "indirect"
}
}
},
"root": "root",
"version": 7
}

11
flake.nix Normal file
View File

@@ -0,0 +1,11 @@
{
outputs = { self, nixpkgs, systems }: let
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f { inherit system; pkgs = nixpkgs.legacyPackages.${system}; });
in {
devShells = eachSystem ({pkgs, ...}: {
default = pkgs.mkShell {
packages = [pkgs.rakudo pkgs.just pkgs.watchexec];
};
});
};
}