Skip to content

Commit cb72411

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

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

2017/days/6/day06.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/markkovari/advent_of_code/aoc-go-common"
8+
)
9+
10+
type Day06 struct{}
11+
12+
func (d Day06) Part1(input string) string {
13+
banks := parse(input)
14+
seen := make(map[[16]int]int)
15+
cycles := 0
16+
for {
17+
if _, ok := seen[banks]; ok {
18+
return fmt.Sprintf("%d", cycles)
19+
}
20+
seen[banks] = cycles
21+
22+
maxIdx, maxVal := 0, banks[0]
23+
for i, val := range banks {
24+
if val > maxVal {
25+
maxIdx, maxVal = i, val
26+
}
27+
}
28+
29+
banks[maxIdx] = 0
30+
for i := 1; i <= maxVal; i++ {
31+
banks[(maxIdx+i)%16]++
32+
}
33+
cycles++
34+
}
35+
}
36+
37+
func (d Day06) Part2(input string) string {
38+
banks := parse(input)
39+
seen := make(map[[16]int]int)
40+
cycles := 0
41+
for {
42+
if firstSeen, ok := seen[banks]; ok {
43+
return fmt.Sprintf("%d", cycles-firstSeen)
44+
}
45+
seen[banks] = cycles
46+
47+
maxIdx, maxVal := 0, banks[0]
48+
for i, val := range banks {
49+
if val > maxVal {
50+
maxIdx, maxVal = i, val
51+
}
52+
}
53+
54+
banks[maxIdx] = 0
55+
for i := 1; i <= maxVal; i++ {
56+
banks[(maxIdx+i)%16]++
57+
}
58+
cycles++
59+
}
60+
}
61+
62+
func parse(input string) [16]int {
63+
var banks [16]int
64+
for i, s := range strings.Fields(input) {
65+
fmt.Sscanf(s, "%d", &banks[i])
66+
}
67+
return banks
68+
}
69+
70+
func main() {
71+
common.Run(2017, 6, Day06{})
72+
}

2017/inputs/6/prod.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 5 10 0 11 14 13 4 11 8 8 7 1 4 12 11

2017/inputs/6/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 2 7 0

0 commit comments

Comments
 (0)