diff options
author | sterni <sternenseemann@systemli.org> | 2022-12-10T20·24+0100 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-12-10T20·29+0000 |
commit | c381fa64cf9d375290c76436faf1078bc8d81ff7 (patch) | |
tree | 8a0844d0fa3f729729485fa56fea7cbd09331c5d /users | |
parent | 42a4f1487899b9a23db462d048d22b138957b40c (diff) |
feat(sterni/aoc/2022): day10 BQN solution r/5403
I cheated a bit to skip implementing multi cycle instructions. The VM is pretty much a normal tail recursive function, but working with scalars in BQN is on brand-ish. Array programming helps again when drawing the picture on screen. Change-Id: I2562c862e228f633c5fad09e503529c6e0785112 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7556 Tested-by: BuildkiteCI Reviewed-by: sterni <sternenseemann@systemli.org> Autosubmit: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'users')
-rw-r--r-- | users/sterni/exercises/aoc/2022/10/10.bqn | 25 | ||||
-rw-r--r-- | users/sterni/exercises/aoc/lib.bqn | 6 |
2 files changed, 30 insertions, 1 deletions
diff --git a/users/sterni/exercises/aoc/2022/10/10.bqn b/users/sterni/exercises/aoc/2022/10/10.bqn new file mode 100644 index 000000000000..59cfa0e8d3c8 --- /dev/null +++ b/users/sterni/exercises/aoc/2022/10/10.bqn @@ -0,0 +1,25 @@ +⟨SplitOn,ReadDec⟩ ← •Import "../../lib.bqn" +# Instead of implementing the VM described in the problem, translate the +# program to instructions with equivalent timing for a similar VM that +# only needs 1 cycle for every instruction. +is ← ∾{"noop": <"noop"; 𝕩: (<"noop")∾<ReadDec⌾(1⊸⊑) (<' ') SplitOn 𝕩}¨ •FLines "input" + +Op ← {x 𝕊 "noop": x;x 𝕊 "addx"‿i: x+i} +Draw ← {𝕊 c‿x‿pic: pic∨(↕240)((c-1)⊸=∘⊣∧∊)(⌊⌾(÷⟜40)c)+¯1+x+↕3} +_vm ← { + is _self s: (⊑s)≥≠is? s; + is _self prev‿sum‿x‿pic: + cycle ← prev+1 + is _self ⟨ + cycle, + sum+x×cycle×⊑cycle∊20‿60‿100‿140‿180‿220, + x Op (¯1+cycle)⊑is, + Draw cycle‿x‿pic + ⟩ +} + +·‿sum‿·‿pic ← is _vm 1‿0‿1‿(240⥊0) + +•Out "day10.1: "∾•Fmt sum +•Out "day10.2:" +•Show (⊑⟜".#")¨ ∘‿40⥊pic diff --git a/users/sterni/exercises/aoc/lib.bqn b/users/sterni/exercises/aoc/lib.bqn index 636eeace78d2..172343b41979 100644 --- a/users/sterni/exercises/aoc/lib.bqn +++ b/users/sterni/exercises/aoc/lib.bqn @@ -1,6 +1,10 @@ IsAsciiNum ⇐ ('0'⊸≤∧≤⟜'9') -ReadInt ⇐ {(𝕨⊸×+⊣)´∘⌽-⟜'0'𝕩} # stolen from leah2 +# based on leah2's function +ReadInt ⇐ { + 𝕨 𝕊 𝕩: '-'=⊑𝕩? -𝕨 𝕊 1↓𝕩; + 𝕨 𝕊 𝕩: (𝕨⊸×+⊣)´∘⌽-⟜'0'𝕩 +} ReadDec ⇐ 10⊸ReadInt SplitOn ⇐ ((⊢ (-1˙)⍟⊣¨ +`∘(1⊸»<⊢))∘(≡¨)⊔⊢) |