Skip to content

Commit

Permalink
return to day 3 part 1, partial
Browse files Browse the repository at this point in the history
  • Loading branch information
qqii committed Dec 5, 2023
1 parent 3638e52 commit eee061a
Showing 1 changed file with 37 additions and 48 deletions.
85 changes: 37 additions & 48 deletions day3.bqn
Original file line number Diff line number Diff line change
@@ -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.."
Expand All @@ -27,52 +15,54 @@ 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"˜𝕩}/⍷
symbolsPad (GetSymbols e1) ˜ >e1
IsDigit ← {('0' 𝕩) (𝕩 '9')}
# From BQNCrate: Split 𝕩 at occurrences of separators 𝕨, removing the separators
SplitSep ← {𝕨 ((⊢-˜¬×·+`»<)˜⊔⊢) 𝕩}
LineNumbers•parseFloat¨ ({0≠≠¨𝕩}/('.'symbols)SplitSep)
NumberIndex1 ← {
maskIsDigit 𝕩
# sum reduce over the falling edge
# add 1 to distinguish between 0s
mask × 1+ +` (»mask) ¬mask
digitsPad (IsDigit×-'0') >e1

# We need to pad these because Grow wraps the grid
parsedsymbols ¨ digits

# Adapted from BQNcrate GoL to just sum the neighbours to 1 step propogate symbols
Grow ← {0<⊑+˝⥊⌽𝕩¨⌜˜¯101}
# Cull symbols where digits is 0, padding is always culled
Cull ← {
sd𝕩
(0s×d)‿d
}
# Spread symbols left and right
Spead ← {
s¨ 𝕩
neighbourhoods¨⌜˜¯101
spread0<+˝˘ 11 neighbourhood
spread ¨ 1¨ 𝕩
}
Part1 ← {(0(LineNumbers 𝕩))¨ (NumberIndex1 𝕩)}

# Part 2

GetSymbols>{´𝕩symbols}¨¨

# Part 3
# Butchered from GoL found on BQNcrate
Propagate ← {1=+˝⥊⌽𝕩¨⌜˜¯101}
Harvest ← {
sd𝕩
d ×s
(d > 0)'.'‿('0'+d)
}

(>Part1¨ e1) × (PropagateGetSymbols e1)
# Adapted BQNCrate split for digits only
SplitDigits ← {((⊢-˜¬×·+`»<)¬IsDigit˜⊔⊢) 𝕩}
MaxDigitLen ← {´ ¨ SplitDigits¨ 𝕩}

initCull¨ Grow (¨) parsed
# If we grew into a digit, we need to spread until we reach the entire number
hHarvest¨ Cull¨Spead(1-˜MaxDigitLen e1) init

# WIP, now to unravel and parse as floats





# 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.

Expand All @@ -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⟩

0 comments on commit eee061a

Please sign in to comment.