Skip to content

Instantly share code, notes, and snippets.

@angch
Created August 19, 2015 17:48
Show Gist options
  • Save angch/6a095f873adabc821d07 to your computer and use it in GitHub Desktop.
Save angch/6a095f873adabc821d07 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strconv"
)
var debug = false
func doTower(input io.Reader) {
re, _ := regexp.Compile("(?i)[0-9]+")
scanner := bufio.NewScanner(input) // was os.Stdin
scanner.Scan()
line := scanner.Text()
words := re.FindAllStringSubmatch(line, -1)
width, _ := strconv.Atoi(words[0][0])
distance, _ := strconv.Atoi(words[1][0])
region := make([][]int, width)
for i := 0; i < width; i++ {
scanner.Scan()
line := scanner.Text()
region[i] = make([]int, width)
words := re.FindAllStringSubmatch(line, -1)
for k, word := range words {
region[i][k], _ = strconv.Atoi(word[0])
}
}
best := 0
for i := 0; i < width; i++ {
for j := 0; j < width; j++ {
currentBest := 0
for ii := i - distance; ii <= i+distance; ii++ {
if ii < 0 {
continue
}
if ii >= width {
break
}
for jj := j - distance; jj <= j+distance; jj++ {
if jj < 0 {
continue
}
if jj >= width {
break
}
currentBest += region[ii][jj]
}
}
if currentBest > best {
best = currentBest
if debug {
fmt.Println("best at ", i, j)
}
}
}
}
fmt.Println(best)
}
func main() {
input := bytes.NewBufferString(`4 1
0 1 2 0
0 0 0 0
0 0 0 0
1 0 0 0`)
doTower(input)
if debug {
input := bytes.NewBufferString(`5 1
0 1 2 0 1
0 0 0 0 10
0 0 0 0 1
1 0 0 0 1
1 1 1 1 1`)
doTower(input)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment