Skip to content

Instantly share code, notes, and snippets.

@jayvdb
Last active June 16, 2016 08:30
Show Gist options
  • Save jayvdb/56c7bd34828a1f54954e08bede45b555 to your computer and use it in GitHub Desktop.
Save jayvdb/56c7bd34828a1f54954e08bede45b555 to your computer and use it in GitHub Desktop.
Pocket Euler hacks
import sys
def load_answers(answer_filename):
answers = {}
lines = open(answer_filename).readlines()
for line in lines:
problem, answer = line.strip().split(': ')
answers[int(problem)] = answer
return answers
def add_answers(markdown_filename, answers):
lines = open(markdown_filename).readlines()
new_lines = []
for line in lines:
if line.startswith('# Soal '):
problem = line[7:].strip()
problem = int(problem)
if problem - 1 in answers:
new_lines.append('Answer: %s\n\n' % answers[problem - 1])
new_lines.append(line)
new_lines.append('Answer: %s\n' % answers[480])
with open(markdown_filename, 'w') as f:
for line in new_lines:
f.write(line)
if __name__ == '__main__':
answers = load_answers(sys.argv[1])
add_answers(sys.argv[2], answers)
grep -E '^(# Problem|\s*Answer:)' README.md | sed -e 's/^#*\s*//g' | awk 'BEGIN {print "\n"} /Problem/ {printf("\n%s", $2)} /Answer/{ print(":", $2) }' | grep -v '^$' > files/answers.txt
for file in _posts/problems/* ; do cat $file ; done > all.md
sed -i '/^layout:\s*problem/d' all.md
sed -i '/^category:\s*problems/d' all.md
sed -i '/^difficulty:\s*[0-9]/d' all.md
sed -i '/^title:\s*/d' all.md
sed -i '/^answer:\s*[0-9]*/d' all.md
sed -Ei "/dir: '{0,1}rtl'{0,1}/d" all.md
sed -i 's/^----*\s*$//' all.md
sed -i 's/^problemId:\s*\([0-9]*\)\s*$/# Soal \1/' all.md
cp all.md all.md.p2
sed -Ei 's/<img src="images\/(spacer|blackdot).gif"[^>]*>//g;/<img .*src/{s/style="[^"]*"//g;s/<img *src=.{0,1}\{\{ site.baseurl \}\}project\/images\/([^"'\'' >]*)[^>]*>/![](files-v1\/\1)/g;}' all.md.p2
sed -Ei 's/\(\{\{ site.baseurl \}\}project\/(images|resources)\/([^)]*)\)/(files-v1\/\2)/g' all.md.p2
sed -i 's/<a href="problem=\([0-9]*\)">Problem \([0-9]*\)<\/a>/<a href="#soal-\1">Soal \2<\/a>/g' all.md.p2
sed -i 's/<a href="problem=\([0-9]*\)">Problem \([0-9]*\)<\/a>/<a href="#soal-\1">Soal \2<\/a>/g' all.md.p2
remark all.md.p2 > all.md.new
diff -u all.md all.md.new | less
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment