# Image Battle of Colours

## CONGRATULATIONS to @kuroineko for the best entry and winning the 200 bounty from @TheBestOne (excellent sportsmanship!).

Write a program to colour as much of an image as possible before opposition programs do.

## Rules in brief

• Your program will be given an image, your colour, and integer N.
• You can update any white pixel that is next to a pixel of your colour.
• The program that has added the most pixels wins.

## Rules in detail

Your program will be given a PNG image filename, home colour, and a number N. The number N is the maximum number of pixels your program may colour each turn.

Example: MyProg arena.png (255,0,0) 30

The input image will be a rectangle with sides between 20 and 1000 pixels long. It will consist of black, white, and colour pixels. Your program may choose a sequence of white pixels to colour as your own, with the condition that each new pixel must have at least one of its four neighbour pixels of your own colour. The image will initially have at least one pixel of your colour. It may also have pixels of colours that no program is assigned to. The alpha channel is not used.

Your goal is to block your opponents and write your colour into as many pixels as you can.

Each turn your program will accept 1 or more message lines on STDIN, and write a line consisting of pixel coordinates on STDOUT. Remember to assign STDOUT as unbuffered or flush the STDOUT buffer each turn.

The order of players called each turn will be randomly assigned. This means that an opponent (or your program) may have 2 turns in a row.

Your program will be sent colour (N,N,N) chose X,Y X,Y ... X,Y information messages that describe the pixels filled in by player programs. If a player makes no moves, or no valid moves, you will not be sent a message about that player's moves. Your program will also be sent a message about your own accepted moves (if you have specified at least one valid move). The pixel 0,0 is in the top left corner of the image.

On receiving pick pixels, your program will output X,Y X,Y ... X,Y up to N pixels (an empty string consisting of just a '\n' is allowed). The pixels must be in order of plotting. If a pixel is invalid, it will be ignored and not be in the report to players. Your program has 2 seconds to initialise after starting, but only 0.1 second to reply with an answer each turn or it will miss that turn. A pixel update sent after 0.1 second will record a fault. After 5 faults your program is suspended and will not be sent updates or pick pixels requests.

When the judge program receives an empty or invalid pixel choice from every non-suspended player program, the image will be considered complete and programs will be sent the message "exit". Programs must terminate after receiving "exit".

## Scoring

The judge will score points after the image is complete. Your score will be your number of pixels updated divided by the average pixel capture that round, expressed as a percentage.

The number of pixels added to the image by your player is A. The total number of pixels added by all P players is T. avg = T/P score = 100*A/avg

## Posting scores

A reference opponent "The Blob" is given. For each answer, title your bot with a name, language, and your score (average of arena 1 to 4) against the reference opponent. A picture or animation of one of your battles would be good too. The winner is the program with the highest score against the reference bot.

If The Blob proves too easy to beat, I may add a second round with a stronger reference opponent.

You may also like to experiment with 4 or more player programs. You could also test your bot against other bots posted as answers.

## The Judge

The judge program requires the common Python Imaging Library (PIL) and should be easy to install from your OS package manager on Linux. I have a report that PIL does not work with 64 bit Python on Windows 7, so please check if PIL will work for you before starting this challenge (updated 2015-01-29).

#!/usr/bin/env python
# Judge Program for Image Battle challenge on PPCG.
# Runs on Python 2.7 on Ubuntu Linux. May need edits for other platforms.
# V1.0 First release.
# V1.2 Added Java inner class support
# usage: judge cfg.py
import sys, re, random, os, shutil, subprocess, datetime, time, signal
from PIL import Image

ORTH = ((-1,0), (1,0), (0,-1), (0,1))
def place(loc, colour):
# if valid, place colour at loc and return True, else False
if pix[loc] == (255,255,255):
plist = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
if any(pix[p]==colour for p in plist if 0<=p[0]<W and 0<=p[1]<H):
pix[loc] = colour
return True
return False

def updateimage(image, msg, bot):
if not re.match(r'(\s*\d+,\d+)*\s*', msg):
return []
plist = [tuple(int(v) for v in pr.split(',')) for pr in msg.split()]
plist = plist[:PIXELBATCH]
return [p for p in plist if place(p, bot.colour)]

