Last active
December 17, 2015 16:09
-
-
Save kaakaa/5637229 to your computer and use it in GitHub Desktop.
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
def file = new File('/Users/kaakaa_hoe/Documents/codeIQ/shufflers/shufflers.txt') | |
Set resultList = new HashSet() | |
def cand = [:] | |
file.splitEachLine(' ') { array -> | |
def idList = getRegsList(array, /[0-9]+/) | |
def nameList = getRegsList(array, /[a-z]+/) | |
if(cand.size() == 0){ | |
cand.putAll([ nameList, [idList] ].combinations().collect{ it as MapEntry }) | |
} else { | |
nameList.each{ name -> | |
if(cand.containsKey(name)){ | |
cand[name] = cand[name].intersect(idList) | |
} else { | |
cand[name] = idList | |
} | |
} | |
} | |
} | |
def i = 0 | |
while (!extractResult(cand, resultList) && i < 100 ) { i++ } | |
def extractResult(cand, resultList) { | |
def obvious = cand.findAll { name, id -> id.size == 1 } | |
obvious.each { name, ids -> | |
cand.remove(name) | |
cand.each { cand_name, cand_ids -> | |
cand[cand_name] -= ids | |
} | |
} | |
resultList.addAll(obvious.collect{ name, id -> "${id.join(' ')} = $name" }) | |
return obvious.size() == 0 | |
} | |
def getRegsList(array, regs) { | |
return array.findAll { it ==~ regs } | |
} | |
resultList.addAll(cand.collect{ name, ids -> "${ids.join(' ')} = $name" }) | |
def footer = """ | |
使用言語 | |
- Groovy 2.1.3 | |
開発環境 | |
- GroovyConsole | |
所要時間 | |
- 2時間強 | |
感想 | |
- 確定した答えを候補からどのように削除していくのかを考えすぎてハマりました。 | |
""" | |
def outputFile = new File('/Users/kaakaa_hoe/Documents/codeIQ/shufflers/answer.txt') | |
outputFile.withWriter { out -> | |
resultList.sort().each { out.println it } | |
out.println footer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment