-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday5a.go
More file actions
62 lines (53 loc) · 1.5 KB
/
day5a.go
File metadata and controls
62 lines (53 loc) · 1.5 KB
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
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
// "strconv"
"strings"
"time"
)
var print = fmt.Println
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}
func parseFile(path string) string {
filename, _ := filepath.Abs(path)
content, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("error reading input file: %v", err)
}
c := string(content)
return c
}
func reactPolymer(polymer string) string {
shrunk_polymer := polymer
for pos, char := range polymer {
if pos > 0 {
ascii_val := int(rune(char))
prev_ascii_val := int(rune(polymer[pos-1]))
difference := Abs(ascii_val - prev_ascii_val)
// 32 is the ascii value difference of the lower case letter and its upper case counterpart
if 32 == difference {
// print(difference)
// print(string(polymer[pos-1])+string(char))
// remove the two characters. they are opposite polarity
shrunk_polymer = reactPolymer(strings.Replace(polymer, string(polymer[pos-1])+string(char), "", -1))
return shrunk_polymer
}
}
}
return shrunk_polymer
}
func main() {
start := time.Now()
polymer_string := parseFile("./input/day5.txt")
print(len(polymer_string))
shrunk_polymer := reactPolymer(polymer_string)
print(len(shrunk_polymer))
fmt.Printf("Runtime took %s\n", time.Since(start))
}