about summary refs log tree commit diff
path: root/users/sterni/exercises/aoc
diff options
context:
space:
mode:
Diffstat (limited to 'users/sterni/exercises/aoc')
-rw-r--r--users/sterni/exercises/aoc/2022/01/1.bqn7
-rw-r--r--users/sterni/exercises/aoc/2022/01/1.k2
-rw-r--r--users/sterni/exercises/aoc/2022/default.nix3
-rw-r--r--users/sterni/exercises/aoc/2022/nix.nix31
4 files changed, 40 insertions, 3 deletions
diff --git a/users/sterni/exercises/aoc/2022/01/1.bqn b/users/sterni/exercises/aoc/2022/01/1.bqn
new file mode 100644
index 000000000000..f95851e0d93e
--- /dev/null
+++ b/users/sterni/exercises/aoc/2022/01/1.bqn
@@ -0,0 +1,7 @@
+lib ← •Import •path∾"/../../lib.bqn"
+input ← lib.ReadDec¨¨ (<"") lib.SplitOn •FLines •path∾"/input"
+
+cpe ← +´¨ input
+
+•Out "day 01.1: "∾•Fmt ⌈´cpe
+•Out "day 01.2: "∾•Fmt +´3↑∨cpe
diff --git a/users/sterni/exercises/aoc/2022/01/1.k b/users/sterni/exercises/aoc/2022/01/1.k
new file mode 100644
index 000000000000..295503905fb8
--- /dev/null
+++ b/users/sterni/exercises/aoc/2022/01/1.k
@@ -0,0 +1,2 @@
+ / a better approach is to use the downgraded list for both tasks, as demonstrated by leah2
+(|/e),+/e@3#>e:(+/.'1_)'(&0=#'i)_i:0:"input"
diff --git a/users/sterni/exercises/aoc/2022/default.nix b/users/sterni/exercises/aoc/2022/default.nix
index ef03cd87e173..23dc2ef70103 100644
--- a/users/sterni/exercises/aoc/2022/default.nix
+++ b/users/sterni/exercises/aoc/2022/default.nix
@@ -1,7 +1,7 @@
 { depot, pkgs, lib, ... }:
 
 let
-  inherit (pkgs.buildPackages) cbqn;
+  inherit (pkgs.buildPackages) cbqn ngn-k;
 
   # input files are not checked in
   meta.ci.skip = true;
@@ -12,6 +12,7 @@ depot.nix.readTree.drvTargets {
     name = "aoc-2022-shell";
     packages = [
       cbqn
+      ngn-k
       depot.tvix.eval
     ];
   };
diff --git a/users/sterni/exercises/aoc/2022/nix.nix b/users/sterni/exercises/aoc/2022/nix.nix
index 5bd3e36b5397..ca8bfc6e57df 100644
--- a/users/sterni/exercises/aoc/2022/nix.nix
+++ b/users/sterni/exercises/aoc/2022/nix.nix
@@ -1,3 +1,30 @@
-{ ... }:
+{ lib ? import <nixpkgs/lib> }:
 
-{ }
+let
+  chomp = lib.removeSuffix "\n";
+  lines = s: builtins.filter builtins.isString (builtins.split "\n" (chomp s));
+  sum = builtins.foldl' builtins.add 0;
+
+  day01 =
+    let
+      input =
+        builtins.map
+          (elf:
+            sum (builtins.map builtins.fromJSON (lines elf))
+          )
+          (
+            builtins.filter builtins.isString (
+              builtins.split "\n\n" (builtins.readFile ./01/input)
+            )
+          );
+    in
+    {
+      "1" = builtins.foldl' lib.max (-1) input;
+      "2" = sum (lib.sublist 0 3 (lib.sort (a: b: a >= b) input));
+    };
+
+in
+
+{
+  inherit day01;
+}