From eee061a5b30aac46cc93e110af7eaa48646bb90d Mon Sep 17 00:00:00 2001 From: qqii Date: Tue, 5 Dec 2023 03:56:16 +0000 Subject: [PATCH] return to day 3 part 1, partial --- day3.bqn | 85 ++++++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 48 deletions(-) diff --git a/day3.bqn b/day3.bqn index edbff4d..b35d84b 100644 --- a/day3.bqn +++ b/day3.bqn @@ -1,18 +1,6 @@ #!/usr/bin/env cbqn -# You (me) notice that no number is more than 3 digits long. Why not take a -# different approach: - -# 1. Find symbols in grid via "#$%&*+-/=@" -# 2. Propagate symbols in a moore neighbourhood to digits -# 3. Propagate twice more over any L-R digits -# 4. Mask the grid -# 5. Parse and sum - -# Written after finishing the final tutorial, and skimming the "Quick Start". -# Understanding arrays might have have been really useful, but I guess I'm out -# of luck as they seems to have really increased the difficulty this year to -# combat LLMs. +# Written after day 4, because of a logical error with the first attempt. e1 ← ⟨ "467..114.." @@ -27,44 +15,46 @@ e1 ← ⟨ ".664.598.." ⟩ -# Ah fuck. this idea is _wrong_ -# Game plan: -# Part 1: -# Somehow "467..114.." -> ⟨467, 467, 467, 0, 0, 114, 114, 114, 0, 0⟩ -# Part 2: -# Identify non digit and non . as 1, others as 0 -# Part 3: -# Propagate all 1s in the part 2 grid in a moore neighbourhood -# Part 4: -# Multiply part 3 on part 1 and sum - -# Part 1 -# Symbols used (from ∧⍷∾ on input): #$%&*+-/=@ . 0123456789 -symbols ← "#$%&*+-/=@" -# Not very idomatic ;( +# From BQNcreate: Pad with a layer of fill elements on all sides +Pad ← (⊢↑˝·≍⟜¬2+≢) + +GetSymbols ← {¬".0123456789"∊˜𝕩}⊸/⍷∘∾ +symbols ← Pad (GetSymbols e1) ∊˜ >e1 IsDigit ← {('0' ≤ 𝕩) ∧ (𝕩 ≤ '9')} -# From BQNCrate: Split 𝕩 at occurrences of separators 𝕨, removing the separators -SplitSep ← {𝕨 ((⊢-˜¬×·+`»⊸<)∘∊˜⊔⊢) 𝕩} -LineNumbers ← •parseFloat¨ ({0≠≠¨𝕩}⊸/('.'∾symbols)⊸SplitSep) -NumberIndex1 ← { - mask ← IsDigit 𝕩 - # sum reduce over the falling edge - # add 1 to distinguish between 0s - mask × 1+ +` (»mask) ∧ ¬mask +digits ← Pad (IsDigit×-⟜'0') >e1 + +# We need to pad these because Grow wraps the grid +parsed ← symbols ⋈¨ digits + +# Adapted from BQNcrate GoL to just sum the neighbours to 1 step propogate symbols +Grow ← {0<⊑+˝⥊⌽⟜𝕩¨⋈⌜˜¯1‿0‿1} +# Cull symbols where digits is 0, padding is always culled +Cull ← { + s‿d ← 𝕩 + (0≠s×d)‿d +} +# Spread symbols left and right +Spead ← { + s ← ⊑¨ 𝕩 + neighbourhood ← ⌽⟜s¨⋈⌜˜¯1‿0‿1 + spread ← ⊑0<+˝˘ 1↑1↓ neighbourhood + spread ⋈ ¨ 1⊑¨ 𝕩 } -Part1 ← {⊑⟜(0∾(LineNumbers 𝕩))¨ (NumberIndex1 𝕩)} - -# Part 2 - -GetSymbols ← >{∨´𝕩⍷symbols}¨¨ - -# Part 3 -# Butchered from GoL found on BQNcrate -Propagate ← {⊑1=+˝⥊⌽⟜𝕩¨⋈⌜˜¯1‿0‿1} +Harvest ← { + s‿d ← 𝕩 + d ×⟜s ↩ + (d > 0)⊑'.'‿('0'+d) +} -(>Part1¨ e1) × (Propagate∘GetSymbols e1) +# Adapted BQNCrate split for digits only +SplitDigits ← {((⊢-˜¬×·+`»⊸<)∘¬∘IsDigit˜⊔⊢) 𝕩} +MaxDigitLen ← {⌈´ ≠¨ ∾ SplitDigits¨ 𝕩} +init ← Cull¨ Grow ⌾ (⊑¨) parsed +# If we grew into a digit, we need to spread until we reach the entire number +h ← Harvest¨ Cull¨∘Spead⍟(1-˜MaxDigitLen e1) init +# WIP, now to unravel and parse as floats @@ -72,7 +62,7 @@ Propagate ← {⊑1=+˝⥊⌽⟜𝕩¨⋈⌜˜¯1‿0‿1} # Thoughts: -# The quick start really shows off a lot more BQN that I ever thought existed! +# The Quick Start really shows off a lot more BQN that I ever thought existed! # There's syntax to do pseudo-procedural programming! I wonder how much overlap # there is with other array languages. @@ -87,6 +77,5 @@ Propagate ← {⊑1=+˝⥊⌽⟜𝕩¨⋈⌜˜¯1‿0‿1} # I can try moving to local development. input ← •FLines "inputs/day3.txt" -•Show ⟨"Symbols: ", ∧⍷∾ input⟩ # •Show ⟨"Part 1", input⟩ # •Show ⟨"Part 2", input⟩ \ No newline at end of file