Posts

CodinGame: Building an AI to play (and win) at "Penguins"

Image
This November, I came back to the US from visiting South Korea, and while I was self quarantining, I decided to get back into solving puzzles on CodinGame . A game called "Penguins" popped up as the weekly challenge, so I decided to give it a shot. I worked on building an AI for a few days, and made decent progress, but then took a break to compete in the Fall Challenge. After the Fall Challenge was over, I decided to come back to the Penguins game and try to get my AI to first place. I finally achieved my goal on December 3rd! I did this using a variation of Monte Carlo Tree Search with a custom evaluation function for scoring the gamestate. Leaderboard (December 2020) KnightMoves, that's me! What is Penguins? Penguins is a game played on a hexagonal grid, and the goal is to move your penguins around the board and eat as many fish as possible. As you move around, the ice breaks, so you have to be careful not to get trapped or stranded. It's based on the boardgame ...

Introduction to Inheritance and the Prototype Chain in JavaScript (with diagrams)

Image
In this post I’m going to demonstrate how the JavaScript prototype chain works by example, starting with the creation of basic objects and functions and working up to inheritance.  To do this I’ll execute commands in the Chrome JavaScript Console, and then I’ll use diagrams to show the state of the system.  To follow along, open the Chrome JavaScript Console.  Start Chrome and then press Ctrl+Shift+J (or Cmd+Option+J on a Mac). Objects and the Prototype Chain First I’ll create an empty object, x: > var x = {} > dir(x) // used to inspect x. What’s going on here? - I’ve created an object, x, and it has a single property, __proto__. - The value of __proto__ is set to Object.prototype. I can type the following statement into the console to verify the value of x.__proto__: > x.__proto__ === Object.prototype // true So what is this __proto__ property, and why does x have it?  In JavaScript, every object is created with a __proto__ property...

Every Time You Write a Regular Expression You Should Be Required to Put Sample Input in a Comment

There's a reason PERL has a reputation for being unreadable: regular expressions are almost impossible to decipher without any context. Consider seeing the following in a python script: conf_regex = " *(\d+) +(\d+\.\d+\.\d+\.\d+)/(\d+) +(\d+\.\d+\.\d+\.\d+) +(\d+) +(.+) *" From the variable name you can infer it's being used to parse a conf file, but that's about it.  Now compare that to the following: # regex for parsing conf file, example entry below # #id     network             addr        port    connections # 0       155.246.80.0/24     127.0.0.1   5000    0,3 conf_regex = " *(\d+) +(\d+\.\d+\.\d+\.\d+)/(\d+) +(\d+\.\d+\.\d+\.\d+) +(\d+) +(.+) *" Suddenly it's not so bad.

aspire 4830T no sound ubuntu fix

I just bought a new Aspire TimelineX AS4830T-6642 laptop, and when I installed Ubuntu 11.04 64-bit I had no sound whatsoever. If you have the same problem here's what to do: The fix is here: http://people.canonical.com/~diwic/temp/alsa-hda-dkms-acer3830tg_0.1_all.deb Download the file and save it.  Open it and the Ubuntu Software Center should come up with an option to install.  Install it (if it says that it's untrusted, ignore this and proceed).  Once the installation is complete restart your computer and you should have sound. note1: This fix originally came from post #76 of this bug: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/783582 note2: Make sure you do this on a fresh install of Ubuntu 11.04, one where you have not already tried other fixes.  I originally applied this patch after trying to install some ALSA drivers and screwing around with some other stuff, and it got my sound working, but jack sense was broken.  After doing a fres...

Web Deployment With Git

This is my solution to do the following 1) deploy a website from my local machine to my server in one step 2) keep a git repository on the server (mainly to have it as backup) I use a combination of git push and rsync to achieve this. Create git repositories on your local machine and the server. 1) create a bare git repository on the server (this is the repository that will be pushed to) $ mkdir /home/cogan/git_projects/myproject.git $ cd myproject $ git init --bare 2) create a git repository on the development machine (where you will do the deployment from) $ mkdir /home/cogan/projects/myproject $ cd myproject $ git init add a test file to the git repository and commit $ echo "<h1>[G]It Works!</h1>" > test.html $ git add test.html $ git commit -m "initial commit" Set up remote repository 3) On your local machine add the repository on the server as a remote repository called 'web' $ git remote add web ss...

Terminate Scripted Minicom Session

If you start a minicom session with the -S you can automate a minicom session with runscript.  This starts two separate processes, the minicom process and the runscript process.  If you want to close the minicom session when the script is finished, simply calling exit in the runscript isn't enough. To End the minicom session you have to kill the minicom process, using the ! operator within the script to execute an external command. The easiest method is to run ! killall minicom This isn't recommended if you might have multiple minicom processes running.  A better way is to use pkill to target the correct minicom session ! pkill -f "^minicom conf_file -S script_file$" Where conf_file and script_file are the names of your minicom files.  pkill uses a regex to kill the process that you specify. Of course to use pkill you have to be able to generate the script file dynamically, or make sure you always call minicom with the same command when you use it....

What Twitter Tells Us

I was in the computer lab at work the other day and my coworker was watching a gamecast of a NY Yankees preseason game online.  Now let me tell you, this guy is a Yankees fanatic.  He's the kind of guy who has every player's batting average memorized to three decimal places.  Just to give you an idea, he brought in cake for the entire lab just to celebrate the first day of Spring Training. Anyway, one of the other guys joked that throughout the season we should remotely hijack his computer at critical moments during the games and kill his browser or play other dastardly pranks.  We quickly started talking about automating this process (we are software engineers, after all), and I had the idea that we should monitor twitter for tweets about the Yankeers and perform a shutdown whenever the buzz reached a certain threshold.  It seemed like it might actually work, and it got me thinking about twitter and what sort of secrets it may hold. I read an article a litt...