Skip to content

Commit f7e1ab2

Browse files
committed
feat(2017): refactor day 5
Refactored Day 5 of 2017 to use the common framework. Successfully migrated solution and integrated it into the runner.
1 parent eb91021 commit f7e1ab2

3 files changed

Lines changed: 1091 additions & 0 deletions

File tree

2017/days/5/day05.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
8+
"github.com/markkovari/advent_of_code/aoc-go-common"
9+
)
10+
11+
type Day05 struct{}
12+
13+
func (d Day05) Part1(input string) string {
14+
jumps := parse(input)
15+
current, steps := 0, 0
16+
for current >= 0 && current < len(jumps) {
17+
jump := jumps[current]
18+
jumps[current]++
19+
current += jump
20+
steps++
21+
}
22+
return fmt.Sprintf("%d", steps)
23+
}
24+
25+
func (d Day05) Part2(input string) string {
26+
jumps := parse(input)
27+
current, steps := 0, 0
28+
for current >= 0 && current < len(jumps) {
29+
jump := jumps[current]
30+
if jump >= 3 {
31+
jumps[current]--
32+
} else {
33+
jumps[current]++
34+
}
35+
current += jump
36+
steps++
37+
}
38+
return fmt.Sprintf("%d", steps)
39+
}
40+
41+
func parse(input string) []int {
42+
var nums []int
43+
for _, line := range strings.Split(input, "\n") {
44+
if line == "" { continue }
45+
val, _ := strconv.Atoi(line)
46+
nums = append(nums, val)
47+
}
48+
return nums
49+
}
50+
51+
func main() {
52+
common.Run(2017, 5, Day05{})
53+
}

0 commit comments

Comments
 (0)