Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My solution to the alphabet code golf challenge.
match = [
'aerate aerate arrest errant serene tanner testes'
'aerate assent assent assert rester retest tenant'
'aerate assert rearer renter resent serene teaser'
'aerate easter easter tenant tester testes tsetse'
'arrest arrest easter entree errant resent senate'
'assent assess assets estate resent staree teaser'
'assert astern renter rerent resent staree street'
'assert enseat entree errata rennet teaser tsetse'
'assert rennet renter reseat rester serene tenant'
'assess easter estate rennet rennet tenant testes'
'assess easter estate rerent resent retest snarer'
'assess renter renter searer seater snarer testes'
'astern enseat entree serene staree tartar tartar'
'astern rennet retest searer snarer tartar tester'
'enseat errata seater senate strata teaser tsetse'
'entree searer staree taster taster tenant testes'
'rerent rester tanner tartar teaser teaser testes'
]
nomatch = [
'aerate astern assess enseat senate street tsetse'
'aerate rennet errant enseat rerent senate testes'
'arrest assess assess assent astern searer testes'
'assert assess errata enseat earner seater serene'
'assert astern staree senate snarer tanner tester'
'assert strata rerent rerent tanner testes tsetse'
'assess easter entree rester reseat seater tartar'
'astern assets rearer rearer assess rearer testes'
'astern easter taster serene reseat taster tester'
'earner entree rerent reseat teaser strata staree'
'earner errant estate taster reseat estate taster'
'enseat astern arrest enseat searer seater tenant'
'errant errant senate renter rearer street tsetse'
'rennet rennet assent errant rester staree tester'
'rennet snarer senate retest tanner tartar tsetse'
'retest astern arrest tsetse strata senate tsetse'
'searer errant teaser staree assess teaser tsetse'
]
alphabet = [
''
't'
'e'
'n'
's'
'a'
'r'
' '
]
lenmax = {
"1":{max: 0, rex: ''}
"2":{max: 0, rex: ''}
"3":{max: 0, rex: ''}
"4":{max: 0, rex: ''}
"5":{max: 0, rex: ''}
"6":{max: 0, rex: ''}
"7":{max: 0, rex: ''}
"8":{max: 0, rex: ''}
"9":{max: 0, rex: ''}
"10":{max: 0, rex: ''}
"11":{max: 0, rex: ''}
"12":{max: 0, rex: ''}
"13":{max: 0, rex: ''}
"14":{max: 0, rex: ''}
"15":{max: 0, rex: ''}
"16":{max: 0, rex: ''}
"17":{max: 0, rex: ''}
"18":{max: 0, rex: ''}
"19":{max: 0, rex: ''}
}
count = 0
costmin = 12
for c1 in alphabet
for c2 in alphabet
for c3 in alphabet
for i in [1..48]
regs = []
regs.push new RegExp c1 + c2 + c3 + ".{" + i + "}"
regs.push new RegExp c1 + c2 + c3 + ".{," + i + "}"
regs.push new RegExp c1 + c2 + c3 + ".{" + i + ",}"
regs.push new RegExp c1 + c2 + ".{" + i + "}" + c3
regs.push new RegExp c1 + c2 + ".{," + i + "}" + c3
regs.push new RegExp c1 + c2 + ".{" + i + ",}" + c3
regs.push new RegExp c1 + ".{" + i + "}" + c2 + c3
regs.push new RegExp c1 + ".{" + i + ",}" + c2 + c3
regs.push new RegExp c1 + ".{," + i + "}" + c2 + c3
regs.push new RegExp ".{" + i + "}" + c1 + c2 + c3
regs.push new RegExp ".{," + i + "}" + c1 + c2 + c3
regs.push new RegExp ".{" + i + ",}" + c1 + c2 + c3
for j in [1..48]
regs.push new RegExp c1 + ".{" + i + "}" + c2 + ".{" + j + "}" + c3
regs.push new RegExp c1 + ".{" + i + "}" + c2 + ".{," + j + "}" + c3
regs.push new RegExp c1 + ".{," + i + "}" + c2 + ".{," + j + "}" + c3
regs.push new RegExp c1 + ".{" + i + ",}" + c2 + ".{," + j + "}" + c3
regs.push new RegExp c1 + ".{," + i + "}" + c2 + ".{" + j + "}" + c3
regs.push new RegExp c1 + ".{," + i + "}" + c2 + ".{" + j + ",}" + c3
regs.push new RegExp c1 + ".{" + i + ",}" + c2 + ".{" + j + "}" + c3
regs.push new RegExp c1 + ".{" + i + ",}" + c2 + ".{" + j + ",}" + c3
regs.push new RegExp c1 + ".{" + i + ",}" + c2 + ".{," + j + "}" + c3
regs.push new RegExp c1 + ".{" + i + "}" + c2 + ".{" + j + ",}" + c3
regs.push new RegExp c1 + ".{," + i + "}" + c2 + ".{" + j + ",}" + c3
for rx in regs # when rx.source.length < 6
clean = true
for n in nomatch
if rx.test(n)
clean = false
break
if clean
matches = 0
for m in match
if rx.test(m)
matches++
if(matches >= lenmax[rx.source.length.toString()].max)
local_cost = (rx.source.length + 1) / matches
if local_cost <= 1.5
console.log rx, local_cost
if local_cost <= costmin
console.log rx, local_cost
costmin = local_cost
lenmax[rx.source.length.toString()] = {max: matches, rex: rx, cost: local_cost }
console.log lenmax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.