Skip to content

Commit a4564f5

Browse files
committed
refactor(golang/2017): structure days into packages
1 parent b73926c commit a4564f5

26 files changed

Lines changed: 1869 additions & 26 deletions

File tree

golang/2017/day01/solver.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package day01
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
type Solver struct{}
9+
10+
func (d Solver) Part1(input string) string {
11+
input = strings.TrimSpace(input)
12+
sum := 0
13+
for i := 0; i < len(input); i++ {
14+
next := (i + 1) % len(input)
15+
if input[i] == input[next] {
16+
sum += int(input[i] - '0')
17+
}
18+
}
19+
return fmt.Sprintf("%d", sum)
20+
}
21+
22+
func (d Solver) Part2(input string) string {
23+
input = strings.TrimSpace(input)
24+
sum := 0
25+
length := len(input)
26+
step := length / 2
27+
for i := 0; i < length; i++ {
28+
next := (i + step) % length
29+
if input[i] == input[next] {
30+
sum += int(input[i] - '0')
31+
}
32+
}
33+
return fmt.Sprintf("%d", sum)
34+
}

golang/2017/day02/solver.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package day02
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type Solver struct{}
10+
11+
func (d Solver) Part1(input string) string {
12+
sum := 0
13+
for _, line := range strings.Split(input, "\n") {
14+
if line == "" {
15+
continue
16+
}
17+
numbers := d.parseRow(line)
18+
min, max := d.findSmallestAndBiggest(numbers)
19+
sum += max - min
20+
}
21+
return fmt.Sprintf("%d", sum)
22+
}
23+
24+
func (d Solver) Part2(input string) string {
25+
sum := 0
26+
for _, line := range strings.Split(input, "\n") {
27+
if line == "" {
28+
continue
29+
}
30+
numbers := d.parseRow(line)
31+
sum += d.getDivisible(numbers)
32+
}
33+
return fmt.Sprintf("%d", sum)
34+
}
35+
36+
func (d Solver) parseRow(line string) []int {
37+
var row []int
38+
for _, part := range strings.Fields(line) {
39+
val, _ := strconv.Atoi(part)
40+
row = append(row, val)
41+
}
42+
return row
43+
}
44+
45+
func (d Solver) findSmallestAndBiggest(numbers []int) (int, int) {
46+
smallest, biggest := numbers[0], numbers[0]
47+
for _, num := range numbers {
48+
if num < smallest {
49+
smallest = num
50+
}
51+
if num > biggest {
52+
biggest = num
53+
}
54+
}
55+
return smallest, biggest
56+
}
57+
58+
func (d Solver) getDivisible(numbers []int) int {
59+
for i, num := range numbers {
60+
for j, num2 := range numbers {
61+
if i != j && num%num2 == 0 {
62+
return num / num2
63+
}
64+
}
65+
}
66+
return 0
67+
}

golang/2017/day03/solver.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package day03
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type Solver struct{}
11+
12+
func (d Solver) Part1(input string) string {
13+
num, _ := strconv.Atoi(strings.TrimSpace(input))
14+
return fmt.Sprintf("%d", d.calculateDistance(num))
15+
}
16+
17+
func (d Solver) Part2(input string) string {
18+
num, _ := strconv.Atoi(strings.TrimSpace(input))
19+
return fmt.Sprintf("%d", d.calculateStressedDistance(num))
20+
}
21+
22+
func (d Solver) abs(x int) int {
23+
if x < 0 {
24+
return -x
25+
}
26+
return x
27+
}
28+
29+
func (d Solver) calculateDistance(n int) int {
30+
if n == 1 {
31+
return 0
32+
}
33+
ring := int(math.Ceil((math.Sqrt(float64(n)) - 1) / 2))
34+
side := 2*ring + 1
35+
maxNum := side * side
36+
sideLen := side - 1
37+
midPoints := make([]int, 4)
38+
for i := 0; i < 4; i++ {
39+
midPoints[i] = maxNum - sideLen/2 - i*sideLen
40+
}
41+
minDist := d.abs(n - midPoints[0])
42+
for _, midPoint := range midPoints {
43+
dist := d.abs(n - midPoint)
44+
if dist < minDist {
45+
minDist = dist
46+
}
47+
}
48+
return ring + minDist
49+
}
50+
51+
func (d Solver) calculateStressedDistance(inputNum int) int {
52+
directions := [][2]int{{0, 1}, {-1, 0}, {0, -1}, {1, 0}}
53+
allNeighborDiffs := [][2]int{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}
54+
coordsToNeighborSum := map[[2]int]int{{0, 0}: 1, {0, 1}: 1}
55+
directionIndex := 0
56+
row, col := 0, 1
57+
for {
58+
leftDiff := directions[(directionIndex+1)%4]
59+
leftCoord := [2]int{row + leftDiff[0], col + leftDiff[1]}
60+
if _, ok := coordsToNeighborSum[leftCoord]; !ok {
61+
directionIndex = (directionIndex + 1) % 4
62+
}
63+
diff := directions[directionIndex]
64+
row += diff[0]
65+
col += diff[1]
66+
next := [2]int{row, col}
67+
var sum int
68+
for _, d := range allNeighborDiffs {
69+
sum += coordsToNeighborSum[[2]int{row + d[0], col + d[1]}]
70+
}
71+
if sum > inputNum {
72+
return sum
73+
}
74+
coordsToNeighborSum[next] = sum
75+
}
76+
}

golang/2017/day04/solver.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package day04
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
)
8+
9+
type Solver struct{}
10+
11+
func (d Solver) Part1(input string) string {
12+
countValid := 0
13+
for _, line := range strings.Split(input, "\n") {
14+
if line == "" {
15+
continue
16+
}
17+
words := strings.Fields(line)
18+
frequency := make(map[string]int)
19+
valid := true
20+
for _, word := range words {
21+
frequency[word]++
22+
if frequency[word] > 1 {
23+
valid = false
24+
break
25+
}
26+
}
27+
if valid {
28+
countValid++
29+
}
30+
}
31+
return fmt.Sprintf("%d", countValid)
32+
}
33+
34+
func (d Solver) Part2(input string) string {
35+
countValid := 0
36+
for _, line := range strings.Split(input, "\n") {
37+
if line == "" {
38+
continue
39+
}
40+
words := strings.Fields(line)
41+
if !d.hasAnagram(words) {
42+
countValid++
43+
}
44+
}
45+
return fmt.Sprintf("%d", countValid)
46+
}
47+
48+
func (d Solver) hasAnagram(words []string) bool {
49+
sortedWords := make([]string, len(words))
50+
for i, word := range words {
51+
chars := strings.Split(word, "")
52+
sort.Strings(chars)
53+
sortedWords[i] = strings.Join(chars, "")
54+
}
55+
for i := 0; i < len(sortedWords); i++ {
56+
for j := i + 1; j < len(sortedWords); j++ {
57+
if sortedWords[i] == sortedWords[j] {
58+
return true
59+
}
60+
}
61+
}
62+
return false
63+
}

golang/2017/day05/solver.go

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

golang/2017/day06/solver.go

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

0 commit comments

Comments
 (0)