20141208

How to Get a Restaurant Reservation with only 57 lines of Code

The restaurant I wanted takes reservations six weeks in advance, every Monday at 10:30am.  So every Monday I would call a few times, and then give up. I don't have time for this nonsense!

Then I got mad and realized I could use Twilio.  Twilio is a neat system that provides programmatic access to the phone system — making calls, receiving calls, conference calls, recording calls and sending/receiving text messages.

Twilio was fun to learn. Once you get it working, you can make phone calls from the command line! I wrote a little script that would call any number, and if it was busy, hang up and try again right away. Once it gets through, then it adds them to a conference call, which I'm already in.

So this morning, the moment of truth came and we started dialing away.  Six minutes and seventy call attempts later, I was in!

Easy :)


20140909

Using AppleScript to block access to distracting websites at night

The goal: block access to timewaster websites at night so that they don't keep you up late.  My Superego program can kill your whole browser at night, but sometimes that's too severe and you just want certain sites to be blocked.

I chose AppleScript as the tool for the job.  I hadn't written AppleScript before, it was almost fun.


set t to (time of (current date))  
if (t < (6 * 60 * 60)) or (t > (22.25 * 60 * 60)) then   -- when it's after 10:15pm or before 6am
tell application "Google Chrome"
repeat with w in windows
repeat with t in tabs of w
repeat with badurl in {"reddit", "ycombinator", "twitch"}
if URL of t contains badurl then
close t
exit repeat
end if
end repeat
end repeat
end repeat
end tell
tell application "Safari"
repeat with w in windows
repeat with t in tabs of w
repeat with badurl in {"reddit", "ycombinator", "twitch"}
if URL of t contains badurl then
close t
exit repeat
end if
end repeat
end repeat
end repeat
end tell
tell application "System Events"
set processList to get the name of every process whose background only is false
if processList contains "Firefox" then
-- display dialog "Firefox is running"
tell application "Firefox"
activate
tell application "System Events"
keystroke "l" using {command down}
keystroke "c" using {command down}
set currenturl to the clipboard
repeat with badurl in {"reddit", "ycombinator", "twitch"}
if currenturl contains badurl then
keystroke "w" using {command down}
end if
end repeat
end tell
end tell
end if
end tell
end if


