Skip to content

Instantly share code, notes, and snippets.

@itarato
Created July 27, 2015 08:22
Show Gist options
  • Save itarato/ce26002cec5b69fa46d6 to your computer and use it in GitHub Desktop.
Save itarato/ce26002cec5b69fa46d6 to your computer and use it in GitHub Desktop.
Challenge 224 hard - Langford strings
package main
import (
"log"
)
var (
n int = 8
l []int = make([]int, n*2)
used map[int]bool = make(map[int]bool)
)
func fill(pos int) {
if pos >= n*2 {
log.Println("Victory!", l)
return
} else if l[pos] != 0 { // Has a number at this position.
fill(pos + 1)
} else {
for i := 1; i <= n; i++ {
is_used, ok := used[i]
if ok && is_used { // This number was used already.
continue
}
second_pos := pos + i + 1
if second_pos >= n*2 || l[second_pos] != 0 { // The right part would not fit.
continue
}
// Place number.
l[pos] = i
l[pos+i+1] = i
used[i] = true
fill(pos + 1)
// Remove number
l[pos] = 0
l[pos+i+1] = 0
used[i] = false
}
}
}
func main() {
fill(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment