Skip to content

Instantly share code, notes, and snippets.

@permil
Created May 20, 2014 14:24
Show Gist options
  • Save permil/79b3ce62dc626578c2b5 to your computer and use it in GitHub Desktop.
Save permil/79b3ce62dc626578c2b5 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import re
def find_greedy(plans):
if len(plans)==0: return []
plans.sort(key=lambda x: x[3]) # 終了時間でソート
next_plan = plans[0] # (残った中で)一番早く終わるチケットを選択
result = [next_plan]
result.extend(find_greedy(filter(lambda x: x[2] > next_plan[3], plans)))
return result
# initialize
plans = []
p = re.compile('([a-zA-Z]+) ([0-9]+)/([0-9]+)-([0-9]+)/([0-9]+)')
for i,line in enumerate(open('tickets.txt')):
m = p.match(line)
plans.append((i, m.group(1), int(m.group(2))*100+int(m.group(3)), int(m.group(4))*100+int(m.group(5)))) # ソートしやすいように日付を数値化
# solve
ans = find_greedy(plans)
ans.sort(key=lambda x:x[0])
# output
out = str(len(ans))
for a in ans:
out += ' ' + a[1]
open('answer.txt', 'w').write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment