about summary refs log tree commit diff
path: root/users/sterni/exercises/aoc/2022/21/21.bqn
blob: 2f91f55d445e6869977c6bec98d7d8575cc23382 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
⟨ImportBqnLibs, IsAsciiNum, ReadDec⟩ ← •Import "../../lib.bqn"
⟨ReplaceAll, Split⟩ ← ImportBqnLibs "strings.bqn"

i ← ": "⊸Split¨ •FLines "input"
ReplaceInts ← {
  𝕊 𝕩: 𝕊´ 2↑(¬∘(∧´IsAsciiNum∘⊑∘⌽)¨⊔⊢) 𝕩;
  # TODO: Efficient replace on tokens
  is 𝕊 es: (((•Fmt⍟(0⊸≠•Type))¨⌾(1⊸⊑) <˘⍉>is)⊸ReplaceAll⌾(1⊸⊑))¨ es
}

c ← 0
CanEval ← (IsAsciiNum∨∊⟜"+-/* ")
Eval ← {
  a‿s‿b ← " " Split 𝕩
  f ← ⊑+‿-‿×‿÷⊏˜"+-*/"⊐s
  a F○ReadDec b
}
EvalExprs ← {
  p‿e ← 2↑((∧´CanEval∘⊑∘⌽)¨⊔⊢) 𝕩
  ev ← (Eval⌾(⊑⌽))¨ e
  c +↩1
  (⊑(⊑¨ev)∊˜<"root")◶⟨EvalExprs∘(ReplaceInts⟜p),1⊸⊑⊑⟩ ev
}

•Show EvalExprs ReplaceInts i