-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathday05.go
63 lines (57 loc) · 1.17 KB
/
day05.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
var inputFile = flag.String("inputFile", "inputs/day05.input", "Relative file path to use as input.")
var partB = flag.Bool("partB", false, "Whether to use the Part B logic.")
func main() {
flag.Parse()
f, err := os.Open(*inputFile)
if err != nil {
return
}
defer f.Close()
r := bufio.NewReader(f)
l, err := r.ReadString('\n')
l = l[:len(l)-1]
if !*partB {
unmodified := react(l)
result := len(unmodified)
fmt.Printf("Result is %d\n", result)
} else {
shortest := len(l)
for c := 'A'; c <= 'Z'; c++ {
second := c + 32
modified := strings.Replace(l, string([]rune{second}), "", -1)
modified = strings.Replace(modified, string([]rune{c}), "", -1)
candidate := len(react(modified))
if candidate < shortest {
shortest = candidate
}
}
fmt.Printf("Result is %d\n", shortest)
}
}
func react(l string) string {
current := l
for {
transformed := false
prev := ' '
for i, c := range current {
if c-prev == 32 || prev-c == 32 {
transformed = true
current = current[0:i-1] + current[i+1:]
break
}
prev = c
}
if !transformed {
break
}
}
return current
}