-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (62 loc) · 1.7 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
package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
)
func parseLine(input string) (int, int, int) {
var total, memory int
total = len(input)
for i := 1; i < total-1; i++ {
if input[i] == '\\' {
if (input[i+1]) == 'x' {
i += 2
}
i++
}
memory++
}
return total, memory, encode(input)
}
func encode(input string) int {
return len("\"" + strings.Replace(strings.Replace(input, "\\", "\\\\", -1), "\"", "\\\"", -1) + "\"")
}
func runTestcases() {
in := "\"\""
total, memory, encoded := parseLine(in)
fmt.Printf("input: % 10s --> total =% 3d memory =% 3d encoded=% 3d ", in, total, memory, encoded)
fmt.Println(total == 2, memory == 0)
in = "\"abc\""
total, memory, encoded = parseLine(in)
fmt.Printf("input: % 10s --> total =% 3d memory =% 3d encoded=% 3d ", in, total, memory, encoded)
fmt.Println(total == 5, memory == 3)
in = "\"aaa\\\"aaa\""
total, memory, encoded = parseLine(in)
fmt.Printf("input: % 10s --> total =% 3d memory =% 3d encoded=% 3d ", in, total, memory, encoded)
fmt.Println(total == 10, memory == 7)
in = "\"\\x27\""
total, memory, encoded = parseLine(in)
fmt.Printf("input: % 10s --> total =% 3d memory =% 3d encoded=% 3d ", in, total, memory, encoded)
fmt.Println(total == 6, memory == 1)
}
func runReal() {
content, err := ioutil.ReadFile("input.txt")
if err != nil {
log.Fatal(err)
}
operations := strings.Split(string(content), "\n")
var total, memory, encoded int
for _, row := range operations {
t, m, e := parseLine(row)
total += t
memory += m
encoded += e
}
fmt.Printf("total=%d, memory=%d, encoded=%d, answer=%d", total, memory, encoded, encoded-total)
}
func main() {
fmt.Println("Day 8 - 2015")
runTestcases()
runReal()
}