From 4cdc3bbf23d2b7ce96d5d256c990d44746def85c Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sun, 10 Dec 2023 04:57:55 +0000 Subject: [PATCH] Add 2022 day 4 --- 2022/4/4.jsonnet | 18 ++++++++++++++++++ 2022/4/default.nix | 14 ++++++++++++++ treefmt.nix | 1 + 3 files changed, 33 insertions(+) create mode 100644 2022/4/4.jsonnet create mode 100644 2022/4/default.nix diff --git a/2022/4/4.jsonnet b/2022/4/4.jsonnet new file mode 100644 index 0000000..586a07d --- /dev/null +++ b/2022/4/4.jsonnet @@ -0,0 +1,18 @@ +local input = importstr './input'; + +local parsedInput = [[[std.parseInt(z) for z in std.split(y, '-')] for y in std.split(x, ',')] for x in std.split(input, '\n')]; + +local part1 = std.sum([ + if ((x[0][0] >= x[1][0] && x[0][1] <= x[1][1]) || (x[1][0] >= x[0][0] && x[1][1] <= x[0][1])) then 1 else 0 + for x in parsedInput +]); + +local part2 = std.sum([ + if (x[0][0] <= x[1][1] && x[0][1] >= x[1][0]) then 1 else 0 + for x in parsedInput +]); + +{ + part1: part1, + part2: part2, +} diff --git a/2022/4/default.nix b/2022/4/default.nix new file mode 100644 index 0000000..00c0efe --- /dev/null +++ b/2022/4/default.nix @@ -0,0 +1,14 @@ +{ pkgs ? import { } }: +pkgs.stdenv.mkDerivation rec { + name = "aoc-2022-4"; + src = ./.; + + phases = [ "buildPhase" ]; + doCheck = true; + + buildInputs = with pkgs; [ go-jsonnet ]; + + buildPhase = '' + jsonnet $src/4.jsonnet > $out + ''; +} diff --git a/treefmt.nix b/treefmt.nix index 9c9d5e7..288585d 100644 --- a/treefmt.nix +++ b/treefmt.nix @@ -9,5 +9,6 @@ statix.enable = true; rustfmt.enable = true; fprettify.enable = true; + jsonnetfmt.enable = true; }; } -- libgit2 1.7.2