From 867210f0846be912f0272ec88fb430c06f4256cc Mon Sep 17 00:00:00 2001 From: sterni Date: Tue, 13 Dec 2022 00:43:08 +0100 Subject: feat(sterni/aoc/2022): day11 BQN solution Change-Id: If9c510e3530ecc1acafa1d708eacf64eb1132db7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7583 Reviewed-by: sterni Autosubmit: sterni Tested-by: BuildkiteCI --- users/sterni/exercises/aoc/2022/11/11.bqn | 41 +++++++++++++++++++++++++++++ users/sterni/exercises/aoc/2022/default.nix | 7 +++++ 2 files changed, 48 insertions(+) create mode 100644 users/sterni/exercises/aoc/2022/11/11.bqn (limited to 'users/sterni/exercises') diff --git a/users/sterni/exercises/aoc/2022/11/11.bqn b/users/sterni/exercises/aoc/2022/11/11.bqn new file mode 100644 index 000000000000..926fa951df56 --- /dev/null +++ b/users/sterni/exercises/aoc/2022/11/11.bqn @@ -0,0 +1,41 @@ +# needs export BQNLIBS=/path/to/mlochbaum/bqn-libs +⟨Split⟩ ← •Import "/strings.bqn"∾˜¯1↓1⊑•SH "printenv"‿"BQNLIBS" +⟨ReadDec⟩ ← •Import "../../lib.bqn" +MakeOp ← { + 𝕊 a‿"+"‿b: 𝕊 a‿+‿b; + 𝕊 a‿"*"‿b: 𝕊 a‿×‿b; + 𝕊 a‿op‿b: + is‿xs ← (<"old") (≡¨⊔⊢) a‿b + {op´ (𝕩⋆≠xs) ∾ReadDec¨ is} +} +ParseMonkey ← { + ·‿items‿op‿if‿then‿else: + { + initial ⇐ ReadDec¨ ", " Split 18↓items + op ⇐ MakeOp " " Split 19↓op + if ⇐ ReadDec 21↓if + then ⇐ ReadDec 29↓then + else ⇐ ReadDec 30↓else + } +} +monkeys ← ParseMonkey¨ 1↓' '((+`(≠⟜⊑)¨)⊔⊢)0(≠⟜≠¨/⊢)•FLines "input" +items ← {𝕩.initial}¨ monkeys +lim ← ×´{𝕩.if}¨ monkeys + +Sim ← { + div 𝕊 len: + Turn ← { + items 𝕊 turnidx: + i ← (≠monkeys)|turnidx + m ← i⊑monkeys + + worry ← lim|⌊div÷˜ m.Op¨ i⊑items + else‿then ← 2↑0 (=⟜(m.if⊸|)⊔⊢) worry + + ⟨then, else⟩⊸(∾˜¨)⌾(m.then‿m.else⊸⊏) ⟨⟩˙⌾(i⊸⊑) items + } + ×´2↑∨+˝(((↕⊣)=|)¨)×(≠¨˘)∘>∘(⊣»Turn`)) ↕len×≠items +} + +•Out "day11.1: "∾•Fmt 3 Sim 20 +•Out "day11.2: "∾•Fmt 1 Sim 10000 diff --git a/users/sterni/exercises/aoc/2022/default.nix b/users/sterni/exercises/aoc/2022/default.nix index 23dc2ef70103..82d743428ab0 100644 --- a/users/sterni/exercises/aoc/2022/default.nix +++ b/users/sterni/exercises/aoc/2022/default.nix @@ -15,6 +15,13 @@ depot.nix.readTree.drvTargets { ngn-k depot.tvix.eval ]; + + BQNLIBS = pkgs.fetchFromGitHub { + owner = "mlochbaum"; + repo = "bqn-libs"; + rev = "d56d8ea0b8c294fac7274678d9ab112553a03f42"; + sha256 = "1c1bkqj62v8m13jgaa32ridy0fk5iqysq5b2qwxbqxhky5zwnk9h"; + }; }; bqn = pkgs.runCommand "bqn-aoc-2022" -- cgit 1.4.1