Created
September 12, 2011 08:35
-
-
Save mohayonao/1210841 to your computer and use it in GitHub Desktop.
gdd2011 devQuiz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt"; | |
"io"; | |
"strings"; | |
/* and more */ | |
imagepng "image/png"; | |
) | |
func CountColor(png io.Reader) int { | |
/* modify here */ | |
i, _ := imagepng.Decode(png); | |
rect, colormap := i.Bounds(), map[uint32]int {}; | |
for x:= rect.Min.X; x < rect.Max.X; x += 1 { | |
for y:= rect.Min.Y; y < rect.Max.Y; y += 1 { | |
r, g, b, _ := i.At(x, y).RGBA(); | |
color := r<<16 + g<<8 + b; | |
colormap[color] = 1; | |
} | |
} | |
count := 0; | |
for _, value := range colormap { | |
count += value; | |
} | |
return count; | |
} | |
func main() { | |
png := GetPngBinary(); | |
cnt := CountColor(png); | |
fmt.Println(cnt); | |
} | |
func GetPngBinary() io.Reader { | |
img_str := "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x18\x00\x00\x00\x08\x08\x06\x00\x00\x00\xe3\xa1?c\x00\x00\x02\xeeiCCPICC Profile\x00\x00x\x01\x85T\xcfk\x13A\x14\xfe6n\xa9\xd0\"\x08Zk\x0e\xb2x\x90\"IY\xabhE\xd46\xfd\x11bk\x0c\xdb\x1f\xb6E\x90d3I\xd6n6\xeb\xee&\xb5\xa5\x88\xe4\xe2\xd1*\xdeE\xed\xa1\x07\xff\x80\x1ez\xf0d/J\x85ZE(\xde\xab(b\xa1\x17-\xf1\xcdnL\xb6\xa5\xea\xc0\xce~\xf3\xde7\xef}ov\xdf\x00\rr\xd24\xf5\x80\x04\xe4\r\xc7R\xa2\x11il|Bj\xfc\x88\x00\x8e\xa2\tA4%U\xdb\xecN$\x06A\x83s\xf9{\xe7\xd8z\x0f\x81[V\xc3{\xfbw\xb2w\xad\x9a\xd2\xb6\x9a\x07\x84\xfd@\xe0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\x0b\xe2`m\xc7\x08\x9d\x95\x86)\xd2m\x91\xfa$\xd5``\x9a\xbc\xf5/]?[x\xbdF\x7f\x0c\xf5Q\x94\x19\xcc\xd2T\x89\xf7\x7f\xc2*d4\x9d\xb9\x0eo\xfa\x8f\xdb\xc7\xfc\x17\xe4\xf7\x8a\xe7\x9f(\x02/l\xe0\xc8\x99\xbamSq\xef\x10\xa1e\xa5ns\xae\x02\x17\xbf\xd1}\xf0\xb6nk\xa3~8\xfc\x04X<\xab\x16\xadR5\x9f \xbc\x01\x1cv\x87z\x1e\xe8)\x98\xd3\x96\x96\xcd9R\x87,\x9f\x93\xba\xe9\xcabR\xccP\xdbCRR\xd7%\xd7eK\x16\xb3\x99Ub\xe9v\xd8\x99\xd3\x1dn\x1c\xa19B\xf7\xc4\xa7Je\x93\xfa\xaf\xf1\x11\xb0\xfd\xb0R\xf9\xf9\xacR\xd9~N\x1a\xd6\x81\x97\xfao\xc0\xbc\xfdE\xc0x\x8b\x89\x00\x00\x00\x97IDAT(\x15\x9d\x92\x81\x0e\x80 \x08D\xa5\xf9m\xf5Y\xad\xcf\xaa\x9f#nz$\xba\xb9\xca\xad\x85\xc1\xbd\x03S\xd4\x96H\xf2\xa5\xea\xe1\xeb@\x8e\x02\xd0}\x14/\x80\x03\xca\xa75{\xeb0\x80\x01\xa9\xa0\xdcC|\x02:\xf1\xc3U\xc7\\K\x97}\xda9HPcq0pQ\x8aE\xe94y\x05'3\x92M[\x86\xc7\xc1\xa4n\x82\x01\x8ci\xe2\xc5\x7f\x02N`\xda\xdcCK\xaeqbqsD\xbd\x86=\xe0g\xdb\x9dy\xba\xb4Xp\x8bX\xf0\xe7\x8d\x89g\x84pD_\x0cx\x9438x7\xa4yC\r7\x04z\xae\x00\x00\x00\x00IEND\xaeB`\x82"; | |
return strings.NewReader(img_str) | |
} | |
// 8g devquiz.go | |
// 8l devquiz.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import datetime, time | |
import optparse | |
import collections | |
Action = collections.namedtuple('Action', 'cond calc') | |
Status = collections.namedtuple('Status', 'board commands lv') | |
def now(): | |
return time.mktime(datetime.datetime.now().timetuple()) | |
class Puzzle: | |
def __init__(self, id, w=0, h=0, b=''): | |
self.id = id | |
self.w = w | |
self.h = h | |
self.b = b | |
self.g = self._makeGoal(b) | |
self.status = 0 | |
self.answer = '' | |
def _makeGoal(self, b): | |
return ''.join(['123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_'[i] | |
if b[i]!='=' else '=' | |
for i in xrange(len(b))][:-1] + ['0']) | |
class PuzzlePool: | |
ACTIONS = {'L':Action(lambda i, w, h: (i%w) != 0 , lambda i, w, h, v=1: i-1*v), | |
'R':Action(lambda i, w, h: (i%w) != w-1, lambda i, w, h, v=1: i+1*v), | |
'U':Action(lambda i, w, h: (i/w) != 0 , lambda i, w, h, v=1: i-w*v), | |
'D':Action(lambda i, w, h: (i/w) != h-1, lambda i, w, h, v=1: i+w*v) } | |
def __init__(self, opts): | |
self._readProblem(opts.problem) | |
self._readAnswer(opts.answer) | |
self._readSkip(opts.skip) | |
self._opts = opts | |
self.isModified = False | |
def _readProblem(self, filepath): | |
lines = [ i.strip() for i in open(filepath).readlines() ] | |
self.lx, self.rx, self.ux, self.dx = [ int(x) for x in lines[0].split(' ') ] | |
self.totalCount = int(lines[1]) | |
self.puzzle = [] | |
for i, l in enumerate(lines[2:]): | |
w, h, b = l.split(',') | |
w, h = int(w), int(h) | |
self.puzzle.append(Puzzle(i, w, h, b)) | |
def _readAnswer(self, filepath): | |
if os.path.exists(filepath): | |
lines = [ i.strip() for i in open(filepath).readlines() ] | |
for i, l in enumerate(lines): | |
self.puzzle[i].answer = l | |
def _readSkip(self, filepath): | |
ids = [] | |
if os.path.exists(filepath): | |
lines = [ i.strip() for i in open(filepath).readlines() ] | |
ids = [ int(l) for l in lines if l.isdigit() ] | |
self.skipIds = set(ids) | |
def check(self): | |
opts = self._opts | |
score = 0.00 | |
for p in self.puzzle: | |
if not p.answer: continue | |
b, g = p.b, p.g | |
for cmd in p.answer: | |
a = self.ACTIONS.get(cmd) | |
if a is not None: | |
i1 = b.index('0') | |
i2 = a.calc(i1, p.w, p.h) | |
try: | |
b = swap(b, i1, i2) | |
except IndexError: b = None | |
else: b = None | |
if b is None: break | |
self.lx -= b.count('L') | |
self.rx -= b.count('R') | |
self.ux -= b.count('U') | |
self.dx -= b.count('D') | |
if b == g: | |
score += 0.01 | |
print '\x1b[1;32m%4d >> correct (score=%.2f)\x1b[0m' % (p.id, score) | |
else: | |
print p.b, p.answer, g | |
print '\x1b[1;31m%4d >> wrong?? (score=%.2f)\x1b[0m' % (p.id, score) | |
else: | |
if self.lx < 0 or self.rx < 0 or self.ux < 0 or self.dx < 0: | |
score = 0.00 | |
print 'check done: score=%.2f' % (score) | |
print ' rest: lx=%d, rx=%d, ux=%d, dx=%d' % (self.lx, self.rx, self.ux, self.dx) | |
def depth(self, lv): | |
opts = self._opts | |
limit = opts.limit | |
def depthSearch(p): | |
X = self._calcDistance(p) | |
Q = [ Status(p.b, [], X) ] | |
checked = {p.b:''} | |
begin = now() | |
while Q: | |
st = Q.pop(0) | |
b1 = st.board | |
i1 = b1.index('0') | |
for cmd, action in self.ACTIONS.iteritems(): | |
if not action.cond(i1, p.w, p.h): continue | |
i2 = action.calc(i1, p.w, p.h) | |
if p.g[i2] == '=': continue | |
try: | |
b2 = swap(b1, i1, i2) | |
except IndexError: continue | |
if b2 in checked: | |
# ?? | |
continue | |
checked[b2] = ''.join(st.commands) | |
new_cmd = st.commands+[cmd] | |
new_X = len(new_cmd) + 2 * self._calcDistance(p, board=b2) | |
new_st = Status(b2, new_cmd, new_X) | |
if b2 == p.g: | |
return ''.join(new_st.commands) | |
if new_X < lv: | |
Q.append(new_st) | |
Q.sort(key=lambda s: s[2]) | |
Q = Q[:1000] | |
sec = now() - begin | |
if limit and limit < sec: | |
break | |
remain = [ p.id for p in self.puzzle if not (p.answer or p.id in self.skipIds) ] | |
remain.sort(key=lambda id: | |
len(self.puzzle[id].b) + self._calcDistance(self.puzzle[id]) * 50) | |
self.isModified = True | |
for id in remain: | |
p = self.puzzle[id] | |
if id != p.id: continue | |
path = depthSearch(p) | |
if path: | |
p.answer = path | |
else: | |
self.skipIds.add(p.id) | |
self._printResult(p) | |
def _calcDistance(self, p, board=None): | |
result = 0 | |
b, g, w = p.b, p.g, p.w | |
if board: b = board | |
for c in b: | |
if c == '=' or c =='0': | |
continue | |
ib, ig = b.index(c), g.index(c) | |
xb, yb = ib%w, ib/w | |
xg, yg = ig%w, ig/w | |
result += abs(xb-xg) + abs(yb-yg) | |
return result | |
def _printResult(self, p): | |
now = datetime.datetime.now().strftime('%H:%M:%S') | |
if p.answer: | |
color, st, ans = '\x1b[1;32m', 'solve!', p.answer | |
else: | |
color, st, ans = '\x1b[1;31m', 'skip..', '???' | |
print color, | |
print '%s id:%4d %s %s: %s' % (now, p.id, st, p.b, ans), | |
print '\x1b[0m' | |
def save(self): | |
opts = self._opts | |
f = open(opts.answer, 'w') | |
f.writelines('%s\n' % p.answer for p in self.puzzle) | |
f.close() | |
f = open(opts.skip, 'w') | |
f.writelines('%s\n' % id for id in self.skipIds) | |
f.close() | |
print 'save done' | |
def swap(src, a, b): | |
l = list(src) | |
l[a], l[b] = l[b], l[a] | |
return ''.join(l) | |
def main(): | |
parser = optparse.OptionParser(usage='%prog [Options]') | |
parser.add_option('-p', '--problem', default='problem.txt') | |
parser.add_option('-a', '--answer', default='answer.txt') | |
parser.add_option('-s', '--skip', default='skip.txt') | |
parser.add_option('-l', '--limit', type='int', default=0) | |
parser.add_option('-m', '--mode', default='breadth') | |
(opts, args) = parser.parse_args() | |
p = PuzzlePool(opts) | |
try: | |
if opts.mode == 'check': | |
p.check() | |
else opts.mode.startswith('depth'): | |
lv = opts.mode[5:] | |
lv = int(lv) if lv.isdigit() else 500 | |
p.depth(lv) | |
except KeyboardInterrupt: | |
pass | |
if p.isModified: | |
p.save() | |
p.check() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:void((function() { | |
var match, lst, i, imax, dict, tid; | |
match = /conc\.setup\(\[(.+?)\]\)/.exec($(document).text()); | |
if (!match) return; | |
lst = match[1].split(','); | |
dict = {}; | |
for (i = 0, imax = lst.length; i < imax; i++) { | |
if (lst[i] in dict) { | |
dict[lst[i]].push(i); | |
} else { | |
dict[lst[i]] = [i]; | |
} | |
} | |
lst = []; | |
for (i in dict) { | |
lst.push(dict[i][0]); | |
lst.push(dict[i][1]); | |
} | |
i = 0; | |
function opencard() { | |
$('%23card' + lst[i]).click(); | |
i += 1; | |
if (i >= imax) { | |
clearInterval(tid); | |
} | |
} | |
tid = setInterval(opencard, 10); | |
}())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment