/
day10.go
82 lines (72 loc) · 1.75 KB
/
day10.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
package aoc2015
import (
"strconv"
"strings"
"time"
"github.com/golang/glog"
)
// lookAndSay looks at the string and returns a string
// containing how many times a rune has repeated itself following said rune.
// For instance:
// lookAndSay("1") returns "11" (one '1')
// lookAndSay("111221") returns "312211" (three '1', two '2', one '1')
//
// If input is empty it will return an empty string.
// If input contains non-integers it may render unexpected behavior,
// but will nonethtless not panic.
func lookAndSay(input string) string {
if input == "" {
return ""
}
count := 0
last := '\x00'
hasStarted := false
var total strings.Builder
for _, current := range input {
if !hasStarted {
count = 1
last = current
hasStarted = true
continue
}
if current != last {
total.WriteString(strconv.Itoa(count))
total.WriteRune(last)
count = 1
last = current
continue
}
count++
}
total.WriteString(strconv.Itoa(count))
total.WriteRune(last)
return total.String()
}
// Day10 solves the tenth day puzzle "Elves Look, Elves Say".
//
// Input
//
// A single line containing the "seed". For example:
//
// 3113322113
//
// It is guaranteed that the input represents an integer.
func Day10(input string) (answer1, answer2 string, err error) {
// iterate forty times
for ii := 0; ii < 40; ii++ {
then := time.Now()
input = lookAndSay(input)
now := time.Now()
glog.Infof("%v took %v nanoseconds", ii+1, now.Sub(then).Nanoseconds())
}
answer1 = strconv.Itoa(len(input))
// now continue this 10 more times
for ii := 0; ii < 10; ii++ {
then := time.Now()
input = lookAndSay(input)
now := time.Now()
glog.Infof("%v took %v nanoseconds", ii+41, now.Sub(then).Nanoseconds())
}
answer2 = strconv.Itoa(len(input))
return
}