class Bot:
botlist = []
def __init__(self, name, interpreter=None, colour=None):
self.prog = name
self.botlist.append(self)
callarg = re.sub(r'\.class$', '', name) # Java fix self.call = [interpreter, callarg] if interpreter else [callarg] self.colour = colour self.colstr = str(colour).replace(' ', '') self.faults = 0 self.env = 'env%u' % self.botlist.index(self) try: os.mkdir(self.env) except: pass if name.endswith('.class'): # Java inner class fix rootname = re.sub(r'\.class$', '', name)
for fn in os.listdir('.'):
if fn.startswith(rootname) and fn.endswith('.class'):
shutil.copy(fn, self.env)
else:
shutil.copy(self.prog, self.env)
shutil.copy(imagename, self.env)
os.chdir(self.env)
args = self.call + [imagename, self.colstr, PIXELBATCH]
self.proc = subprocess.Popen(args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.chdir('..')
def send(self, msg):
if self.faults < FAULTLIMIT:
self.proc.stdin.write(msg + '\n')
self.proc.stdin.flush()
if self.faults < FAULTLIMIT:
start = time.time()
if time.time() - start > timelimit:
self.faults += 1
inline = ''
return inline.strip()
def exit(self):
self.send('exit')

from cfg import *
for i, (prog, interp) in enumerate(botspec):
Bot(prog, interp, colourspec[i])

image = Image.open(imagename)
W,H = image.size

time.sleep(INITTIME)
total = 0
for turn in range(1, MAXTURNS+1):
random.shuffle(Bot.botlist)
nullbots = 0
for bot in Bot.botlist:
bot.send('pick pixels')
newpixels = updateimage(image, inmsg, bot)
total += len(newpixels)
if newpixels:
pixtext = ' '.join('%u,%u'%p for p in newpixels)
msg = 'colour %s chose %s' % (bot.colstr, pixtext)
for msgbot in Bot.botlist:
msgbot.send(msg)
else:
nullbots += 1
if nullbots == len(Bot.botlist):
break
if turn % 100 == 0: print 'Turn %s done %s pixels' % (turn, total)
for msgbot in Bot.botlist:
msgbot.exit()

counts = dict((c,f) for f,c in image.getcolors(W*H))
avg = 1.0 * sum(counts.values()) / len(Bot.botlist)
for bot in Bot.botlist:
score = 100 * counts[bot.colour] / avg
print 'Bot %s with colour %s scored %s' % (bot.prog, bot.colour, score)
image.save(BATTLE+'.png')


## Example Config - cfg.py

BATTLE = 'Green Blob vs Red Blob'
MAXTURNS = 20000
PIXELBATCH = 10
INITTIME = 2.0
TIMELIMIT = 0.1
FAULTLIMIT = 5

imagename = 'arena1.png'

colourspec = (0,255,0), (255,0,0)

botspec = [
('blob.py', 'python'),
('blob.py', 'python'),
]


## The Blob - the reference opponent

# Blob v1.0 - A reference opponent for the Image Battle challenge on PPCG.
import sys, os
from PIL import Image

image = Image.open(sys.argv[1])
W,H = image.size
mycolour = eval(sys.argv[2])
pixbatch = int(sys.argv[3])

ORTH = ((-1,0), (1,0), (0,-1), (0,1))
def canchoose(loc, colour):
if pix[loc] == (255,255,255):
plist = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
if any(pix[p]==colour for p in plist if 0<=p[0]<W and 0<=p[1]<H):
return True
return False

def near(loc):
plist = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
pboard = [p for p in plist if 0<=p[0]<W and 0<=p[1]<H]
return [p for p in pboard if pix[p] == (255,255,255)]

def updateimage(image, msg):
ctext, colourtext, chose, points = msg.split(None, 3)
colour = eval(colourtext)
plist = [tuple(int(v) for v in pr.split(',')) for pr in points.split()]
for p in plist:
pix[p] = colour
if colour == mycolour:
for np in near(p):

board = [(x,y) for x in range(W) for y in range(H)]
skin = set(p for p in board if canchoose(p, mycolour))

while 1:
if msg.startswith('colour'):
updateimage(image, msg.strip())
if msg.startswith('pick'):
plen = min(pixbatch, len(skin))
moves = [skin.pop() for i in range(plen)]
movetext = ' '.join('%u,%u'%p for p in moves)
sys.stdout.write(movetext + '\n')
sys.stdout.flush()
if msg.startswith('exit'):
break

image.save('blob.png')


## An Example Battle - Blob vs Blob

This battle had a predictable result:

Bot blob.py with colour (255, 0, 0) scored 89.2883333333
Bot blob.py with colour (0, 255, 0) scored 89.365


• Are you sure this shouldn't be a [king-of-the-hill]? Dec 29, 2014 at 9:14
• I thought about that. The bots do not battle each other directly. They battle the reference bot. Does that rule out KOTH? Dec 29, 2014 at 9:31
• Yes, as this is, it is not a KOTH, I was asking if you were sure you wanted to battle the reference bot rather than each other. Dec 29, 2014 at 9:32
• @TheBestOne, Added Java support. Untested with Java program though. Let me know if it doesn't work. Dec 30, 2014 at 3:12
• The 10 pixels are placed in order, so later pixels may rely on previous pixel placements. They can build on each other as you suggest. Jan 2, 2015 at 3:44

# Score = 111.475388276 153.34210035

Update : Now using a custom Set class to get the pop() method to produce a sort of grid pattern which drastically improves the area covered in the beginning cutting off large parts of the image from the enemy. Note : I am using a 12 x 12 grid for this which of a random sample of grid sizes seemed to give the best results for arena3 (the one that got the worst score before the update), however it is very likely that a more optimal grid size exists for the given selection of arenas.

I went for a simple modification to the reference bot to make it favor picking feasible points that are bordered by as few own-colored points as possible. An improvement might be to make it also favor picking feasible points that are bordered by as many enemy-colored points as possible.

dfblob.py:

import sys, os
from PIL import Image

class RoomyIntPairHashSet:
def __init__(self, firstMax, secondMax):
self.m1 = firstMax
self.m2 = secondMax
self.set = [set() for i in range((firstMax - 1) * (secondMax - 1) + 1)]
self.len = 0

subset = self.set[self.gettuphash(tup)]
self.len -= len(subset)
self.len += len(subset)

subset = self.set[self.gettuphash(tup)]
self.len -= len(subset)
self.len += len(subset)

def pop(self):
for s in self.set:
if len(s) > 0:
self.len -= 1
return s.pop()
return self.set[0].pop()

def gettuphash(self, tup):
return (tup[0] % self.m1) * (tup[1] % self.m2)

def __len__(self):
return self.len

gridhashwidth = 12
gridhashheight = 12
image = Image.open(sys.argv[1])
W,H = image.size
mycolour = eval(sys.argv[2])
pixbatch = int(sys.argv[3])

ORTH = ((-1,0), (1,0), (0,-1), (0,1))
def canchoose(loc, virtualneighbors, colour, num_neighbors):
if pix[loc] == (255,255,255):
plist = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
actual_num_neighbors = 0
for p in plist:
if 0<=p[0]<W and 0<=p[1]<H and pix[p]==colour or p in virtualneighbors:
actual_num_neighbors += 1
return num_neighbors == actual_num_neighbors
return False

def near(loc, exclude):
plist = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
pboard = [p for p in plist if 0<=p[0]<W and 0<=p[1]<H]
return [p for p in pboard if pix[p] == (255,255,255) and p not in exclude]

def updateimage(image, msg):
ctext, colourtext, chose, points = msg.split(None, 3)
colour = eval(colourtext)
plist = [tuple(int(v) for v in pr.split(',')) for pr in points.split()]
for p in plist:
pix[p] = colour
for i in range(len(skins)):
if colour == mycolour:
for np in near(p, []):
for j in range(len(skins)):
if canchoose(np, [], mycolour, j + 1):

board = [(x,y) for x in range(W) for y in range(H)]
skins = []
for i in range(1, 1 + len(ORTH)):
skin = RoomyIntPairHashSet(gridhashwidth, gridhashheight)
skins.append(skin)
for p in board:
if canchoose(p, [], mycolour, i):

while 1:
print("got message "+ msg, file=sys.stderr)
if msg.startswith('colour'):
print("updating image", file=sys.stderr)
updateimage(image, msg.strip())
print("updated image", file=sys.stderr)
if msg.startswith('pick'):
moves = []
print("picking moves", file=sys.stderr)
virtualskins = [RoomyIntPairHashSet(gridhashwidth, gridhashheight) for i in range(len(skins))]
for i in range(pixbatch):
for j in range(len(skins)):
if len(virtualskins[j]) > 0 or len(skins[j]) > 0:
move = None
if len(virtualskins[j]) > 0:
move = virtualskins[j].pop()
else:
move = skins[j].pop()
moves.append(move)
print("picking move (%u,%u) " % move, file=sys.stderr)
for p in near(move, moves):
for k in range(len(skins)):
if canchoose(p, moves, mycolour, k + 1):
break
movetext = ' '.join('%u,%u'%p for p in moves)
print("picked %u moves" % (len(moves)), file=sys.stderr)
sys.stdout.write(movetext + '\n')
sys.stdout.flush()
if msg.startswith('exit') or len(msg) < 1:
break

image.save('dfblob.png')


The original judge has been modified slightly to work with Python 3.2 (and to add a crude logging functionality to the bots + save picture of arena periodically to make animation) :

import sys, re, random, os, shutil, subprocess, datetime, time, signal, io
from PIL import Image

ORTH = ((-1,0), (1,0), (0,-1), (0,1))
def place(loc, colour):
# if valid, place colour at loc and return True, else False
if pix[loc] == (255,255,255):
plist = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH]
if any(pix[p]==colour for p in plist if 0<=p[0]<W and 0<=p[1]<H):
pix[loc] = colour
return True
return False

def updateimage(image, msg, bot):
if not re.match(r'(\s*\d+,\d+)*\s*', msg):
return []
plist = [tuple(int(v) for v in pr.split(',')) for pr in msg.split()]
plist = plist[:PIXELBATCH]
return [p for p in plist if place(p, bot.colour)]

