-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdetails.go
74 lines (63 loc) · 1.36 KB
/
details.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import "fmt"
import "log"
import "bufio"
import "os"
import "strings"
var DEBUG = false
type Detail struct {
lines []string
size int
}
// Remove the top element from the stack and return it's value
// If the stack is empty, return nil
func (d *Detail) Init(lines string) {
splitLines := strings.Split(lines, ",")
d.size = len(splitLines[0])
d.lines = splitLines
if DEBUG {
fmt.Println(lines)
}
}
func (d *Detail) CheckEndsAreCovered() {
for _, line := range d.lines {
if line[0] != 'X' || line[len(line)-1] != 'Y' {
fmt.Println("Uncovered ends", line)
}
}
}
func (d *Detail) CountSpaceBetweenDetails() int {
minSpace := 1000000
for _, line := range d.lines {
rightMostX := strings.LastIndex(line, "X")
leftMostY := strings.Index(line, "Y")
if DEBUG {
fmt.Println("processing", line, line[rightMostX+1:leftMostY])
}
space := leftMostY - rightMostX - 1
if space < minSpace {
if DEBUG {
fmt.Println("found new min", space)
}
minSpace = space
}
}
return minSpace
}
func solve(line string) int {
detail := new(Detail)
detail.Init(line)
return detail.CountSpaceBetweenDetails()
}
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
fmt.Println(solve(line))
}
}