Skip to content

Commit 0401846

Browse files
committed
Move reusable functions to dedicated libraries
In order to reduce solution time in future. Signed-off-by: Alexander Kurbatov <sir.alkurbatov@yandex.ru>
1 parent f10b8d2 commit 0401846

File tree

6 files changed

+43
-35
lines changed

6 files changed

+43
-35
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ linters:
8888
- bodyclose
8989
- cyclop
9090
# - deadcode
91-
- depguard
91+
# - depguard
9292
- dogsled
9393
# - dupl
9494
- durationcheck

2023/1/main.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"log"
88
"os"
99
"regexp"
10+
11+
"github.com/alkurbatov/adventofcode/internal/strmod"
1012
)
1113

1214
var numsMap = map[string]int{
@@ -30,15 +32,6 @@ var numsMap = map[string]int{
3032
"9": 9,
3133
}
3234

33-
func reverse(s string) string {
34-
rns := []rune(s)
35-
for i, j := 0, len(rns)-1; i < j; i, j = i+1, j-1 {
36-
rns[i], rns[j] = rns[j], rns[i]
37-
}
38-
39-
return string(rns)
40-
}
41-
4235
func main() {
4336
input, err := os.Open("./2023/1/input.txt")
4437
if err != nil {
@@ -75,13 +68,13 @@ func main() {
7568
return
7669
}
7770

78-
lastNum := lastNumber.FindString(reverse(src))
71+
lastNum := lastNumber.FindString(strmod.Reverse(src))
7972
if lastNum == "" {
8073
log.Printf("Failed to find last number in input string: %s", src)
8174
return
8275
}
8376

84-
hiddenNumber = numsMap[firstNum]*10 + numsMap[reverse(lastNum)]
77+
hiddenNumber = numsMap[firstNum]*10 + numsMap[strmod.Reverse(lastNum)]
8578
secondPartResult += hiddenNumber
8679
log.Printf("Part 2: %s -> %d", src, hiddenNumber)
8780
}

2023/8/main.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"os"
1010
"regexp"
1111
"strings"
12+
13+
"github.com/alkurbatov/adventofcode/internal/calculus"
1214
)
1315

1416
var errNoNodes = errors.New("no nodes found")
@@ -85,28 +87,6 @@ func calcSteps(route []rune, from, to string, nodes map[string]Node) int {
8587
}
8688
}
8789

88-
// Find greatest common divisor (GCD) via Euclidean algorithm.
89-
func FindGCD(a, b int) int {
90-
for b != 0 {
91-
t := b
92-
b = a % b
93-
a = t
94-
}
95-
96-
return a
97-
}
98-
99-
// Find Least Common Multiple (LCM) via Greatest Common Divisor (GCD).
100-
func FindLCM(a, b int, values ...int) int {
101-
result := a * b / FindGCD(a, b)
102-
103-
for i := 0; i < len(values); i++ {
104-
result = FindLCM(result, values[i])
105-
}
106-
107-
return result
108-
}
109-
11090
func calcGhostSteps(route []rune, nodes map[string]Node) int {
11191
steps := make([]int, 0)
11292

@@ -118,7 +98,7 @@ func calcGhostSteps(route []rune, nodes map[string]Node) int {
11898
steps = append(steps, calcSteps(route, from, "Z", nodes))
11999
}
120100

121-
return FindLCM(steps[0], steps[1], steps...)
101+
return calculus.FindLCM(steps...)
122102
}
123103

124104
func main() {

internal/calculus/gcd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package calculus
2+
3+
// Find greatest common divisor (GCD) via Euclidean algorithm.
4+
func FindGCD(a, b int) int {
5+
for b != 0 {
6+
t := b
7+
b = a % b
8+
a = t
9+
}
10+
11+
return a
12+
}

internal/calculus/lcm.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package calculus
2+
3+
// Find Least Common Multiple (LCM) via Greatest Common Divisor (GCD).
4+
func FindLCM(values ...int) int {
5+
result := values[0] * values[1] / FindGCD(values[0], values[1])
6+
7+
for i := 2; i < len(values); i++ {
8+
result = FindLCM(result, values[i])
9+
}
10+
11+
return result
12+
}

internal/strmod/reverse.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package strmod
2+
3+
// Reverse string.
4+
func Reverse(s string) string {
5+
rns := []rune(s)
6+
for i, j := 0, len(rns)-1; i < j; i, j = i+1, j-1 {
7+
rns[i], rns[j] = rns[j], rns[i]
8+
}
9+
10+
return string(rns)
11+
}

0 commit comments

Comments
 (0)