-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
111 lines (98 loc) · 1.92 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"bufio"
"io"
"math"
"strings"
)
func fs1(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
m := make(map[string][]string)
start := ""
for scanner.Scan() {
s := scanner.Text()
if s == "" {
scanner.Scan()
start = scanner.Text()
break
}
idx := indexAll(s, " ")
m[s[:idx[0]]] = append(m[s[:idx[0]]], s[idx[1]+1:])
}
set := make(map[string]struct{})
for i := 0; i < len(start); i++ {
s := start[i : i+1]
if options, exists := m[s]; exists {
for _, v := range options {
res := start[:i] + v + start[i+1:]
set[res] = struct{}{}
}
}
if i != len(start)-1 {
s = start[i : i+2]
if options, exists := m[s]; exists {
for _, v := range options {
res := start[:i] + v + start[i+2:]
set[res] = struct{}{}
}
}
}
}
return len(set), nil
}
func fs2(input io.Reader) (int, error) {
scanner := bufio.NewScanner(input)
m := make(map[string][]string)
reverse := make(map[string]string)
target := ""
for scanner.Scan() {
s := scanner.Text()
if s == "" {
scanner.Scan()
target = scanner.Text()
break
}
idx := indexAll(s, " ")
from := s[:idx[0]]
to := s[idx[1]+1:]
m[from] = append(m[from], to)
reverse[to] = from
}
start := "e"
return best(target, start, reverse), nil
}
func best(start, target string, reverse map[string]string) int {
if start == target {
return 0
}
for to, from := range reverse {
all := indexAll(start, to)
for _, idx := range all {
s := start[:idx] + from + start[idx+len(to):]
v := best(s, target, reverse)
if v != math.MaxInt {
return v + 1
}
}
}
return math.MaxInt
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func indexAll(s string, search string) []int {
i := 0
var res []int
for i < len(s) {
index := strings.Index(s[i:], search)
if index == -1 {
return res
}
res = append(res, index+i)
i += index + len(search)
}
return res
}