class Bot:
botlist = []
def __init__(self, name, interpreter=None, colour=None):
self.prog = name
self.botlist.append(self)
callarg = re.sub(r'\.class$', '', name) self.call = [interpreter, callarg] if interpreter else [callarg] self.colour = colour self.colstr = str(colour).replace(' ', '') self.faults = 0 self.env = 'env%u' % self.botlist.index(self) try: os.mkdir(self.env) except: pass shutil.copy(self.prog, self.env) shutil.copy(imagename, self.env) os.chdir(self.env) args = self.call + [imagename, self.colstr, str(PIXELBATCH)] errorfile = 'err.log' with io.open(errorfile, 'wb') as errorlog: self.proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=errorlog) os.chdir('..') def send(self, msg): if self.faults < FAULTLIMIT: self.proc.stdin.write((msg+'\n').encode('utf-8')) self.proc.stdin.flush() def read(self, timelimit): if self.faults < FAULTLIMIT: start = time.time() inline = self.proc.stdout.readline().decode('utf-8') if time.time() - start > timelimit: self.faults += 1 inline = '' return inline.strip() def exit(self): self.send('exit') from cfg import * for i, (prog, interp) in enumerate(botspec): Bot(prog, interp, colourspec[i]) image = Image.open(imagename) pix = image.load() W,H = image.size os.mkdir('results') time.sleep(INITTIME) total = 0 for turn in range(1, MAXTURNS+1): random.shuffle(Bot.botlist) nullbots = 0 for bot in Bot.botlist: bot.send('pick pixels') inmsg = bot.read(TIMELIMIT) newpixels = updateimage(image, inmsg, bot) total += len(newpixels) if newpixels: pixtext = ' '.join('%u,%u'%p for p in newpixels) msg = 'colour %s chose %s' % (bot.colstr, pixtext) for msgbot in Bot.botlist: msgbot.send(msg) else: nullbots += 1 if nullbots == len(Bot.botlist): break if turn % 100 == 0: print('Turn %s done %s pixels' % (turn, total)) image.save("results/"+BATTLE+str(turn//100).zfill(3)+'.png') for msgbot in Bot.botlist: msgbot.exit() counts = dict((c,f) for f,c in image.getcolors(W*H)) avg = 1.0 * sum(counts.values()) / len(Bot.botlist) for bot in Bot.botlist: score = 100 * counts[bot.colour] / avg print('Bot %s with colour %s scored %s' % (bot.prog, bot.colour, score)) image.save(BATTLE+'.png')  The arena results follow. The dfblob bot was given the red color for all arenas. ## Arena 1 : Bot dfblob.py with colour (255, 0, 0) scored 163.75666666666666 Bot blob.py with colour (0, 255, 0) scored 14.896666666666667  ## Arena 2 : Bot blob.py with colour (0, 255, 0) scored 17.65563547726219 Bot dfblob.py with colour (255, 0, 0) scored 149.57006774236964  ## Arena 3 : Bot blob.py with colour (0, 255, 0) scored 21.09758208782965 Bot dfblob.py with colour (255, 0, 0) scored 142.9732433108277  ## Arena 4 : Bot blob.py with colour (0, 255, 0) scored 34.443810082244205 Bot dfblob.py with colour (255, 0, 0) scored 157.0684236785121  • Your algorithm is the same as the one I implemented in Blob's stronger brother Boxer. I was going to use Boxer if Blob was not enough challenge. Very nice animations too. Dec 31, 2014 at 1:39 • To use PIL in python 3, are you using pillow? Jan 1, 2015 at 16:17 • @githubphagocyte Yes Jan 1, 2015 at 17:00 • What software did you use to make those GIFs? Jan 1, 2015 at 22:10 • @TheBestOne I used ImageMagick specifically the command convert -delay 5 -loop 0 result*.png animated.gif although some of the gifs had to be later manually cut down to be uploaded here Jan 2, 2015 at 13:39 # Swallower # Language = Java # Score = 162.3289512601408075 169.4020975612382575 Seeks enemies out and surrounds. You might have to give it a longer time limit. Could be improved quite a bit. Sometimes prints invalid pixels. Update: Surrounds much faster. Uses another thread to update priorities. Always returns within .1 seconds. Score should be impossible to beat without increasing MAX_TURNS. import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.List; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; public class Swallower { static final byte MY_TYPE = 1; static final byte BLANK_TYPE = 0; static final byte NEUTRAL_TYPE = 2; static final byte ENEMY_TYPE = 3; private static final int WHITE = Color.WHITE.getRGB(); private static final int MAX_TIME = 50; private final int color; private final int N; private final int width; private final int height; private final BufferedReader in; Lock borderLock; private final PriorityBlockingQueue<Pixel> border; private final Set<Pixel> borderSet; private final Thread updater; Lock imageLock; volatile byte[][] image; Lock priorityLock; volatile int[][] priority; volatile boolean updating; volatile private boolean exit; class Pixel implements Comparable<Pixel> { int x; int y; public Pixel(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pixel o) { return priority() - o.priority(); } private int priority() { priorityLock.lock(); int p = priority[x][y]; priorityLock.unlock(); return p; } public byte type() { imageLock.lock(); byte i = image[x][y]; imageLock.unlock(); return i; } public boolean isBorder() { if (type() != BLANK_TYPE){ return false; } for (Pixel p : pixelsAround()){ if (p.type() == MY_TYPE){ return true; } } return false; } public void setType(byte newType) { imageLock.lock(); image[x][y] = newType; imageLock.unlock(); } public void setPriority(int newPriority) { borderLock.lock(); boolean contains = borderSet.remove(this); if (contains){ border.remove(this); } priorityLock.lock(); priority[x][y] = newPriority; priorityLock.unlock(); if (contains){ border.add(this); borderSet.add(this); } borderLock.unlock(); } public List<Pixel> pixelsAround() { List<Pixel> pixels = new ArrayList<>(4); if (x > 0){ pixels.add(new Pixel(x - 1, y)); } if (x < width - 1){ pixels.add(new Pixel(x + 1, y)); } if (y > 0){ pixels.add(new Pixel(x, y - 1)); } if (y < height - 1){ pixels.add(new Pixel(x, y + 1)); } return pixels; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pixel pixel = (Pixel) o; return x == pixel.x && y == pixel.y; } @Override public int hashCode() { int result = x; result = 31 * result + y; return result; } } public static void main(String[] args) throws IOException { BufferedImage image = ImageIO.read(new File(args[0])); int color = parseColorString(args[1]); int N = Integer.parseInt(args[2]); new Swallower(image, color, N).start(); } private void start() throws IOException { updater.start(); try { while (true) { String input = in.readLine(); if (input.equals("exit")) { exit = true; if (!updating) { updater.interrupt(); } return; } else if (input.startsWith("colour")) { updateImage(input); } else if (input.equals("pick pixels")) { if (updating) { try { synchronized (Thread.currentThread()){ Thread.currentThread().wait(MAX_TIME); } } catch (InterruptedException ignored) { } } for (int i = 0; i < N && !border.isEmpty(); i++) { borderLock.lock(); Pixel p = border.poll(); borderSet.remove(p); borderLock.unlock(); if (!p.isBorder()){ i--; continue; } updateImage(MY_TYPE, p); System.out.print(p.x + "," + p.y + " "); } System.out.println(); } } } catch (Throwable e){ exit = true; if (!updating){ updater.interrupt(); } throw e; } } private void updateImage(byte type, Pixel... pixels) { for (Pixel pixel : pixels){ pixel.setType(type); if (type == MY_TYPE){ pixel.setPriority(Integer.MAX_VALUE); } else { pixel.setPriority(0); } } for (Pixel pixel : pixels){ for (Pixel p : pixel.pixelsAround()){ if (p.type() == BLANK_TYPE){ addPixelToUpdate(p); } if (type == MY_TYPE && p.isBorder()){ borderLock.lock(); if (borderSet.add(p)){ border.add(p); } borderLock.unlock(); } } } } private synchronized void addPixelToUpdate(Pixel p) { if (pixelsToUpdateSet.add(p)) { pixelsToUpdate.add(p); if (!updating){ updater.interrupt(); } } } Queue<Pixel> pixelsToUpdate; Set<Pixel> pixelsToUpdateSet; private void update(){ while (true){ if (exit){ return; } if (pixelsToUpdate.isEmpty()){ try { updating = false; while (!exit) { synchronized (Thread.currentThread()) { Thread.currentThread().wait(); } } } catch (InterruptedException ignored){} continue; } updating = true; Pixel pixel = pixelsToUpdate.poll(); if (pixel.type() != BLANK_TYPE){ continue; } pixelsToUpdateSet.remove(pixel); updatePixel(pixel); } } private void updatePixel(Pixel pixel) { int originalPriority = pixel.priority(); int minPriority = Integer.MAX_VALUE; List<Pixel> pixelsAround = pixel.pixelsAround(); for (Pixel p : pixelsAround){ int priority = p.priority(); if (priority < minPriority){ minPriority = priority; } } if (minPriority >= originalPriority){ pixel.setPriority(Integer.MAX_VALUE); pixelsToUpdate.addAll(pixelsAround.stream().filter(p -> p.type() == 0 && p.priority() != Integer.MAX_VALUE).filter(pixelsToUpdateSet::add).collect(Collectors.toList())); } else { pixel.setPriority(minPriority + 1); for (Pixel p : pixelsAround){ if (p.type() == 0 && p.priority() > minPriority + 2){ if (pixelsToUpdateSet.add(p)){ pixelsToUpdate.add(p); } } } } } private void updateImage(String input) { String[] inputs = input.split("\\s"); int color = parseColorString(inputs[1]); byte type; if (color == this.color){ return; } else { type = ENEMY_TYPE; } Pixel[] pixels = new Pixel[inputs.length - 3]; for (int i = 0; i < inputs.length - 3; i++){ String[] coords = inputs[i + 3].split(","); pixels[i] = new Pixel(Integer.parseInt(coords[0]), Integer.parseInt(coords[1])); } updateImage(type, pixels); } private static int parseColorString(String input) { String[] colorString = input.split("[\$$\$$,]"); return new Color(Integer.parseInt(colorString[1]), Integer.parseInt(colorString[2]), Integer.parseInt(colorString[3])).getRGB(); } private Swallower(BufferedImage image, int color, int N){ this.color = color; this.N = N; this.width = image.getWidth(); this.height = image.getHeight(); this.image = new byte[width][height]; this.priority = new int[width][height]; for (int x = 0; x < width; x++){ for (int y = 0; y < height; y++){ int pixelColor = image.getRGB(x,y); priority[x][y] = Integer.MAX_VALUE; if (pixelColor == WHITE){ this.image[x][y] = BLANK_TYPE; } else if (pixelColor == this.color){ this.image[x][y] = MY_TYPE; } else { this.image[x][y] = NEUTRAL_TYPE; } } } border = new PriorityBlockingQueue<>(); borderSet = Collections.synchronizedSet(new HashSet<>()); borderLock = new ReentrantLock(); priorityLock = new ReentrantLock(); imageLock = new ReentrantLock(); for (int x = 0; x < width; x++){ for (int y = 0; y < height; y++){ Pixel pixel = new Pixel(x,y); if (pixel.type() == BLANK_TYPE){ if (pixel.isBorder()){ if (borderSet.add(pixel)){ border.add(pixel); } } } } } in = new BufferedReader(new InputStreamReader(System.in)); updating = false; updater = new Thread(this::update); pixelsToUpdate = new ConcurrentLinkedQueue<>(); pixelsToUpdateSet = Collections.synchronizedSet(new HashSet<>()); exit = false; } }  # How it works: This bot maintains a priority queue of pixels it can add. The priority of an enemy pixel is 0. The priority of a blank pixel is 1 greater than the lowest priority around it. All other pixels have a priority of Integer.MAX_VALUE. The updater thread is constantly updating the priorities of the pixels. Every turn the lowest N pixels are popped off the priority queue. # Green Blob vs Red Swallower # Blob's Score = 1.680553372583887225 # Swallower's Score = 169.4020975612382575 # Arena 1: Bot Blob.py with colour (0, 255, 0) scored 1.2183333333333333 Bot Swallower.class with colour (255, 0, 0) scored 177.435  # Arena 2: Bot Swallower.class with colour (255, 0, 0) scored 149.57829253338517 Bot Blob.py with colour (0, 255, 0) scored 0.5159187091564356  # Arena 3: Bot Blob.py with colour (0, 255, 0) scored 0.727104853136361 Bot Swallower.class with colour (255, 0, 0) scored 163.343720545521  # Arena 4: Bot Swallower.class with colour (255, 0, 0) scored 187.25137716604686 Bot Blob.py with colour (0, 255, 0) scored 4.260856594709419  # Green Swallower vs. Red Blob # Blob's Score = 1.6852943642218457375 # Swallower's Score = 169.3923095387498625 # Arena 1: Bot Blob.py with colour (255, 0, 0) scored 1.3166666666666667 Bot Swallower.class with colour (0, 255, 0) scored 177.33666666666667  # Arena 2: Bot Swallower.class with colour (0, 255, 0) scored 149.57829253338517 Bot Blob.py with colour (255, 0, 0) scored 0.49573058575466195  # Arena 3: Bot Swallower.class with colour (0, 255, 0) scored 163.14367053301788 Bot Blob.py with colour (255, 0, 0) scored 0.9271548656394868  # Arena 4: Bot Swallower.class with colour (0, 255, 0) scored 187.51060842192973 Bot Blob.py with colour (255, 0, 0) scored 4.0016253388265675  # Red Swallower vs Green Depth First Blob # Swallower's Score = 157.0749775233111925 # Depth First Blob's Score = 18.192783547939744 # Arena 1: Bot Swallower.class with colour (255, 0, 0) scored 173.52166666666668 Bot dfblob.py with colour (0, 255, 0) scored 5.131666666666667  # Arena 2: Bot dfblob.py with colour (0, 255, 0) scored 17.25635925887156 Bot Swallower.class with colour (255, 0, 0) scored 149.57829253338517  # Arena 3: Bot Swallower.class with colour (255, 0, 0) scored 153.59801488833747 Bot dfblob.py with colour (0, 255, 0) scored 10.472810510319889  # Arena 4: Bot dfblob.py with colour (0, 255, 0) scored 39.91029775590086 Bot Swallower.class with colour (255, 0, 0) scored 151.60193600485545  # Green Swallower vs Red Depth First Blob # Swallower's Score = 154.3368355651281075 # Depth First Blob's Score = 18.84463249420435425 # Arena 1: Bot Swallower.class with colour (0, 255, 0) scored 165.295 Bot dfblob.py with colour (255, 0, 0) scored 13.358333333333333  # Arena 2: Bot dfblob.py with colour (255, 0, 0) scored 8.91118721119768 Bot Swallower.class with colour (0, 255, 0) scored 149.57829253338517  # Arena 3: Bot Swallower.class with colour (0, 255, 0) scored 157.01136822667206 Bot dfblob.py with colour (255, 0, 0) scored 7.059457171985304  # Arena 4: Bot dfblob.py with colour (255, 0, 0) scored 46.0495522603011 Bot Swallower.class with colour (0, 255, 0) scored 145.4626815004552  # Green Blob vs Red Depth First Blob vs Blue Swallower: # Blob's Score = 6.347962032393275525 # Depth First Blob's Score = 27.34842554331698275 # Swallower's Score = 227.720728953415375 # Arena 1: Bot Swallower.class with colour (0, 0, 255) scored 242.54 Bot Blob.py with colour (0, 255, 0) scored 1.21 Bot dfblob.py with colour (255, 0, 0) scored 24.3525  # Arena 2: Bot dfblob.py with colour (255, 0, 0) scored 17.828356088588478 Bot Blob.py with colour (0, 255, 0) scored 0.9252889892479551 Bot Swallower.class with colour (0, 0, 255) scored 224.36743880007776  # Arena 3: Bot dfblob.py with colour (255, 0, 0) scored 7.105141670032893 Bot Swallower.class with colour (0, 0, 255) scored 226.52057245080502 Bot Blob.py with colour (0, 255, 0) scored 12.621905476369092  # Arena 4: Bot dfblob.py with colour (255, 0, 0) scored 60.10770441464656 Bot Blob.py with colour (0, 255, 0) scored 10.634653663956055 Bot Swallower.class with colour (0, 0, 255) scored 217.45490456277872  Here is Sam Yonnou's judge with a few changes so that you specify the files and the command separately: import sys, re, random, os, shutil, subprocess, datetime, time, signal, io from PIL import Image ORTH = ((-1,0), (1,0), (0,-1), (0,1)) def place(loc, colour): # if valid, place colour at loc and return True, else False if pix[loc] == (255,255,255): plist = [(loc[0]+dx, loc[1]+dy) for dx,dy in ORTH] if any(pix[p]==colour for p in plist if 0<=p[0]<W and 0<=p[1]<H): pix[loc] = colour return True return False def updateimage(image, msg, bot): if not re.match(r'(\s*\d+,\d+)*\s*', msg): return [] plist = [tuple(int(v) for v in pr.split(',')) for pr in msg.split()] plist = plist[:PIXELBATCH] return [p for p in plist if place(p, bot.colour)] class Bot: botlist = [] def __init__(self, progs, command=None, colour=None): self.prog = progs[0] self.botlist.append(self) self.colour = colour self.colstr = str(colour).replace(' ', '') self.faults = 0 self.env = 'env%u' % self.botlist.index(self) try: os.mkdir(self.env) except: pass for prog in progs: shutil.copy(prog, self.env) shutil.copy(imagename, self.env) os.chdir(self.env) args = command + [imagename, self.colstr, str(PIXELBATCH)] errorfile = 'err.log' with io.open(errorfile, 'wb') as errorlog: self.proc = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=errorlog) os.chdir('..') def send(self, msg): if self.faults < FAULTLIMIT: self.proc.stdin.write((msg+'\n').encode('utf-8')) self.proc.stdin.flush() def read(self, timelimit): if self.faults < FAULTLIMIT: start = time.time() inline = self.proc.stdout.readline().decode('utf-8') if time.time() - start > timelimit: self.faults += 1 inline = '' return inline.strip() def exit(self): self.send('exit') from cfg import * for i, (progs, command) in enumerate(botspec): Bot(progs, command, colourspec[i]) image = Image.open(imagename) pix = image.load() W,H = image.size resultdirectory = 'results of ' + BATTLE os.mkdir(resultdirectory) time.sleep(INITTIME) total = 0 image.save(resultdirectory+'/'+'result000.png') for turn in range(1, MAXTURNS+1): random.shuffle(Bot.botlist) nullbots = 0 for bot in Bot.botlist: bot.send('pick pixels') inmsg = bot.read(TIMELIMIT) newpixels = updateimage(image, inmsg, bot) total += len(newpixels) if newpixels: pixtext = ' '.join('%u,%u'%p for p in newpixels) msg = 'colour %s chose %s' % (bot.colstr, pixtext) for msgbot in Bot.botlist: msgbot.send(msg) else: nullbots += 1 if nullbots == len(Bot.botlist): break if turn % 100 == 0: print('Turn %s done %s pixels' % (turn, total)) image.save(resultdirectory+'/result'+str(turn//100).zfill(3)+'.png') image.save(resultdirectory+'/result999.png') for msgbot in Bot.botlist: msgbot.exit() resultfile = io.open(resultdirectory+'/result.txt','w') counts = dict((c,f) for f,c in image.getcolors(W*H)) avg = 1.0 * sum(counts.values()) / len(Bot.botlist) for bot in Bot.botlist: score = 100 * counts[bot.colour] / avg print('Bot %s with colour %s scored %s' % (bot.prog, bot.colour, score)) print('Bot %s with colour %s scored %s' % (bot.prog, bot.colour, score), file=resultfile) image.save(BATTLE+'.png')  Example cfg: BATTLE = 'Green DepthFirstBlob vs Red Swallower @ arena1' MAXTURNS = 20000 PIXELBATCH = 10 INITTIME = 2.0 TIMELIMIT = .1 FAULTLIMIT = 5 imagename = 'arena1.png' colourspec = (0,255,0), (255,0,0) botspec = [ (['DepthFirstBlob.py'], ['python', 'DepthFirstBlob.py']), (['Swallower.class','Swallower$Pixel.class'], ['java', 'Swallower']),
]


Note: Anyone that manages to swallow the Swallower get's a bounty of 100 reputation. Please post in comments below if you succeed at this.

• @githubphagocyte As requested. Jan 1, 2015 at 17:11
• Nice work with the judge changes. Separate file copying and command is a good idea, and the error logging was badly needed. Jan 2, 2015 at 4:45
• If you meant MAXTURNS, feel free to change it. It is not part of the rules. It just stops the judge from running forever (but I guess the termination conditions prevent that anyway). Jan 2, 2015 at 4:49
• @githubphagocyte Fixed Jan 3, 2015 at 2:00
• After looking at your animated battles, I started to wonder what a Swallower vs Swallower battle would look like. Would one quickly trap the other, or would it be a constant fight for domination of space? Jan 4, 2015 at 3:36

# ColorFighter - C++ - eats a couple of swallowers for breakfast

EDIT

• cleaned up the code
• added a simple but effective optimization

## God I hate snakes (just pretend they are spiders, Indy)

Actually I love Python. I wish I were less of a lazy boy and started to learn it properly, that's all.

All this being said, I had to struggle with the 64 bits version of this snake to get the Judge working. Making PIL work with the 64 bits version of Python under Win7 requires more patience than I was ready to devote to this challenge, so in the end I switched (painfully) to the Win32 version.

Also, the Judge tends to crash badly when a bot is too slow to respond.
Being no Python savvy, I did not fix it, but it has to do with reading an empty answer after a timeout on stdin.

A minor improvement would be to put stderr output to a file for each bot. That would ease tracing for post-mortem debugging.

Except for these minor problems, I found the Judge very simple and pleasant to use.
Kudos for yet another inventive and fun challenge.

## The code

#define _CRT_SECURE_NO_WARNINGS // prevents Microsoft from croaking about the safety of scanf. Since every rabid Russian hacker and his dog are welcome to try and overflow my buffers, I could not care less.
#include "lodepng.h"
#include <vector>
#include <deque>
#include <iostream>
#include <sstream>
#include <cassert>   // paranoid android
#include <cstdint>   // fixed size types
#include <algorithm> // min max

using namespace std;

// ============================================================================
// The less painful way I found to teach C++ how to handle png images
// ============================================================================
typedef unsigned tRGB;
#define RGB(r,g,b) (((r) << 16) | ((g) << 8) | (b))
class tRawImage {
public:
unsigned w, h;

tRawImage(unsigned w=0, unsigned h=0) : w(w), h(h), data(w*h * 4, 0) {}
void read(const char* filename) { unsigned res = lodepng::decode(data, w, h, filename); assert(!res);  }
void write(const char * filename)
{
std::vector<unsigned char> png;
unsigned res = lodepng::encode(png, data, w, h, LCT_RGBA); assert(!res);
lodepng::save_file(png, filename);
}
tRGB get_pixel(int x, int y) const
{
size_t base = raw_index(x,y);
return RGB(data[base], data[base + 1], data[base + 2]);
}
void set_pixel(int x, int y, tRGB color)
{
size_t base = raw_index(x, y);
data[base+0] = (color >> 16) & 0xFF;
data[base+1] = (color >>  8) & 0xFF;
data[base+2] = (color >> 0) & 0xFF;
data[base+3] = 0xFF; // alpha
}
private:
vector<unsigned char> data;
void bound_check(unsigned x, unsigned y) const { assert(x < w && y < h); }
size_t raw_index(unsigned x, unsigned y) const { bound_check(x, y); return 4 * (y * w + x); }
};

// ============================================================================
// coordinates
// ============================================================================
typedef int16_t tCoord;

struct tPoint {
tCoord x, y;
tPoint operator+  (const tPoint & p) const { return { x + p.x, y + p.y }; }
};

typedef deque<tPoint> tPointList;

// ============================================================================
// command line and input parsing
// (in a nice airtight bag to contain the stench of C++ string handling)
// ============================================================================
enum tCommand {
c_quit,
c_update,
c_play,
};

class tParser {
public:
tRGB color;
tPointList points;

{
int r, g, b;
sscanf(s, "(%d,%d,%d)", &r, &g, &b);
return RGB(r, g, b);
}

tCommand command(void)
{
string line;
getline(cin, line);

string cmd = get_token(line);
points.clear();

if (cmd == "exit") return c_quit;
if (cmd == "pick") return c_play;

// even more convoluted and ugly than the LEFT$s and RIGHT$s of Apple ][ basic...
if (cmd != "colour")
{
cerr << "unknown command '" << cmd << "'\n";
exit(0);
}
assert(cmd == "colour");
get_token(line); // skip "chose"
while (line != "")
{
string coords = get_token(line);
int x = atoi(get_token(coords, ',').c_str());
int y = atoi(coords.c_str());
points.push_back({ x, y });
}
return c_update;
}

private:
// even more verbose and inefficient than setting up an ADA rendezvous...
string get_token(string& s, char delimiter = ' ')
{
size_t pos = 0;
string token;
if ((pos = s.find(delimiter)) != string::npos)
{
token = s.substr(0, pos);
s.erase(0, pos + 1);
}
}
};

// ============================================================================
// pathing
// ============================================================================
class tPather {

public:
tPather(tRawImage image, tRGB own_color)
: arena(image)
, w(image.w)
, h(image.h)
, own_color(own_color)
, enemy_threat(false)
{
// extract colored pixels and own color areas
tPointList own_pixels;
color_plane[neutral].resize(w*h, false);
color_plane[enemies].resize(w*h, false);
for (size_t x = 0; x != w; x++)
for (size_t y = 0; y != h; y++)
{
tRGB color = image.get_pixel(x, y);
if (color == col_white) continue;
plane_set(neutral, x, y);
if (color == own_color) own_pixels.push_back({ x, y }); // fill the frontier with all points of our color
}

// compute initial frontier
for (tPoint pixel : own_pixels)
for (tPoint n : neighbour)
{
tPoint pos = pixel + n;
if (!in_picture(pos)) continue;
if (image.get_pixel(pos.x, pos.y) == col_white)
{
frontier.push_back(pixel);
break;
}
}
}

tPointList search(size_t pixels_required)
{
// flood fill the arena, starting from our current frontier
tPointList result;
tPlane closed;
static tCandidate pool[max_size*max_size]; // fastest possible garbage collection
size_t alloc;
static tCandidate* border[max_size*max_size]; // a FIFO that beats a deque anytime
static vector<tDistance>distance(w*h); // distance map to be flooded
size_t filling_pixels = 0; // end of game  optimization

get_more_results:

// ready the distance map for filling
distance.assign(w*h, distance_max);

// seed our flood fill with the frontier
alloc = head = tail = 0;
for (tPoint pos : frontier)
{
border[tail++] = new (&pool[alloc++]) tCandidate (pos);
}

closed = color_plane[neutral]; // that's one huge copy

for (tPoint pos : result)
{
border[tail++] = new (&pool[alloc++]) tCandidate(pos);
closed[raw_index(pos)] = true;
}

// let's floooooood!!!!
while (tail > head && pixels_required > filling_pixels)
{
tDistance  dist = candidate.distance;
distance[raw_index(candidate.pos)] = dist++;
for (tPoint n : neighbour)
{
tPoint pos = candidate.pos + n;
if (!in_picture (pos)) continue;
size_t index = raw_index(pos);
if (closed[index]) continue;
if (color_plane[enemies][index])
{
if (dist == (distance_initial + 1)) continue; // already near an enemy pixel

// reached the nearest enemy pixel
static tPoint trail[max_size * max_size / 2]; // dimensioned as a 1 pixel wide spiral across the whole map
size_t trail_size = 0;

// walk back toward the frontier
tPoint walker = candidate.pos;
tDistance cur_d = dist;
while (cur_d > distance_initial)
{
trail[trail_size++] = walker;
tPoint next_n;
for (tPoint n : neighbour)
{
tPoint next = walker + n;
if (!in_picture(next)) continue;
tDistance prev_d = distance[raw_index(next)];
if (prev_d < cur_d)
{
cur_d = prev_d;
next_n = n;
}
}
walker = walker + next_n;
}

// collect our precious new pixels
if (trail_size > 0)
{
while (trail_size > 0)
{
if (pixels_required-- == 0) return result;       // ;!; <-- BRUTAL EXIT
tPoint pos = trail[--trail_size];
result.push_back (pos);
}
goto get_more_results; // I could have done a loop, but I did not bother to. Booooh!!!
}
continue;
}

// on to the next neighbour
closed[index] = true;
border[tail++] = new (&pool[alloc++]) tCandidate(pos, dist);
if (!enemy_threat) filling_pixels++;
}
}

// if all enemies have been surrounded, top up result with the first points of our flood fill
if (enemy_threat) enemy_threat = pixels_required == 0;
tPathIndex i = frontier.size() + result.size();
while (pixels_required--) result.push_back(pool[i++].pos);
return result;
}

// tidy up our map and frontier while other bots are thinking
void validate(tPointList moves)
{
// report new points
for (tPoint pos : moves)
{
frontier.push_back(pos);
color_plane[neutral][raw_index(pos)] = true;
}

// remove surrounded points from frontier
for (auto it = frontier.begin(); it != frontier.end();)
{
bool in_frontier = false;
for (tPoint n : neighbour)
{
tPoint pos = *it + n;
if (!in_picture(pos)) continue;
if (!(color_plane[neutral][raw_index(pos)] || color_plane[enemies][raw_index(pos)]))
{
in_frontier = true;
break;
}
}
if (!in_frontier) it = frontier.erase(it); else ++it; // the magic way of deleting an element without wrecking your iterator
}
}

void update(tRGB color, tPointList points)
{
assert(color != own_color);

// plot enemy moves
enemy_threat = true;
for (tPoint p : points) plane_set(enemies, p);

// important optimization here :
/*
* Stop 1 pixel away from the enemy to avoid wasting moves in dogfights.
* Better let the enemy gain a few more pixels inside the surrounded region
* and use our precious moves to get closer to the next threat.
*/
for (tPoint p : points) for (tPoint n : neighbour) plane_set(enemies, p+n);

// if a new enemy is detected, gather its initial pixels
for (tRGB enemy : known_enemies) if (color == enemy) return;
known_enemies.push_back(color);
tPointList start_areas = scan_color(color);
for (tPoint p : start_areas) plane_set(enemies, p);
}

private:
typedef uint16_t tPathIndex;

typedef uint16_t tDistance;
static const tDistance distance_max     = 0xFFFF;
static const tDistance distance_initial = 0;

struct tCandidate {
tPoint pos;
tDistance distance;
tCandidate(){} // must avoid doing anything in this constructor, or pathing will slow to a crawl
tCandidate(tPoint pos, tDistance distance = distance_initial) : pos(pos), distance(distance) {}
};

// neighbourhood of a pixel
static const tPoint neighbour[4];

// dimensions
tCoord w, h;
static const size_t max_size = 1000;

// colors lookup
const tRGB col_white = RGB(0xFF, 0xFF, 0xFF);
const tRGB col_black = RGB(0x00, 0x00, 0x00);
tRGB own_color;
const tRawImage arena;
tPointList scan_color(tRGB color)
{
tPointList res;
for (size_t x = 0; x != w; x++)
for (size_t y = 0; y != h; y++)
{
if (arena.get_pixel(x, y) == color) res.push_back({ x, y });
}
return res;
}

// color planes
typedef vector<bool> tPlane;
tPlane color_plane[2];
const size_t neutral = 0;
const size_t enemies = 1;
bool plane_get(size_t player, tPoint p) { return plane_get(player, p.x, p.y); }
bool plane_get(size_t player, size_t x, size_t y) { return in_picture(x, y) ? color_plane[player][raw_index(x, y)] : false; }
void plane_set(size_t player, tPoint p) { plane_set(player, p.x, p.y); }
void plane_set(size_t player, size_t x, size_t y) { if (in_picture(x, y)) color_plane[player][raw_index(x, y)] = true; }
bool in_picture(tPoint p) { return in_picture(p.x, p.y); }
bool in_picture(int x, int y) { return x >= 0 && x < w && y >= 0 && y < h; }
size_t raw_index(tPoint p) { return raw_index(p.x, p.y); }
size_t raw_index(size_t x, size_t y) { return y*w + x; }

// frontier
tPointList frontier;

// register enemies when they show up
vector<tRGB>known_enemies;

// end of game optimization
bool enemy_threat;
};

// small neighbourhood
const tPoint tPather::neighbour[4] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

// ============================================================================
// main class
// ============================================================================
class tGame {
public:
tGame(tRawImage image, tRGB color, size_t num_pixels)
: own_color(color)
, response_len(num_pixels)
, pather(image, color)
{}

void main_loop(void)
{
// grab an initial answer in case we're playing first
tPointList moves = pather.search(response_len);
for (;;)
{
size_t        num_points;
tPointList    played;

switch (parser.command())
{
case c_quit:
return;

case c_play:
// play as many pixels as possible
if (moves.size() < response_len) moves = pather.search(response_len);
num_points = min(moves.size(), response_len);
for (size_t i = 0; i != num_points; i++)
{
answer << moves[0].x << ',' << moves[0].y;
if (i != num_points - 1) answer << ' '; // STL had more important things to do these last 30 years than implement an implode/explode feature, but you can write your own custom version with exception safety and in-place construction. It's a bit of work, but thanks to C++ inherent genericity you will be able to extend it to giraffes and hippos with a very manageable amount of code refactoring. It's not anyone's language, your C++, eh. Just try to implode hippos in Python. Hah!
played.push_back(moves[0]);
moves.pop_front();
}

// now that we managed to print a list of points to stdout, we just need to cleanup the mess
pather.validate(played);
break;

case c_update:
if (parser.color == own_color) continue; // hopefully we kept track of these already
pather.update(parser.color, parser.points);
moves = pather.search(response_len); // get cracking
break;
}
}
}

private:
tParser parser;
tRGB    own_color;
size_t  response_len;
tPather pather;
};

void main(int argc, char * argv[])
{
// process command line
int num_pixels               = atoi (argv[3]);

// init and run
tGame game (raw_image, my_color, num_pixels);
game.main_loop();
}


## Building the executable

I used LODEpng.cpp and LODEpng.h to read png images.
About the easiest way I found to teach this retarded C++ language how to read a picture without having to build half a dozen libraries.
Just compile & link LODEpng.cpp along with the main and Bob's your uncle.

I compiled with MSVC2013, but since I used only a few STL basic containers (deque and vectors), it might work with gcc (if you're lucky).
If it doesn't, I might try a MinGW build, but frankly I'm getting tired of C++ portability issues.

I did quite a lot of portable C/C++ in my days (on exotic compilers for various 8 to 32 bits processors as well as SunOS, Windows from 3.11 up to Vista and Linux from its infancy to Ubuntu cooing zebra or whatever, so I think I have a pretty good idea of what portability means), but at the time it did not require to memorize (or discover) the innumerable discrepancies between the GNU and Microsoft interpretations of the cryptic and bloated specs of the STL monster.

## How it works

At the core, this is a simple brute-force floodfill pathing.

The frontier of the player's color (i.e. the pixels that have at least one white neighbour) is used as a seed to perform the classic distance-flooding algorithm.

When a point reaches the vincinity of an enemy color, a backward path is computed to produce a string of pixels moving toward the nearest enemy spot.

The process is repeated until enough points have been gathered for a response of the desired length.

This repetition is obscenely expensive, especially when fighting near the enemy.
Each time a string of pixels leading from the frontier to an enemy pixel has been found (and we need more points to complete the answer), the flood fill is redone from start, with the new path added to the frontier. It means you could have to do 5 flood fills or more to get an 10 pixels answer.

If no more enemy pixels are reachable, arbitraty neighbours of the frontier pixels are selected.
The algorithm devolves to a rather inefficient flood fill, but this only happens after the outcome of the game has been decided (i.e. there is no more neutral territory to fight for).
I did optimize it so that the Judge do not spend ages filling up the map once the competition has been dealt with. In its current state, the execution time is neglectible compared with the Judge itself.

Since enemy colors are not known at start, the initial arena image is kept in store in order to copy the enemy's starting areas when it makes its first move.
If the code plays first, it will simply flood fill a few arbitrary pixels.

This makes the algorithm capable of fighting an arbitrary number of adversaries, and even possibly new adversaries arriving at a random point in time, or colors appearing without a starting area (though this has absolutely no practical use).

Enemy handling on a color-per-color basis would also allow to have two instances of the bot cooperate (using pixel coordinates to pass a secret recognition sign).
Sounds like fun, I'll probably try that :).

Computation-heavy pathing is done as soon as new data are available (after a move notification), and some optimizations (the frontier update) are done just after a response has been given (to do as much computation as possible during the other bots turns).

Here again, there could be ways of doing more subtle things if there were more than 1 adversary (like aborting a computation if new data become available), but at any rate I fail to see where multitasking is needed, as long as the algorithm is able to work on full load.

## Performance issues

All this cannot work without fast data access (and more computing power than the whole Appolo program, i.e. your average PC when used to do more than post a few tweets).

The speed is heavily compiler-dependent. Usually GNU beats Microsoft by a 30% margin (that's the magic number I noticed on 3 other pathing-related code challenges), but this mileage may vary of course.

The code in its current state barely breaks a sweat on arena 4. Windows perfmeter reports about 4 to 7 % CPU usage, so it should be able to cope with a 1000x1000 map within the 100ms response time limit.

At the heart of about every pathing algorithm lies a FIFO (possibly proritized, though not in that case), which in turn requires fast element allocation.

Since the OP obligingly set a limit to the arena size, I did some maths and saw that fixed data structures dimensioned to max (i.e. 1.000.000 pixels) would not consume more than a couple dozen megabytes, which your average PC eats for breakfast.
Indeed under Win7 and compiled with MSVC 2013, the code consumes about 14Mb on arena 4, while Swallower's two threads are using more than 20Mb.

I started with STL containers for easier prototyping, but STL made the code even less readable, since I had no desire to create a class to encapsulate each and every bit of data to hide the obfuscation away (whether that is due to my own inabilities to cope with the STL is left to the appreciation of the reader).
Regardless, the result was so atrociously slow that at first I thought I was building a debug version by mistake.

I reckon this is partly due to the incredibly poor Microsoft implementation of the STL (where, for instance, vectors and bitsets do bound checks or other crypic operations on operator[], in direct violation of the spec), and partly to the STL design itself.

I could cope with the atrocious syntax and portability (i.e. Microsoft vs GNU) issues if the performances were there, but this is certainly not the case.

For instance, deque is inherently slow, because it shuffles lots of bookkeeping data around waiting for the occasion to do its super smart resizing, about which I could not care less.
Sure I could have implemented a custom allocator and whaterver other custom template bits, but a custom allocator alone costs a few hundred lines of code and the better part of a day to test, what with the dozen of interfaces it has to implement, while a handmade equivalent structure is about zero lines of code (albeit more dangerous, but the algorithm would not have worked if I did not know - or think I knew - what I was doing anyway).

So eventually I kept the STL containers in non-critical parts of the code, and built my own brutal allocator and FIFO with two circa 1970 arrays and three unsigned shorts.

## Swallowing the swallower

As its author confirmed, the erratic patterns of the Swallower are caused by lag between enemy moves notifications and updates from the pathing thread.
The system perfmeter shows clearly the pathing thread consuming 100% CPU all the time, and the jagged patterns tend to appear when the focus of the fight shifts to a new area. This is also quite apparent with the animations.

## A simple but effective optimization

After looking at the epic dogfights between Swallower and my fighter, I remembered an old saying from the game of Go: defend close up, but attack from afar.

There is wisdom in that. If you try to stick to your adversary too much, you will waste precious moves trying to block each possible path. On the contrary, if you stay just one pixel away, you will likely avoid filling small gaps that would gain very little, and use your moves to counter more important threats.

To implement this idea, I simply extended the moves of an enemy (marking the 4 neighbours of each move as an enemy pixel).
This stops the pathing algorithm one pixel away from the enemy's border, allowing my fighter to work its way around an adversary without getting caught in too many dogfights.

You can see the improvement
(though all runs are not as successful, you can notice the much smoother outlines):

• Wow. I thought nothing was going to beat the Swallower. Excellent solution with great description. I remember K&R C from the good old days, but then C went to the dark side. I think you will like Python. Jan 30, 2015 at 10:11
• It was a real pleasure to tackle a challenge so... well.. challenging and fun. This allowed me to test this little gem of LODEpng full scale, and the results are so promising that I might revisit the png racer, testing yet again my love/hate relationship with this infamous post-incremented C.
– user16991
Jan 30, 2015 at 10:19
• The swallower is a little erratic at times in order to keep within the time limit. This is partially what the multi-threading is for. Good job!! I think I'll double my bonus... Jan 30, 2015 at 13:15
• Pillow has downloads for 64-bits. It can be used just like PIL. Jan 30, 2015 at 16:27
• @TheBestOne I thought so. My brutal painter takes advantage of these moments where your swallower munches outdated data :). As for PIL, I did download about all Amd64 PIL and Pillow versions available on the World Wide Web, but they would not work with my core 63.5 bits Python, which was probably a bootleg and/or outdated version. Anyway, Win32 port works just as well, and if one day I need something faster I'll have to switch to PyPy all the same.
– user16991
Jan 30, 2015 at 17:10

# Random, Language = java, Score = 0.43012126100275

This program randomly puts pixels on the screen. Some (if not all) of the pixels will not be valid. On a side note, it should be difficult to make a faster program than this one.

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class Random {

static int n;

static int height;

static int width;

public static void main(String[] args) throws Exception{
height = image.getHeight();
width = image.getWidth();
n = Integer.parseInt(args[2]);
while (true){
if (input.equals("exit")){
return;
}
if (!input.equals("pick pixels")){
continue;
}
for (int i = 0; i < n; i++){
System.out.print((int) (Math.random() * width) + ",");
System.out.print((int) (Math.random() * height) + " ");
}
System.out.println();
}
}
}


Arena 1:

Arena 2:

Arena 3:

Arena 4:

• I see you have not fallen into the trap of premature optimisation. Dec 31, 2014 at 1:42