-
Notifications
You must be signed in to change notification settings - Fork 3
/
day05.go
111 lines (94 loc) · 1.65 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Package main provides ...
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func AddOne(x int) int {
return x + 1
}
// Parse filename -> slice of ints
func Parse(filename string) []int {
ints := make([]int, 0)
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
i, err := strconv.Atoi(line)
if err != nil {
log.Fatal(err)
}
ints = append(ints, i)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return ints
}
func Part1(jumps []int) int {
current_idx := 0
old_idx := 0
steps := 0
max := len(jumps)
for {
// Save old
old_idx = current_idx
// Are we done?
if current_idx >= max {
break
}
// Jump
current_jump := jumps[current_idx]
current_idx += current_jump
// Increment old by one
jumps[old_idx] += 1
steps += 1
}
return steps
}
func Part2(jumps []int) int {
current_idx := 0
old_idx := 0
steps := 0
max := len(jumps)
for {
// Save old
old_idx = current_idx
// Are we done?
if current_idx >= max {
// fmt.Println(jumps)
break
}
// Jump
current_jump := jumps[current_idx]
current_idx += current_jump
// Increment old by one
if jumps[old_idx] >= 3 {
jumps[old_idx] -= 1
} else {
jumps[old_idx] += 1
}
steps += 1
}
return steps
}
func main() {
jumps := Parse("../input.txt")
fmt.Println("Part1:")
// fmt.Println(jumps)
steps := Part1(jumps)
fmt.Println(steps)
fmt.Println("Part2:")
jumps = Parse("../input.txt")
// fmt.Println(jumps)
// 3220: Incorrect guess, answer is too low.
steps = Part2(jumps)
fmt.Println(steps)
}