|
|
@@ -6,6 +6,47 @@ import (
|
|
|
"os"
|
|
|
)
|
|
|
|
|
|
+var path []byte = []byte{'r', 'o', 'y', 'g', 'b', 'v'}
|
|
|
+
|
|
|
+func abs(x int) int {
|
|
|
+ if x < 0 {
|
|
|
+ return -x
|
|
|
+ }
|
|
|
+
|
|
|
+ return x
|
|
|
+}
|
|
|
+
|
|
|
+func getDistances() map[byte]map[byte]int {
|
|
|
+ distances := make(map[byte]map[byte]int)
|
|
|
+
|
|
|
+ for i := range path {
|
|
|
+ distances[path[i]] = make(map[byte]int)
|
|
|
+ for j := range path {
|
|
|
+ if i == j {
|
|
|
+ distances[path[i]][path[j]] = 6
|
|
|
+ } else if i < j {
|
|
|
+ distances[path[i]][path[j]] = j - i
|
|
|
+ } else {
|
|
|
+ distances[path[i]][path[j]] = len(path) - i + j
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return distances
|
|
|
+}
|
|
|
+
|
|
|
+func part1(program []byte, distances map[byte]map[byte]int) int {
|
|
|
+ var distance int
|
|
|
+ var current byte = 'r'
|
|
|
+
|
|
|
+ for i := range program {
|
|
|
+ distance += distances[current][program[i]]
|
|
|
+ current = program[i]
|
|
|
+ }
|
|
|
+
|
|
|
+ return distance
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
if len(os.Args) < 2 {
|
|
|
log.Fatal("You need to specify a file!")
|
|
|
@@ -17,6 +58,7 @@ func main() {
|
|
|
}
|
|
|
|
|
|
program = program[:len(program)-1]
|
|
|
+ distances := getDistances()
|
|
|
|
|
|
- fmt.Println(program)
|
|
|
+ fmt.Println("Part1:", part1(program, distances))
|
|
|
}
|