-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain1.go
81 lines (62 loc) · 1.52 KB
/
main1.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
type Replacement struct {
From string
To string
}
func main() {
atoms, replacements, input := parse()
// set of found molecules
molecules := make(map[string]bool)
// loop on the input string
for i := 0; i < len(input); i++ {
// Try to see if the substring starting in `i` is a known atom
for _, a := range atoms {
if strings.HasPrefix(input[i:], a) {
// ok, we found an atom `a` starting at `i`
// Apply all the right replacements
for _, r := range replacements {
if r.From == a {
// substring before `i` is untouched
// `r.To` is the replacement string
// substring starting in `i+len(a)` is untouched
newMolecule := input[:i] + r.To + input[i+len(a):]
molecules[newMolecule] = true
}
}
}
}
}
fmt.Println(len(molecules))
}
func parse() (atoms []string, replacements []Replacement, input string) {
scanner := bufio.NewScanner(os.Stdin)
atomSet := make(map[string]bool)
replacements = make([]Replacement, 0)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
break
}
var from, to string
n, _ := fmt.Sscanf(line, "%s => %s", &from, &to)
if n != 2 {
log.Fatal("parsing failed")
}
atomSet[from] = true
replacements = append(replacements, Replacement{From: from, To: to})
}
atoms = make([]string, 0, len(atomSet))
for k := range atomSet {
atoms = append(atoms, k)
}
scanner.Scan()
input = strings.TrimSpace(scanner.Text())
return
}