diff --git a/aoc24.html b/aoc24.html index d3cb14e..23c81e5 100644 --- a/aoc24.html +++ b/aoc24.html @@ -215,25 +215,25 @@

Coding in Advent

Table of Contents

-
-

Foreword

-
+
+

Foreword

+

Welcome to my Advent of Code solutions for the year 2024. Please read the puzzles' descriptions upstream to get more context. These definitions and imports are shared by the various puzzles: @@ -241,6 +241,8 @@

Foreword

Split ← (¬-˜⊢×·+`»⊸>)∘≠⊔⊢
+Ints ← {•ParseFloat¨((¯1+⊢×·+`»⊸<)𝕩∊'0'+↕10)⊔𝕩}
+SInts ← {•ParseFloat¨((¯1+⊢×·+`»⊸<)𝕩∊'-'∾'0'+↕10)⊔𝕩}
 Input ← {"../supp/aoc/2024/"∾".inp"∾˜•Fmt𝕩}
 _is ← {
  ∾⟨"Computed ", •Fmt(𝔽𝕩).eins, " and ",
@@ -269,9 +271,9 @@ 

Foreword

-
-

I

-
+
+

I

+
I ← {
   inp ← >(•ParseFloat¨' '⊸Split)¨•FLines Input𝕩
@@ -287,9 +289,9 @@ 

I

-
-

II

-
+
+

II

+
II ← {
   inp ← (-´˘·2⊸↕·•ParseFloat¨' '⊸Split)¨•FLines Input𝕩
@@ -307,9 +309,9 @@ 

II

-
-

III

-
+
+

III

+
III ← {
   mem ← •FChars Input𝕩
@@ -330,9 +332,9 @@ 

III

-
-

IV

-
+
+

IV

+
IV ← {
   inp ← >•FLines Input 𝕩
@@ -349,9 +351,9 @@ 

IV

-
-

V

-
+
+

V

+
V ← {
   p‿r ← ",|" •ParseFloat⚇1∘(Split¨)¨ 1↓(∧`⊸+⟨⟩⊸≢¨)⊸⊔ •FLines Input𝕩
@@ -368,9 +370,9 @@ 

V

-
-

VI

-
+
+

VI

+
VI ← {
   hm ← (↕∘≢•HashMap○⥊⊢) inp ← >•Flines Input𝕩
@@ -391,9 +393,9 @@ 

VI

-
-

VII

-
+
+

VII

+
VII ← {
   inp ← (⊑⊸⋈○(•ParseFloat¨' '⊸Split)´':'⊸Split)¨•FLines Input𝕩
@@ -410,9 +412,9 @@ 

VII

-
-

VIII

-
+
+

VIII

+
VIII ← {
   n ← ≠⊑inp‿ant ← (⊢⋈1↓⍷∘⥊)>•FLines Input𝕩
@@ -429,9 +431,9 @@ 

VIII

-
-

IX

-
+
+

IX

+
IX ← {
    f‿s ← <˘⍉↑‿2⥊•ParseFloat∘⋈¨⊑•FLines Input𝕩
@@ -451,9 +453,9 @@ 

IX

-
-

X

-
+
+

X

+

In this problem I have included additional functions to solve it in an array way using the powers of the adjacency matrix. This method is unfortunately slower than simple dynamic programming. @@ -490,9 +492,9 @@

X

-
-

XI

-
+
+

XI

+
XI ← {
   inp ← •ParseFloat¨' 'Split⊑•Flines Input𝕩 ⋄ m ← •HashMap˜⟨⟩
@@ -515,9 +517,9 @@ 

XI

-
-

XII

-
+
+

XII

+
XII ← {
    n‿inp ← ≠⊸⋈>•FLines Input𝕩
@@ -541,15 +543,12 @@ 

XII

-
-

XIII

-
+
+

XIII

+
XIII ← {
- inp ← {𝕊p‿q‿pq:
-   e ← ∾{∾•Parsefloat⎊⟨⟩¨∾'+'Split¨','Split𝕩}¨ p‿q
-   3‿2⥊e∾∾•Parsefloat⎊⟨⟩¨∾'='Split¨','Split pq
- }˘∘‿3⥊⟨⟩⊸≢¨⊸/•FLines Input 𝕩
+ ∘‿3‿2⥊∾Ints¨⟨⟩⊸≢¨⊸/•FLines Input 𝕩
  C ← (-´×⟜⌽˝){𝕨𝔽_𝕣[a,b,c]: (𝔽÷˜(𝕨+c)⌾⊏⋈○𝔽(𝕨+c)⌾(⊏⌽))a≍b} 
  eins‿zwei ⇐ {+´⥊3‿1×⎉1(∧´⌊⊸=)˘⊸/𝕩⊸C˘inp}¨ 0‿1e13
 }
@@ -558,7 +557,23 @@ 

XIII

-"Computed 32026 and 89013607072065 in 0.0044758065s"
+"Computed 480 and 875318608908 in 0.0003234552s"
+
+ + +
+
XIV ← {
+  sm ← 0⥊˜s ← 101‿103 ⋄ inp ← 2‿2⊸⥊˘>SInts¨•FLines Input𝕩
+  Q ← {𝕊r: ×´+´∘⥊¨0‿2‿6‿8⊏⥊(({/𝕩∾1∾𝕩}·⌊÷⟜2)¨s)⊔+˝{1⌾((s|𝕨+r×𝕩)⊸⊑)sm}˝˘inp}
+  eins ⇐ Q 100
+  zwei ⇐ ⊑⍋Q¨↕1e5
+}
+XIV _is 14
+
+
+ +
+"Computed 229069152 and 7383 in 52.1381874919s"
 
diff --git a/hf.html b/hf.html index fc0240b..2a031e4 100644 --- a/hf.html +++ b/hf.html @@ -234,9 +234,9 @@

Helonium's Hartree-Fock program

-
-

Exordium

-
+
+

Exordium

+

We will implement the Hartree-Fock1 program from the classic Szabo-Ostlund text, a staple in quantum chemistry. If you have any experience in the field, chances are you know it well. @@ -286,9 +286,9 @@

Exordium

-
-

Basis set

-
+
+

Basis set

+

Basis sets are used to transform the PDEs into linear algebra problems. Physical intuition suggests that Slater type orbitals4 are a good choice for our Hamiltonian. However, the computation of the integrals @@ -306,9 +306,9 @@

Basis set

-
-

Electronic integrals

-
+
+

Electronic integrals

+

Constructing the integrals' tensor is complicated6 and is the main reason for the poor scaling of electronic structure methods. The \(1s\) orbitals are the simplest case, and here two types of integrals @@ -387,7 +387,7 @@

Electronic integrals

-
+
 Got 38006 samples
 (self-hosted runtime1): 1067 samples
 (REPL): 36939 samples:
@@ -416,9 +416,9 @@ 

Electronic integrals

-
-

Fock matrix

-
+
+

Fock matrix

+

The following function constructs the Fock matrix, our approximation to the true Hamiltonian of the system:

@@ -472,9 +472,9 @@

Fock matrix

-
-

Self-consistent field

-
+
+

Self-consistent field

+

The final stage of the computation involves solving the pseudo-eigenvalue problem using a fixed-point iteration. This process is commonly known as the self-consistent field method, a term coined by D. R. Hartree. diff --git a/nm.html b/nm.html index 6301863..396c4d8 100644 --- a/nm.html +++ b/nm.html @@ -197,13 +197,13 @@

A numerical methods bonsai display

-
-

Foreword

+
+

Foreword

-
-

Bibliography

-
+
+

Bibliography

+

These are my favorite books on numerical analysis, in descending order of preference. I use the first two very often, and consult the others to deepen my understanding: diff --git a/nn.html b/nn.html index e5710a9..4af352d 100644 --- a/nn.html +++ b/nn.html @@ -234,9 +234,9 @@

The miniaturist's neural network (WIP)

-
-

Preface

-
+
+

Preface

+

We will implement a fully-connected feed-forward neural network1, in other words, a

@@ -280,9 +280,9 @@

Preface

-
-

Tinn's diminution

-
+
+

Tinn's diminution

+

The original C implementation has 175 lines excluding the optimization loop. The BQN version has only

@@ -355,9 +355,9 @@

Tinn's diminution

-
-

Learning the logistic map

-
+
+

Learning the logistic map

+

_minn should handle digit recognition just fine3. However, I would like to switch clichés for the demonstration. Instead, we will use it to learn the logistic map4. This is a quintessential example of how chaos can emerge from simple systems. diff --git a/qbqn.html b/qbqn.html index 896f37e..06f08a3 100644 --- a/qbqn.html +++ b/qbqn.html @@ -197,9 +197,9 @@

BQN's Quantum Noise

-
-

Preamble

-
+
+

Preamble

+

We will implement and test a Schrödinger-style1 quantum simulator in the BQN2 programming language. Initially, we import the necessary system functions and define a 1-modifier for handling @@ -207,7 +207,7 @@

Preamble

-
Sin‿Cos‿GCD ← •math
+
Sin‿Cos‿GCD ← •math
 U ← •rand.Range
 _cp ← {(-´𝔽¨)⋈(+´𝔽¨)⟜⌽}
 
@@ -221,16 +221,16 @@ 

Preamble

-
-

Interpreter

-
+
+

Interpreter

+

The (400 chars3) quantum interpreter is based on references arXiv:1711.02086 and arXiv:1608.03355. For simplicity, we always measure at the end of the execution:

-
Q ← {𝕊qb‿sc‿r:
+
Q ← {𝕊qb‿sc‿r:
   wf ← (1⌾⊑⋈⊢)⥊⟜0 2⋆qb
   M‿K ← ⟨+˝∘×⎉1‿∞ _cp, {1𝕊𝕩:𝕩; 𝕨𝕊1:𝕨; 𝕨∾∘×⟜<_cp𝕩}⟩
   E ← {0𝕊𝕩:1; K⍟(𝕨-1)˜𝕩}
@@ -246,9 +246,9 @@ 

Interpreter

-
-

Shor's algorithm

-
+
+

Shor's algorithm

+

As a test case, we employ the quantum circuit of Shor's algorithm for the number fifteen and base eleven, following references @@ -259,7 +259,7 @@

Shor's algorithm

-
n‿a‿qb‿r ← ⟨15, 11, 5, 0 U˜ 2⋆3⟩
+
n‿a‿qb‿r ← ⟨15, 11, 5, 0 U˜ 2⋆3⟩
 
 sc ← ⟨
   ⟨0⟩‿g.h ⋄ ⟨1⟩‿g.h ⋄ ⟨2⟩‿g.h
@@ -277,7 +277,7 @@ 

Shor's algorithm

-
C >+˝{Q qb‿sc‿𝕩}¨ r
+
C >+˝{Q qb‿sc‿𝕩}¨ r
 
@@ -291,9 +291,9 @@

Shor's algorithm

-
-

Epilogue

-
+
+

Epilogue

+

Why the hieroglyphs, you may ask? The tacit and functional style, coupled with numerous combinators, makes programming feel like solving a fun algebraic puzzle rather than drafting a manifesto. @@ -312,7 +312,7 @@

Epilogue

-
+
 ⟨ 44 64 ⟩
 
@@ -325,7 +325,7 @@

Epilogue

-
+
 ┌─                                                                                                                                                                                 
 ╵ '-' '´' '¨' '⋈' '+' '⟜' '⌽' '⊢' '≢' '⥊' '<' '=' '⌜' '˜' '↕' '∾' '○' '⌾' '⊸' '⊑' '÷' '√' '⊏' '⋆' '˝' '∘' '×' '⎉' '≡' '⊣' '⌊' '⁼' '≠' '⍟' '◶' '↓' '¬' '∊' '/' '»' '∨' '`' '>' '⍒'  
   8   8   10  5   8   3   6   7   1   5   9   6   3   12  6   5   2   5   7   9   5   1   1   5   4   8   5   1   3   3   1   1   5   1   2   1   1   1   1   1   1   2   3   1    
@@ -346,7 +346,7 @@ 

Epilogue

While the interpreter's performance is not particularly optimized, here is a comparison with the equivalent Common Lisp code:

-
+
 Benchmark 1: cbqn -f ./bqn/q.bqn
   Time (mean ± σ):      5.468 s ±  0.077 s    [User: 5.427 s, System: 0.005 s]
   Range (min … max):    5.358 s …  5.535 s    5 runs
@@ -369,7 +369,7 @@ 

Epilogue

-
+
 Got 25361 samples
 (REPL): 25361 samples:
      2│  Q ← {𝕊qb‿sc‿r:
diff --git a/rollim.html b/rollim.html
index e680eb3..655c9ab 100644
--- a/rollim.html
+++ b/rollim.html
@@ -241,13 +241,13 @@ 

A coding impromptu

Table of Contents

@@ -257,9 +257,9 @@

Table of Contents

would devise 35 to 40 distinct solutions to even the simplest problem in BQN. Therefore, I will frequently juxtapose my implementations with those of seasoned BQNators2.

-
-

Z algorithm

-
+
+

Z algorithm

+

This is a very efficient procedure that finds prefix strings in linear time. The imperative implementation reads: @@ -326,9 +326,9 @@

Z algorithm

-
-

Longest increasing sub-sequence

-
+
+

Longest increasing sub-sequence

+

This problem can be solved in \(O(n\log n)\) using dynamic programming. Here is an imperative implementation which is quadratic, but can be optimized: @@ -368,7 +368,7 @@

Longest increasing sub-sequence

Let's )explain this optimized version, so we can truly appreciate its beauty:

-
+
  +´∞≠∞¨{𝕨⌾((⊑𝕩⍋𝕨-1)⊸⊑)𝕩}´⌽ 
  │ │ │ ││    │ │ │  │ │  │ 
  │ │ │ {┼────┼─┼─┼──┼─┼─´│ 
@@ -398,9 +398,9 @@ 

Longest increasing sub-sequence

-
-

N-queens problem

-
+
+

N-queens problem

+

This problem is the archetypal example of backtracking. Initially, I tried to solve it using a function to place the queens in the full board, hoping that it would lead to a @@ -412,7 +412,7 @@

N-queens problem

-
+
 ┌─                 
 ╵ 0 1 0 1 0 1 0 0  
   0 0 1 1 1 0 0 0  
@@ -460,9 +460,9 @@ 

N-queens problem

-
-

Majority element

-
+
+

Majority element

+

The Boyer–Moore algorithm allows for finding the majority element (element that appears more than ⌊𝕩÷2 times in the array) in linear time. If such element exists, then it is @@ -497,9 +497,9 @@

Majority element

-
-

An identity on the naturals

-
+
+

An identity on the naturals

+

Some time ago, while working on performance optimization of linear algebra operations with Boolean arrays, I encountered an @@ -534,9 +534,9 @@

An identity on the naturals

-
-

Depth of nested lists

-
+
+

Depth of nested lists

+

Studying tree algorithms in APL, I learned about the depth vector representation. If the nested object in consideration is a string, the best approach is using boolean masks. @@ -554,9 +554,9 @@

Depth of nested lists

-
-

H-index

-
+
+

H-index

+

This metric is one of the reasons for the deplorable state of modern academia, and the headaches for outsiders trying to get in. Consider that Peter Higgs diff --git a/si.html b/si.html index 80b712a..8eb381c 100644 --- a/si.html +++ b/si.html @@ -229,9 +229,9 @@

Scheming a mise-en-abîme in BQN

-
-

Prelude

-
+
+

Prelude

+

We will build an interpreter for a subset of the Scheme programming language, following an essay by Peter Norvig. An alternative reference would @@ -242,9 +242,9 @@

Prelude

-
-

A R5RS dialect

-
+
+

A R5RS dialect

+

Our goal is to adhere to the Revised\(^5\) Report on the Algorithmic Language Scheme (R5RS). However, seasoned schemers will quickly notice that our implementation still has @@ -337,9 +337,9 @@

A R5RS dialect

-
-

A Lisp quine

-
+
+

A Lisp quine

+

Given the title of this post, it's only fitting that we test our interpreter with a quine. In fact, building this interpreter was, for me, an exercise in bootstrapping the necessary machinery to produce this recursive effect: diff --git a/spodat.html b/spodat.html index a2c5812..0e7d7a2 100644 --- a/spodat.html +++ b/spodat.html @@ -201,9 +201,9 @@

Songs to pave the seasons

I have analyzed my Spotify data1 for the period 2016-2024. The results accurately represent my actual music taste2.

-
-

Technical details

-
+
+

Technical details

+

This is a suitable task for an array language, so I rely on BQN which is my favorite one: @@ -235,15 +235,15 @@

Technical details

-
-

Top songs

-
+
+

Top songs

+
s Q spd	
 
-
+
 ┌─                                                     
 ╵ 1  "Countless Skies"                                 
   2  "Divertimento I, K.136: Allegro"                  
@@ -269,15 +269,15 @@ 

Top songs

-
-

Top artists

-
+
+

Top artists

+
a Q spd
 
-
+
 ┌─                              
 ╵ 1  "Opeth"                    
   2  "Wolfgang Amadeus Mozart"  
@@ -303,9 +303,9 @@ 

Top artists

-
-

Bonus: Opeth anthology

-
+
+

Bonus: Opeth anthology

+

This is the Opeth album I would recommend to anyone. The query function needs to be modified a bit for generating it. But before that, let's look at the official discography: @@ -334,7 +334,7 @@

Bonus: Opeth anthology

-
+
 ┌─                             
 ╵ 1 "Ghost of Perdition"       
   2 "River"