Skip to content

Instantly share code, notes, and snippets.

Binary search is very easy to mess up. Here's my intuition - with it, my binary searches work first time, every time, with no off-by-ones.

This is loosely based off of We Need To Talk About Binary Search, which is a great explanation but is a bit finicky with its +1s, -1s and best_so_fars.

  • Binary search is about finding when a predicate function flips. That is, below a certain unknown value it's always false/true, and above that it's always the other value.
    • Visually, it's a step function but you don't know where the "step" is.
  • Write your predicate. Actually write it out - define a function and everything. It makes the code for the binary search much nicer to read and reason about. We'll get into examples of predicates below.
  • **The loop invariant for binary search
@mcpower
mcpower / aoc-tips.md
Last active June 24, 2026 20:29
Tips for getting on the Advent of Code leaderboard

Hi, I'm mcpower. I've done Advent of Code seriously for two years now in Python, placing 9th in 2018 and 12th in 2017. This year, I'm taking a break from aiming for the leaderboard - while it's fun and all, it is a bit stressful at times (the good kind of stress, though!). As such, I'd like to share a few tips for anyone wanting to aim for the leaderboard.

This is everything that worked for me. Your mileage may vary, though - don't take this as gospel, see what works for you.

Go fast

Go fast.

Keybase proof

I hereby claim:

  • I am mcpower on github.
  • I am mcpower (https://keybase.io/mcpower) on keybase.
  • I have a public key ASAP9e2zkiWgreVGbhxKp_i0PWAi0Tn1lqS6B__Dbpzlfgo

To claim this, I am signing this object:

@mcpower
mcpower / aoc-2018.css
Last active November 21, 2024 11:01 — forked from lynn/aoc-2018.css
My user stylesheet for Advent of Code
/* Sans-serif for most content. */
body { font-family: "Ubuntu", "Segoe UI", sans-serif; line-height: 1.3em; }
input~span:before, .leaderboard-entry, .privboard-row { font-family: "Source Code Pro", monospace; letter-spacing: -1px; }
/* Emphasis = bold, instead of glowy. */
body * { text-shadow: none !important; }
article em { font-weight: bold; }
/* Center content. */
main>article, main>p, pre.calendar, pre.stats, form, .leaderboard-entry, header { width: 40em !important; margin-left: auto; margin-right: auto; }
cloudprint
googleplus
docs
drive
fitness
keep
newswidget
books
playgames
movies
@mcpower
mcpower / dynasty_preloader.user.js
Last active January 9, 2017 12:05
Dynasty Preloader
// ==UserScript==
// @name Dynasty Preloader
// @namespace http://tampermonkey.net/
// @version 0.3.5.1
// @description Preloads Dynasty Reader pages.
// @author mcpower
// @match http://dynasty-scans.com/chapters/*
// @grant none
// @license GPLv3
// ==/UserScript==
diff -ur old/extractwifi.c new/extractwifi.c
--- old/extractwifi.c 2016-12-29 23:04:40.000000000 +1100
+++ new/extractwifi.c 2016-12-30 16:00:29.106186400 +1100
@@ -51,7 +51,7 @@
int main(int argc, char** argv) {
if(argc<2) {
printf("Usage:\n");
- printf("./extractwifi filename\n");
+ printf("./extractwifi filename [offset=b730]\n");
return -1;
@mcpower
mcpower / fix.ps1
Created October 2, 2016 09:42
Fixing broken Windows 10 apps
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\ContactSupport_cw5n1h2txyewy\appxmanifest.xml" -Register
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\Microsoft.AAD.BrokerPlugin_cw5n1h2txyewy\appxmanifest.xml" -Register
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\Microsoft.AccountsControl_cw5n1h2txyewy\appxmanifest.xml" -Register
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\Microsoft.BioEnrollment_cw5n1h2txyewy\appxmanifest.xml" -Register
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\Microsoft.LockApp_cw5n1h2txyewy\appxmanifest.xml" -Register
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\appxmanifest.xml" -Register
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\Microsoft.PPIProjection_cw5n1h2txyewy\appxmanifest.xml" -Register
Add-AppxPackage -DisableDevelopmentMode "$env:windir\systemapps\Microsoft.Windows.AppRep.ChxApp_cw5n1h2txyewy\appxmanifest.x
@mcpower
mcpower / osugame_func+.user.js
Created March 15, 2016 00:12
Userscript for /r/osugame
// ==UserScript==
// @name osugame_func+
// @namespace /r/osugame
// @author /u/N3G4
// @description Adds osu! related functionality to /r/osugame
// @include *reddit.com/r/osugame*
// @version 1.2.2
// @run-at document-end
// @grant GM_xmlhttpRequest
// ==/UserScript==
#Hotstring EndChars :
#Hotstring O ?
:::+1::👍
:::-1::👎
:::100::💯
:::1234::🔢
:::8ball::🎱
:::a::🅰️
:::ab::🆎
:::abc::🔤