{"version":"https://jsonfeed.org/version/1.1","title":"Alice-N-Bob","home_page_url":"https://www.alice-bob.tech/","feed_url":"https://www.alice-bob.tech/feed.json","description":"An intermittent stream of blogs about technology, self-hosting & science.","language":"en-GB","icon":"https://www.alice-bob.tech/apple-touch-icon.png","favicon":"https://www.alice-bob.tech/favicon-32.png","authors":[{"name":"Mayank Raj","url":"https://www.alice-bob.tech/about/"}],"items":[{"id":"https://www.alice-bob.tech/the-root-n-trick/","url":"https://www.alice-bob.tech/the-root-n-trick/","title":"The lazy algorithm vs BIT","summary":"Back in my days of competitive coding - I realized that there are two kinds of competitive coders - one who know binary indexed trees (BIT) vs the ones who do not.","content_html":"<figure><img src=\"https://www.alice-bob.tech/_astro/cover.DZy3kkqU_ZU3J8o.jpeg\" alt=\"A Fenwick tree over sixteen cells: each node spans the cells it sums, with nodes 13, 12 and 8 highlighted as the path for prefix(13)\" width=\"1200\" height=\"800\" /></figure>\n<p>Back in my days of competitive coding - I realized that there are two kinds of competitive coders - one who know binary indexed trees (BIT) vs the ones who do not. I have to admit that BITs came very late into my arsenal - and even after that I did not have a sharp enough memory to reproduce BIT’s code in a closed book programming contest.</p>\n<p>Inadvertently, I had been using an alternative algorithmic approach which can only be named <code>Laziness</code>. Today, I would like to share it as well as subject it to some stress. Also, it’s a good moment to refresh a key tenet of coding contests.</p>\n<blockquote>\n<p>Find the simplest (&amp; worst-performing) algorithm which solves the problem in the permitted time limit.</p>\n</blockquote>\n<h2>Binary Indexed Tree (BIT)</h2>\n<p>BIT is an insanely simple to code &amp; equivalently complex to understand data structure. It generally comes at midpoint in a competitive coder’s life &amp; never comes back when a coder becomes a real life software engineer. A typical issue with BITs is that books &amp; tutorials explain it as <code>It is what it is &amp; it works. Followed by mathematical proofs</code>. I am yet to find a tutorial which explains BITs with a <code>learning by discovery</code> approach.</p>\n<p>This article is not about details of BIT. We will start with the premise that the following problem is a BIT problem: <a href=\"https://leetcode.com/problems/range-sum-query-mutable/\">range sum query - leetcode</a>.</p>\n<figure>\n<pre><code>Given an integer array nums, handle multiple queries of the following types:\n\n-&gt; Update the value of an element in nums.\n-&gt; Calculate the sum of the elements of nums between indices left and right inclusive.\n\n&gt; Total Queries = Size of Arr ~ 3 x 10^4\n&gt; Permissible Time Limit: O(N * √N)</code></pre>\n<figcaption>The most basic BIT coding problem</figcaption>\n</figure>\n<p>Known acceptable solutions to the above problem:</p>\n<ul>\n<li>BITs: O(log N) updates &amp; query.</li>\n<li>Segment Tree: O(log N) updates &amp; query - but more complex to code.</li>\n<li><a href=\"https://cp-algorithms.com/data_structures/sqrt_decomposition.html\">Sqrt Decomposition</a>: O(√N) updates &amp; query. This involves breaking down the array into segments of √N. Every segment always keeps an up-to-date sum of all its elements.</li>\n</ul>\n<p>The sqrt solution qualifies because it’s really hard to create contest problems which can accept N log(N) but not N √N solutions.</p>\n<h2>The Lazy Algorithm</h2>\n<p>We considered the three ways to solve the range sum query problem - which run within the given time limit. Let’s circle back to the trivial case when there are no updates. In this case, the optimal way is to compute prefix sums. ie. compute another array pre containing cumulative sums of elements of original array, as shown below:</p>\n<figure>\n<pre><code># THE PREFIX SUM METHOD\ngiven arr = [a0, a1, ...]\n\n# Compute an array of prefix sums.\n# ith element stores the sum of first i + 1 elements\npre[i] = sum(arr[0:i]) = pre[i-1] + arr[i]\n\n# Answer range query:\nrangeSum(L, R) = pre[R] - pre[L-1]\n\n## Deal with corner case of index = 0 separately</code></pre>\n<figcaption>Prefix sum/cumulative sum method for answering range sum queries</figcaption>\n</figure>\n<p><strong>The prefix sum method + range sum problem have the following properties:</strong></p>\n<ul>\n<li>Computing the pre[] array takes O(N) time.</li>\n<li>After pre computation, every rangeSum query takes O(1) time</li>\n<li>The problem can have O(N) updates. This would make our algorithm O(N*N).</li>\n</ul>\n<blockquote>\n<p>So, what do we do? I will drop a hint: <code>Amortization</code>. A competitive coder should pause here &amp; make an attempt to figure out the lazy-algorithm.</p>\n</blockquote>\n<h3>Welcome laziness</h3>\n<p>Let us say we are lazy in propagating the updates to the <code>pre[]</code> array of prefix sums. Instead of propagating the updates; we just keep them in a pending list <em>for a while</em>.</p>\n<blockquote>\n<p>Can u figure out a way to answer the rangeSum(1, 2) by using the stale-prefix array + looking into the pending list?</p>\n</blockquote>\n<figure>\n<pre><code>## The array &amp; computed prefix sums array\narr = [1, 2, 3, 4,  5]\npre = [1, 3, 6, 10, 15]\n\n## New updates requested - Lazy algorithm keeps them in pending list:\nupdate(index = 0, value = 3)\nupdate(index = 1, value = 9)\nupdate(index = 3, value = 10)\n\narr would have become: [3, 9, 3, 10, 5]\n\n## Range sum query comes up:\nrangeSum(1, 2)</code></pre>\n<figcaption>Calculate cost of laziness</figcaption>\n</figure>\n<p>We shall use the precomputed array to get a stale answer &amp; go through all elements of the pending list &amp; apply the ones which affect the queried range:</p>\n<ul>\n<li>Stale rangeSum(1, 2) = <code>pre[2] - pre[0] = 6 - 1 = 5</code></li>\n<li>The list has an update for index = 1 which applies to the range(1, 2). The value of arr[1] used in stale sum = 2. Updated value = 9.</li>\n<li>Up to date rangeSum(1, 2) = <code>5 - 2 + 9 = 12</code>.</li>\n</ul>\n<p>Now, the only thing left to figure out is exactly <em>how lazy can we be</em>? Let the max size of pending list be <code>L</code>.</p>\n<ul>\n<li>Cost of being lazy = O(L) per range query.</li>\n<li>Cost of not-being-lazy = O(N) computation time. Happens once per L updates.</li>\n</ul>\n<p>One can work out the math to see that the optimal size of <code>L = √N</code>. which will get the amortized cost of solving the range sum queries problem in O(N * √N).</p>\n<h2>Extending Laziness</h2>\n<p>What have we really achieved? A <code>simple to code &amp; understand lazy solution</code> which can solve a few problems otherwise solvable by BITs, Segment trees or SQRT decomposition?</p>\n<p>In order to truly appreciate laziness; we need to go further than the list of problems which have been already posed in programming contests. Here is one such problem which I have never seen in contests &amp; it kicks asses of BITs &amp; Segment Trees.</p>\n<figure>\n<pre><code>N queries need to be answered in order. The queries are of the following types:\n\n-&gt; newScore(score): New score is published.\n-&gt; rmScore(score): An old score is removed.\n-&gt; getRank(score): Get rank of a given score.\n\n\n&gt; N ~ 3 x 10^4.\n&gt; score: Integer. There shall not be duplicate published scores.\n&gt; Permissible Time Limit: O(N * √(N * log N))</code></pre>\n<figcaption>The new problem</figcaption>\n</figure>\n<blockquote>\n<p>Can u augment the laziness approach to solve it? Can u solve it a new creative way which beats the lazy approach? Please let me know.</p>\n</blockquote>\n","date_published":"2022-09-07T20:29:16.000Z","date_modified":"2022-09-08T09:16:18.000Z","tags":["binary-indexed-tree","fenwick tree","algorithm","coding","contest"],"image":"https://www.alice-bob.tech/_astro/cover.DZy3kkqU_ZU3J8o.jpeg","authors":[{"name":"Mayank Raj","url":"https://www.alice-bob.tech/about/"}]},{"id":"https://www.alice-bob.tech/driving-a-boat-abstractions/","url":"https://www.alice-bob.tech/driving-a-boat-abstractions/","title":"Driving a boat & abstractions","summary":"Amsterdam is a beautiful city permeated with rich & artistic canal system knitted along streets & bridges. A trip to Amsterdam is not complete without cruising its canals in a boat.","content_html":"<figure><img src=\"https://www.alice-bob.tech/_astro/cover.D00WD6xS_ZOLU7.jpeg\" alt=\"Small open motorboats full of people cruising a tree-lined Amsterdam canal past houseboats and an arched bridge\" width=\"1200\" height=\"680\" /></figure>\n<p>Amsterdam is a beautiful city permeated with rich &amp; artistic canal system knitted along streets &amp; bridges. A trip to Amsterdam is not complete without cruising its canals in a boat. If you are in Amsterdam for a few days, you can rent out a self-drive boat from one of the many providers like:</p>\n<ul>\n<li><a href=\"https://mokumbootverhuur.nl/en/home/\">mokumboot</a>, <a href=\"https://www.canalmotorboats.com/\">canalmotorboats</a>: Electric boats, speed limited &amp; convenient for first timers.</li>\n<li><a href=\"https://www.clickandboat.com/en/\">clickandboat</a>: This is like airbnb for boats. If you are lucky you can get a powerful motorboat which runs on gas.</li>\n</ul>\n<p>Please <a href=\"https://www.iamsterdam.com/en/plan-your-trip/getting-around/boating\">read the rules</a> &amp; adhere to the instructions &amp; map provided when you get the boat.</p>\n<h3>Boat Mechanics</h3>\n<p>Boat is an extremely simple device when compared with a car. It also requires no understanding of physics to drive a boat - trial and error for 15 mins will get you tame a canal motor boat easily. However, getting to know the mechanics of it can make you appreciate it better - like <a href=\"https://fs.blog/richard-feynman-on-beauty/#:~:text=I%20can%20appreciate%20the%20beauty,which%20also%20have%20a%20beauty.\">Richard Feynman appreciates a flower</a>.</p>\n<p>Motorboats come with just 2 controls:</p>\n<ul>\n<li><strong>Throttle:</strong> Controls power of the propeller to push water - Forward &amp; Backward.</li>\n<li><strong>Direction Control:</strong> Basically this controls the direction in which the boat’s propeller pushes water.</li>\n</ul>\n<p>Further, there are 2 variants in which these controls are presented to the captain - with a steering wheel or with a tiller (a stick). The effect is the same, a captain provides a value for the <strong>direction &amp; power of a propeller for every time instant</strong> - and the boat responds with motion.</p>\n<h3>Abstractions &amp; models</h3>\n<p>If all of software engineering was to be condensed into one word, it would be <code>abstraction</code>. Abstractions have a wider applicability though. An interesting power of an abstraction is that it does not need to be correct in order to be useful. Abstractions when applied to the process of thinking produce <a href=\"https://jamesclear.com/mental-models\">mental models</a>. We shall be modeling the mechanics of a boat into a crisp abstraction, which shall help us drive it easily.</p>\n<p>In order to do this we need to understand the difference b/w driving a car vs boat. Typically you will note the following:</p>\n<ul>\n<li>Do the same moves as a car, but expect a delay in turns. As speed increases, the delay goes down.</li>\n<li>There is no brake. Brake = <code>Throttle forward - backward - forward - backward ...</code> with decreasing amplitude.</li>\n</ul>\n<p>The above abstraction actually works quite well, but can we do better? Let’s see the mechanics from the lens of basic physics &amp; find out…</p>\n<h3>Physics of car vs boat</h3>\n<p>The basic difference b/w the two in terms of physics can be summarized as:</p>\n<ol>\n<li>The center-of-mass of a boat <em>must move left a little in order to do a right turn</em>. This is not true for a car.</li>\n<li>The rectangle of a boat as seen from the sky can move (translational motion) in an arbitrary direction, which is not the case for a car.</li>\n</ol>\n<p>Where are these inferences coming from?</p>\n<blockquote>\n<p>A boat can be treated as a rectangle on a frictionless surface with a propeller attached at the center of rear end, acting like a rocket’s thruster applying a force exactly at the rear center.</p>\n</blockquote>\n<figure>\n<p><img alt=\"Hand-drawn top view of a boat between two canal walls on frictionless water, with a thrust arrow F at the rear that swings between positions A and B\" width=\"1400\" height=\"1176\" src=\"https://www.alice-bob.tech/_astro/free-body-diagram.6QGEPhNJ_Z1ouBKR.webp\" /></p>\n<figcaption>Free body diagram of a boat from sky</figcaption>\n</figure>\n<p>Now, let’s see how this free body takes a right turn, assuming we start from a boat at rest. In order to do so, the propeller has to push the boat <code>LEFT</code> i.e the red force arrow would move to B. In this situation, the effect is that the boat will rotate clockwise &amp; the center of mass would move towards north-west.</p>\n<p>A new captain would keep doing this <code>move-B</code> for much longer expecting a right turn and <em>the sideways velocity gets very uncomfortable soon</em> - specially in narrow canals. The solution is to do <code>move-B</code> for a while - quickly followed by a half <code>move-A</code> followed by small corrections to reach the center as shown in the graph below:</p>\n<figure>\n<p><img alt=\"Graph of steering input over time: a dip below zero, then a smaller swing above, then damped oscillations settling at zero\" width=\"1796\" height=\"778\" src=\"https://www.alice-bob.tech/_astro/right-turn.CgkBjKNY_1hvpbR.webp\" /></p>\n<figcaption>Taking a right turn: Y axis represents the amount of motion of the steering</figcaption>\n</figure>\n<h3>The Abstract Model</h3>\n<blockquote>\n<p>The free body diagram of a <code>rectangle with arrow at the rear center</code> is the perfect abstraction for captaining a boat. A captain has to imagine being given control of the <code>power &amp; direction of the F arrow</code>. And then one has to simulate the motion using the a mental physics engine*.</p>\n</blockquote>\n<p>Typically tiller based boats are harder to navigate than steering based ones. But, this abstract model fits perfectly in that case. The direction of tiller matches the direction of thrust - and using the model above helps.</p>\n<p>*We all have a mental physics engine - LeCun, the chief scientist at meta <a href=\"https://www.technologyreview.com/2022/06/24/1054817/yann-lecun-bold-new-vision-future-ai-deep-learning-meta/\">calls it common sense</a> &amp; has been using it to improve AI systems! Thought experiments like this help improve the powers of the mental physics engine!</p>\n","date_published":"2022-08-19T19:46:39.000Z","date_modified":"2022-08-19T19:47:46.000Z","tags":[],"image":"https://www.alice-bob.tech/_astro/cover.D00WD6xS_ZOLU7.jpeg","authors":[{"name":"Mayank Raj","url":"https://www.alice-bob.tech/about/"}]},{"id":"https://www.alice-bob.tech/quantum_dice/","url":"https://www.alice-bob.tech/quantum_dice/","title":"🎲 Quantum Dice","summary":"A curious exploration with mathematics, physics & philosophy into the realms of the fundamental quantum mechanical nature of universe.","content_html":"<figure><img src=\"https://www.alice-bob.tech/_astro/cover.T5S0EfFj_PJwKm.jpeg\" alt=\"A yellow and a red die resting on a Catan board among hex tiles, number tokens and wooden pieces\" width=\"1200\" height=\"600\" /></figure>\n<p>The world is made up of some <em>primitives</em> like atoms, muons, quarks, etc - and the <a href=\"https://www.ams.org/notices/200902/rtx090200212p.pdf\">visionary birds &amp; hardworking frogs</a> of the world have been working round the decades hunting them down! Historically the hunt works something like this:</p>\n<ol>\n<li>The <a href=\"https://en.wikipedia.org/wiki/Cloud_chamber\">cloud chamber</a> was invented. It enabled us to detect elementary particles.</li>\n<li><strong>~1930(?):</strong> <a href=\"https://en.wikipedia.org/wiki/Dmitri_Skobeltsyn\">Dmitri Skobeltsyn</a> created an advanced cloud chamber by adding magnetic field. He saw <code>anti-matter</code> for the first time - but thought it was a fluke in apparatus.</li>\n<li><strong>1931:</strong> Dirac cooked up some Maths to explain a bunch of particle interactions &amp; <a href=\"https://en.wikipedia.org/wiki/Dirac_equation\">his equation</a> had an unexplained negative value solution - so he said that anti-matter should exist.</li>\n<li><strong>1932:</strong> A year later, Carl Anderson did the Skobeltsyn’s experiment and conclusively found the positron:e+; the anti-matter twin of electron:e-. Carl was later awarded <a href=\"https://www.nobelprize.org/prizes/physics/1936/anderson/facts/\">the Nobel prize</a>.</li>\n</ol>\n<h2>I - The Standard Model</h2>\n<figure>\n<p><img alt=\"The Standard Model: twelve fermions in three generations of quarks and leptons, four gauge bosons and the Higgs boson\" width=\"736\" height=\"618\" src=\"https://www.alice-bob.tech/_astro/standard-model.cHaWgQes_Z2wd9IU.svg\" /></p>\n<figcaption>The standard model</figcaption>\n</figure>\n<p>A whole lot has happened since then. With advances in measurement &amp; theory; we have <a href=\"https://en.wikipedia.org/wiki/Standard_Model\">the standard model of particle physics</a>. This is something like the level-0 periodic table. By the same analogy the level-2 periodic table would be a map of all types of cells &amp; would gradually shift branch to biology.</p>\n<h2>II - Dice: A quantum particle</h2>\n<p>Today we are concerned with the 2 major types of elementary particles - <code>fermions</code> &amp; <code>bosons</code>. We shall treat a dice as a quantum particle which can be in one of 6 quantum states with equal probability. We shall take 2 <em>dice</em> which can be in states: <code>A1, A2, ... A6</code> &amp; <code>B1, B2, ... B6</code>.</p>\n<p>A key difference b/w fermions &amp; bosons is that when you smoosh 2 of these in a system - for example (A2, B3), the following is literally true about <em>God &amp; dice</em>*:</p>\n<ul>\n<li><strong>Fermion-ic Dice:</strong> God knows which one of the dice says 2 &amp; which one says 3. Even if they switch states, God knows about that too.</li>\n<li><strong>Bosoni-ic Dice:</strong> Even God does not know which one is in state-2 &amp; which one is in state-3. Further, there are no two ways this can happen. The two dice have fused into one quantum system &amp; they are <strong>indistinguishable</strong>.</li>\n</ul>\n<p>This property of God has profound implications on the mathematics of bosons. This was first figured out by <a href=\"https://en.wikipedia.org/wiki/Satyendra_Nath_Bose\">Satyendra Nath Bose</a> &amp; the current scheme of things works like this:</p>\n<ul>\n<li>Fermions obey Fermi-Dirac statistics.</li>\n<li>Bosons obey Bose-Einstein statistics.</li>\n</ul>\n<h2>III - The Mathematics</h2>\n<p>Let’s talk about 2 dice games like <a href=\"https://en.wikipedia.org/wiki/Catan\">catan</a> &amp; <a href=\"https://en.wikipedia.org/wiki/Craps\">craps</a>. Every turn a player rolls a pair of dice &amp; sums the numbers from the two dice. The sum is used to further determine actions of the game.</p>\n<figure>\n<table>\n<thead>\n<tr>\n<th>A \\ B</th>\n<th>1</th>\n<th>2</th>\n<th>3</th>\n<th>4</th>\n<th>5</th>\n<th>6</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><strong>1</strong></td>\n<td>2</td>\n<td>3</td>\n<td>4</td>\n<td>5</td>\n<td>6</td>\n<td>7</td>\n</tr>\n<tr>\n<td><strong>2</strong></td>\n<td>3</td>\n<td>4</td>\n<td>5</td>\n<td>6</td>\n<td>7</td>\n<td>8</td>\n</tr>\n<tr>\n<td><strong>3</strong></td>\n<td>4</td>\n<td>5</td>\n<td>6</td>\n<td>7</td>\n<td>8</td>\n<td>9</td>\n</tr>\n<tr>\n<td><strong>4</strong></td>\n<td>5</td>\n<td>6</td>\n<td>7</td>\n<td>8</td>\n<td>9</td>\n<td>10</td>\n</tr>\n<tr>\n<td><strong>5</strong></td>\n<td>6</td>\n<td>7</td>\n<td>8</td>\n<td>9</td>\n<td>10</td>\n<td>11</td>\n</tr>\n<tr>\n<td><strong>6</strong></td>\n<td>7</td>\n<td>8</td>\n<td>9</td>\n<td>10</td>\n<td>11</td>\n<td>12</td>\n</tr>\n</tbody>\n</table>\n<figcaption>Outcomes chart for rolling 2 fermion-ic dice</figcaption>\n</figure>\n<p>There are <code>6 ways to roll a 7</code> | <code>5 ways to roll a 6</code> &amp; so on.</p>\n<figure>\n<table>\n<thead>\n<tr>\n<th>Sum</th>\n<th>Ways</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>2</td>\n<td>1</td>\n</tr>\n<tr>\n<td>3</td>\n<td>2</td>\n</tr>\n<tr>\n<td>4</td>\n<td>3</td>\n</tr>\n<tr>\n<td>5</td>\n<td>4</td>\n</tr>\n<tr>\n<td>6</td>\n<td>5</td>\n</tr>\n<tr>\n<td>7</td>\n<td>6</td>\n</tr>\n<tr>\n<td>8</td>\n<td>5</td>\n</tr>\n<tr>\n<td>9</td>\n<td>4</td>\n</tr>\n<tr>\n<td>10</td>\n<td>3</td>\n</tr>\n<tr>\n<td>11</td>\n<td>2</td>\n</tr>\n<tr>\n<td>12</td>\n<td>1</td>\n</tr>\n</tbody>\n</table>\n<figcaption>2x Fermion Dice</figcaption>\n</figure>\n<p>However, this is assuming that our two quantum dice Alice &amp; Bob are fermions. What happens if they are bosons? The maths follows along with the discovery of Bose about <strong>indistinguishability</strong>. There are no longer 6 ways to roll a sum of 7; there are only three. i.e (A6, B1) = (A1, B6) if the dice are bosons.</p>\n<figure>\n<table>\n<thead>\n<tr>\n<th>Sum</th>\n<th>Ways</th>\n<th>Pairs</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>2</td>\n<td>1</td>\n<td>(1,1)</td>\n</tr>\n<tr>\n<td>3</td>\n<td>1</td>\n<td>(1,2)</td>\n</tr>\n<tr>\n<td>4</td>\n<td>2</td>\n<td>(1,3) (2,2)</td>\n</tr>\n<tr>\n<td>5</td>\n<td>2</td>\n<td>(1,4) (2,3)</td>\n</tr>\n<tr>\n<td>6</td>\n<td>3</td>\n<td>(1,5) (2,4) (3,3)</td>\n</tr>\n<tr>\n<td>7</td>\n<td>3</td>\n<td>(1,6) (2,5) (3,4)</td>\n</tr>\n<tr>\n<td>8</td>\n<td>3</td>\n<td>(2,6) (3,5) (4,4)</td>\n</tr>\n<tr>\n<td>9</td>\n<td>2</td>\n<td>(3,6) (4,5)</td>\n</tr>\n<tr>\n<td>10</td>\n<td>2</td>\n<td>(4,6) (5,5)</td>\n</tr>\n<tr>\n<td>11</td>\n<td>1</td>\n<td>(5,6)</td>\n</tr>\n<tr>\n<td>12</td>\n<td>1</td>\n<td>(6,6)</td>\n</tr>\n</tbody>\n</table>\n<figcaption>2x Boson Dice</figcaption>\n</figure>\n<blockquote>\n<p><strong>The Problem:</strong> Is it possible to have 2 regular dice such that the sum of the numbers from their rolling faces follows the same distribution as our Boson-ic pair of 6 state dice?</p>\n</blockquote>\n<h2>IV - A small trip to Geometry</h2>\n<p>A dice is a regular polyhedron. These are convex figures made out of regular polygons in 3D cartesian geometry. It just so happens that there are only a total of 5 types of regular polyhedron in cartesian geometry! And it had been known since 350 BC. It’s a good exercise to prove it yourself just to make sure.</p>\n<p>To refine our problem, we are interested in taking 2 or more of these platonic solids &amp; write numbers on their faces such that the sum of the numbers while rolling them:</p>\n<ul>\n<li>Can only be between 2 … 12.</li>\n<li>The probability distribution is same as if we rolled 2 pure 6 faced bosonic dice.</li>\n</ul>\n<h2>V - The Solution</h2>\n<p>Following the carpenter’s syndrome of being a software engineer, I asked a computer to find out a solution for me. For this I used a python notebook on the databricks <a href=\"https://databricks.com/product/faq/community-edition\">community edition</a>; which provides decent resources to run arbitrary compute jobs for free. The program iterates over various combinations of dice &amp; numbers to be written on faces &amp; tries to find a pair or triplet of dice which can give close answers.</p>\n<blockquote>\n<p>Unfortunately, there was no perfect solution with the platonic solids which days of semi-brute-force could find! However, a solution involving a 7 faced dice &amp; 6 faced dice was found! Here it is:</p>\n</blockquote>\n<ul>\n<li>Dice A: <code>[1, 1, 3, 3, 5, 5]</code> - 6 faced</li>\n<li>Dice B: <code>[1, 2, 3, 4, 5, 6, 7]</code> - 7 faced</li>\n</ul>\n<h3>All Perfect Solutions</h3>\n<p>The computational search was not going anywhere &amp; it did not give any guarantees that there might exist other solutions which were not searched. It was then, a friend of mine <a href=\"https://www.linkedin.com/in/shashank-kedia-207a82a0/\">Shashank Kedia</a>, showed me this video by Zach Star on a similar problem of alternative dice creation.</p>\n<p><a href=\"https://www.youtube.com/watch?v=XjQoNbJI7Sw\">Watch Zach Star’s video on YouTube</a></p>\n<p>I am going to leave out the details here, but the task reduces to factorising the following polynomial:</p>\n<p><var>x</var><sup>12</sup> + <var>x</var><sup>11</sup> + 2<var>x</var><sup>10</sup> + 2<var>x</var><sup>9</sup> + 3<var>x</var><sup>8</sup> + 3<var>x</var><sup>7</sup> + 3<var>x</var><sup>6</sup> + 2<var>x</var><sup>5</sup> + 2<var>x</var><sup>4</sup> + <var>x</var><sup>3</sup> + <var>x</var><sup>2</sup></p>\n<p>How do we do that? We simply query the sibling of google which took on a different approach towards cataloging &amp; serving the knowledge of the world - <a href=\"https://www.wolframalpha.com/input?i=x%5E12+%2B+1+x%5E11+%2B+2+x%5E10+%2B+2+x%5E9+%2B+3+x%5E8+%2B+3+x%5E7+%2B+3+x%5E6+%2B+2+x%5E5+%2B+2+x%5E4+%2B+1+x%5E3+%2B+x%5E2\">wolframalpha</a>. It’s quite amazing that we can get step-by-step solutions as well to such problems - with hints included in their pro plan.</p>\n<figure>\n<p><var>x</var><sup>2</sup> (<var>x</var><sup>2</sup> + <var>x</var> + 1)(<var>x</var><sup>2</sup> − <var>x</var> + 1)(<var>x</var><sup>6</sup> + <var>x</var><sup>5</sup> + <var>x</var><sup>4</sup> + <var>x</var><sup>3</sup> + <var>x</var><sup>2</sup> + <var>x</var> + 1)</p>\n<figcaption>The factorisation</figcaption>\n</figure>\n<p>Working out the math, it boils down to having a 7 faced dice &amp; a 3 faced dice. A 3 faced dice can be simulated by a 6 faced dice.</p>\n<h3>A close imperfect solution</h3>\n<p>Some good approximate solutions exist if we restrict ourselves to platonic dice. Can u find a good one with 2 x 8 faced dice?</p>\n<h3>Making the 7 faced dice</h3>\n<p>A colleague of mine with a 3D printer made a basic version like a cone with flat 7 edges. It works but needs more work to replace the dice while playing catan. Also, I am pleasantly surprised that one can <a href=\"https://www.mathartfun.com/d357.html\">buy different kinds of platonic &amp; non platonic dice</a>.</p>\n<figure>\n<p><img alt=\"A red seven-sided die beside a white and a black die, all with pentagonal ends\" width=\"288\" height=\"175\" src=\"https://www.alice-bob.tech/_astro/d7-trio.CJFHPSDQ_2gs99j.webp\" /></p>\n<p><img alt=\"Six seven-sided dice with rounded faces in red, green, blue, black, white and purple\" width=\"324\" height=\"242\" src=\"https://www.alice-bob.tech/_astro/d7-six.CNYnBYl1_6wnOF.webp\" /></p>\n<figcaption>Examples of 7 faced dice on market - the right one is imperfect.</figcaption>\n</figure>\n<h2>A whatif puzzle: negative mass</h2>\n<p>We saw how negative solutions found by Dirac’s equations helped us discover antimatter. Let’s see what happens if we play around similarly with laws of gravitation.</p>\n<p>So far, we have not been able to experimentally observe the <code>gravitational interaction b/w matter &amp; antimatter</code>. It is accepted that antimatter has a positive mass just like matter. However, let’s say hypothetically we had a particle with negative mass interacting only gravitationally with another positive mass - what would happen?</p>\n<figure>\n<p><img alt=\"A positive mass +m on the left and a negative mass −m on the right, a distance r apart\" width=\"500\" height=\"230\" src=\"https://www.alice-bob.tech/_astro/gravity-pair.Bs6z4lRt_Z1OKzB2.svg\" /></p>\n<figcaption>What would happen if a mass of +m &amp; -m interact gravitationally?</figcaption>\n</figure>\n<h4>Options:</h4>\n<ol>\n<li>Attract each other</li>\n<li>Repel each other.</li>\n<li>Play tom-n-jerry towards right.</li>\n<li>Play tom-n-jerry towards left.</li>\n</ol>\n<h2>Epilogue</h2>\n<p>I hope you learnt something about how the world works at the most fundamental level, and you are curious to find out how the dynamics of a game of catan changes by replacing the regular dice with quantum dice! We shall uncover the rejuvenation effects of different dice on the gameplay of catan as well as seven faced dice 3D models another time.</p>\n<p>The physics &amp; maths of the quantum dice situation is not strictly 1:1. However, facts of physics presented are reasonably correct.</p>\n<blockquote>\n<p><strong>B.S:</strong> There are very few conclusively known properties of God - The inability to determine which dice in state-X &amp; state-Y when two boson-ic dice consummate &amp; their wave functions are dancing in harmony; is one of such properties unlike <code>omniscience</code> &amp; <code>omni-*</code>.<br />\n<br />\nmore B.S: DISCLAIMER - For the skeptics on above argument - please do not conclude that God is incapable to produce a regular polyhedron apart from the 5 platonic solids - it’s a mere consequence of cartesian geometry.</p>\n</blockquote>\n<hr />\n<h4>References</h4>\n<ul>\n<li><a href=\"https://www.thefactsite.com/catan-facts/\">https://www.thefactsite.com/catan-facts/</a></li>\n</ul>\n","date_published":"2022-07-07T22:32:35.000Z","date_modified":"2022-07-07T23:38:59.000Z","tags":["quantum","quantum-mechanics","math","programming"],"image":"https://www.alice-bob.tech/_astro/cover.T5S0EfFj_PJwKm.jpeg","authors":[{"name":"Mayank Raj","url":"https://www.alice-bob.tech/about/"}]},{"id":"https://www.alice-bob.tech/5-levels-of-ad-blocking/","url":"https://www.alice-bob.tech/5-levels-of-ad-blocking/","title":"5 levels of ad-blocking","summary":"Ads are a necessary evil in some narratives. The narratives where the most talented techies & decision makers of the tech world created the ad delivery system.","content_html":"<figure><img src=\"https://www.alice-bob.tech/_astro/cover.CxfWuMKG_1FWEco.jpeg\" alt=\"Flat illustration of a team at work around a giant megaphone, a target, a calculator and a calendar\" width=\"1200\" height=\"800\" /></figure>\n<p>Ads are a necessary evil in some narratives. The narratives where the most talented techies &amp; decision makers of the tech world created the ad delivery system. This system has reached the scale of such proportions that it now interacts with humanity as a whole in notorious ways. Look at <a href=\"https://strategicfactory.com/2016/11/the-psychology-of-color-how-coca-cola-captured-hearts-around-the-world/\">this report</a> which in the world of an unbiased 3rd party observer would read.</p>\n<p>I am not going to start throwing information on how consolidation of massive amounts of personal data is a by product of massive ad delivery factories - and there are efforts to keep that in check going around the world notably by Apple &amp; European Union.</p>\n<p>All that geo-politics is really beyond me. I acknowledge the presence of the ad-monster &amp; I really have very simple issues with it:</p>\n<ul>\n<li><strong>ad-to-content ratio</strong> must be &lt; 10% on static pages &amp; 2% of time on dynamic sites. Unfortunately on instagram your mind is given a stimulus decided by the highest bidder every 3rd story.</li>\n<li>An analytics dashboard for the ad-viewers as good as the one for ad-creators - and ability to tweak ad-preferences with 100s of parameters to play with.</li>\n</ul>\n<hr />\n<p>Keeping the battle b/w the systems aside; let’s talk guerrilla warfare. Ads are evil &amp; let us stop them grabbing our un-necessary attention because someone threw money to Mr. Zuck for adulterating 1B social feed’s top posts - which happens everyday.</p>\n<h3>Level 1: The browser ad-blocker</h3>\n<p>There is nothing left to say about these. Already <a href=\"https://blog.hubspot.com/marketing/ad-blocking-stats#:~:text=Over%2038%25%20of%20people%20block,companies%20from%20collecting%20personal%20data.\">~40% of the desktop users</a> are aware of browser extensions or browsers themselves like <a href=\"https://www.mozilla.org/en-US/firefox/new/\">firefox</a>, <a href=\"https://brave.com/\">brave</a>, <a href=\"https://www.opera.com/\">opera</a>, <a href=\"https://vivaldi.com/\">vivaldi</a> who based on their status in the food chain believe that ad-blocking comes in the <code>required</code> feature list of browsers. However, this does not cover mobile - where most of the mortal world plugs into the internet territory. These browsers are also offered on android &amp; ios but they are powerless in other apps or webviews.</p>\n<h3>Level 2: The <code>private DNS</code> trick</h3>\n<blockquote>\n<p>This is really an under appreciated feature in android! Just search for DNS in settings &amp; enter: <code>dns.adguard.com</code> &amp; browse to <a href=\"https://adblock-tester.com/\">an ablock testing site</a> to see your score immediately jump up!</p>\n</blockquote>\n<p>There are other public DNS providers with varying degrees/domains of blocking which one can choose from - here is <a href=\"https://kb.adguard.com/en/general/dns-providers#default\">one such list</a>. For android, this works with mobile data - however, while another VPN is on - it can be flaky.</p>\n<p>For iOS, the alternative option is to use an alternative DNS server’s IP address in the wifi settings &gt; manual DNS. Unfortunately will not work over mobile data.</p>\n<h3>Level 3: Buy adguard home</h3>\n<p>Technically this is level 4 if you don’t mind spending <a href=\"https://stacksocial.com/sales/adguard-personal-plan-lifetime-subscription\">20$ on the premium version</a> of the open source software mentioned in true level-4. The best option is to use their https-filtering approach by installing a CA certificate. In security perspective this is https-peeking is a standard in enterprise security these days by corporate VPNs. Additionally for adguard, the encryption keys of the certificates never leave your device.</p>\n<p>So, it might feel like red-flags; but realistically speaking its designed to be safe unless in implementation there is a grand conspiracy b/w NSA &amp; adguard.</p>\n<h3>Level 4: Host your own DNS</h3>\n<p>Okay, now things start getting uphill in complexity; however, the rewards are worth it. The basics:</p>\n<blockquote>\n<p>Most ad-blockers (browser based or not) work majorly as a DNS filter. They change the answer about browser address (url) -&gt; registered address of residence in internet’s real-estate-registry. Instead of DNS filter, at this level, we shall setup our own DNS server!</p>\n</blockquote>\n<h5>Pre-requisite: This setup requires a linux server - physical or virtual.</h5>\n<ul>\n<li><strong>👉🏼 Buy a router’s sidekick:</strong> The recommended hardware is a raspberry pi - literally even the smallest pi zero works - which costs 5$ &amp; &lt;5W of electricity to be a little sidekick for your router. Install raspbian OR ubuntu by flashing the appropriate image.</li>\n<li><strong>👉🏼 Rent virtual-machine:</strong> If you want to go zero budget, get a linux VM (a really powerful one) <a href=\"https://www.oracle.com/cloud/free/\">for free in oracle cloud</a>. This article will not go over the linux, network security list etc setup; the goal is to have a reasonably secure linux setup with port 53 open.</li>\n</ul>\n<h5>Steps: The docker way (recommended)</h5>\n<ul>\n<li>Follow the <a href=\"https://gist.github.com/dsculptor/2e17822c22a313ffa03e3f963a4bcb65\">instructions in this gist</a> to setup adguard on your linux machine.</li>\n</ul>\n<h5>Steps: Without docker | <a href=\"https://github.com/AdguardTeam/AdGuardHome#getting-started\">source</a></h5>\n<pre><code># Run the following one-line automatic adguardhome installer\ncurl -s -S -L https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh -s -- -v\n\n\n# Disclaimer: Do not expose adguard's admin ports on a machine on public cloud.</code></pre>\n<h3>Level 5: Create a secure home lab</h3>\n<p>If you have achieved level 3 or 4; I would say your defences against the ad-monster are top-of-the-line as of today. However, let’s discuss; how we can take it a notch up! Let’s see the issue with our setup at level-4:</p>\n<p>❌ Does not work on mobile data network.<br />\n❌ If the DNS server is outside home (cloud), DNS port(s) needs to be open to the public internet.</p>\n<blockquote>\n<p><strong>Solution:</strong> We build on the setup from level-4 &amp; create a private-p2p network for our devices &amp; server. We shall use tailscale for this. The free tier is good enough to create a secure P2P network among 20 devices.</p>\n</blockquote>\n<p><a href=\"https://tailscale.com/\">Tailscale</a></p>\n<h5>Steps: <a href=\"https://tailscale.com/kb/1114/pi-hole/\">Reference</a></h5>\n<ol>\n<li>Install tailscale on all your server(s), phones &amp; laptops. This shall provide all your devices a permanent IP address which is only accessible over your tailscale devices</li>\n<li><code>tailscale.com &gt; DNS settings</code>: Set the adguard home DNS server’s IP.</li>\n<li>Make sure no ports are exposed on servers.</li>\n<li>That’s it! Now when you want to get rid of ads on your phone - just turn on tailscale application.</li>\n</ol>\n<blockquote>\n<p>Level 5 is not an end goal - but the beginning of setting up of your private home lab.</p>\n</blockquote>\n<hr />\n<h4>Attribution &amp; References</h4>\n<p><a href=\"https://www.vecteezy.com/free-vector/advertising\">Advertising Vectors by Vecteezy</a></p>\n<p><a href=\"https://github.com/AdguardTeam/AdGuardHome\">GitHub - AdguardTeam/AdGuardHome: Network-wide ads &amp; trackers blocking DNS server</a></p>\n","date_published":"2022-06-05T00:08:00.000Z","date_modified":"2022-06-07T08:24:30.000Z","tags":["adblock","ad","ads-vs-humanity"],"image":"https://www.alice-bob.tech/_astro/cover.CxfWuMKG_1FWEco.jpeg","authors":[{"name":"Mayank Raj","url":"https://www.alice-bob.tech/about/"}]}]}