(The Firefox code is different from the Safari/Chrome code because Firefox doesn't support the same AppleScript interaction.)

Then, I run this script by cron once per minute, using the osascript command.

20140523

Data-diving my way to a winning Gold-league PvZ build

PvZ has been my weak spot for a long time.  Very frustrating, and I felt like I had no idea what I was doing.

I finally buckled down and took a deep dive into the data, looking for a strat that would work for a Gold-league scrub like myself.  In particular, I was looking for a strat that would work from silver/gold up to gold/plat and didn't require an APM over 80.

My site GGTracker has millions of replays, and there are several thousands of PvZs in there played since the last patch (March 1st) and in my league range.  Zergs win ZvP in Gold/Plat about 55% of the time, since March 1st.

Then I split up the games by what time Protoss completes their natural expansion.  The majority of Protoss complete their Nexus at 6:00 or later (meaning they start it at 4:20 or later), and those players lose 60% of the time.

FlatlineSC2's 1 GateFE into 4 Gate Pressure into Immortal/Sentry All-In starts its Nexus at 3:50, finishing at 5:30.  That seemed like fun to me, and the PvZ winrate associated with that timing wasn't as bad.

In that build, the all-in immortal/sentry army moves out around 10:00, so I used that as a benchmark: 45+ workers, 6+ sentries, 2+ immortals.  Of course the GGTracker replays might be a biased sample, but in those 92 games, the Protoss wins 80% of the time.

I like those odds!

After practicing twice against the AI, I played the strat on ladder and...   I won!


I realize that "Immortal/Sentry" is nothing new.  What was new to me was:

  • confirming from data that it actually works in the Gold-league range (after all, some strats are solid but require too much multi-tasking or micro to work in Gold)
  • confirming that the win rate is relatively APM-independent (at APMs of 60 or above, there is no clear relationship between APM and win rate for this strat)
  • being able to watch a bunch of gold-league level replays of people playing this strat, to see exactly how they scouted, protected their expansion, how many gateways, and all the other little details.


In my particular game, the immortal/sentry push at 10:00 did a lot of economic damage, but due to my crappy micro and army positioning I wasn't able to finish the game right then and there.  After another so-so push at 21:00, some proxy zealots spied that he was going broods, so when his brood army showed up, I was ready with voids and stalkers and he had a bad time.

What do you think, is this strat actually viable or am I being fooled by these 92 games?

20140507

Superego


"You really do need a system"  —Leonard Shelby, Memento


Until a few months ago, a lot of my evenings went like this: after everyone else went to sleep, I'd fire up the computer, grab a sleeve of crackers and a fresh tub of Nutella, play video games, and read Facebook, Twitter and Hacker News.  I was rolling!  I'd roll straight through to one-thirty, two and beyond.


Basically this


The next day, I'd wake up bleary-eyed with a nasty grumpy brain fog.  Bad, bad, bad, I was mad at myself for being so stupid.  I did the math and on average, I was getting six hours' sleep, not really enough.  And in retrospect, the late nights weren't even that much fun.  Like eating a whole bag of potato chips in one sitting, in recollection, they don't seem so delicious.

I tried to be more responsible, many times, but nothing worked for long.  During the day I wanted to do better; but when night came, I wanted to stay up and play.  I started tracking my sleep with the Jawbone UP; that helped for about two weeks.


A device for measuring my failure


Three months ago, after yet another morning waking up tired and angry, I realized that simply wishing and wanting to do better was not enough.  I put on my thinking cap.  What was actually going to fix the problem for real?   Well, I'm a software programmer; I program things.  I can't program myself, but I can program my computer — to simply kick me off after a certain time of night.

So I built a system, called Superego, which does just that.  It works like this:

A) The Jawbone UP tracks my sleep.
B) Superego sets the day's Computer Curfew time based on how much sleep I've been getting lately.
C) At Computer Curfew time, Superego shuts off all web browsing and games until 6am the next morning.

Now I'm getting seven and a half hours of sleep a night.






Some common questions:


I need this!  Let me have it!

If you've got a Mac, sign up and go for it!



Can't you just work around Superego by using your computer's administrator password?

Yes, and that's why I hid my administrator password.  I made a new password and printed out five copies.  One is at work, one's in the cellar, another's in my wife's nightstand, and there's one in each of my kid's piggy banks.  During the day, I can easily get the password if I need it.  But at night, my wife is a light sleeper, so if I try to open her nightstand drawer, she'll catch me.  If I make the long trip down to the cellar, I'll come to my senses, give up and just go to bed.

Most important, if I do somehow circumvent the system and stay up late, the next day I will do what it takes to prevent a recurrence — for example, hide the passwords in places even more remote.


How is the Computer Curfew time set?

The Computer Curfew time adjusts every day based on how much sleep I'm getting lately.

It's like how you hit a target with a bow and arrow on a windy day.  You aim and shoot; if the arrow falls a little to the left, you aim your next shot a little further to the right.  With the Computer Curfew, the target is seven and half hour sleep.  When I get less sleep than the target, the next day's Curfew is earlier; if more, later.

For example, last night was a good night — eight hours, sixteen minutes, more sleep than I need — so tonight's curfew is 10:25pm, fourteen minutes later than the previous night.

The actual math is nothing too fancy; a weighted average to compute recent sleep, and another to compute the recent curfew.  If recent average sleep is ten minutes more than the target, then tonight's curfew will be ten minutes later than the recent average curfew.


Can't you just stay up late watching TV or reading instead?

For whatever reason, late-night TV and reading are not a problem for me; I don't like TV, and when I read at night, I fall asleep pretty fast.


This would never work for me, my problems are different.

Indeed, people have all kinds of different sleep-related issues.  If you think a modified version of this system could help you, let me know.  I love doing this kind of self-improvement stuff.




Why so much effort and nonsense, why don't you just go to sleep at a reasonable time?

Ha!  If you are one of those "willpower" people who can just decide to do something and then actually do it, good for you.  The rest of us aren't so lucky.




I have more questions!

Please ask away!  I'll add the common ones to this article.