mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-22 10:20:48 +00:00
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// GCF returns the greatest common factor of two numbers
|
|
func GCF(a, b int) int {
|
|
for b != 0 {
|
|
t := b
|
|
b = a % b
|
|
a = t
|
|
}
|
|
return a
|
|
}
|
|
|
|
// LCM returns least common multiple of a set of numbers
|
|
func LCM(a, b int, integers ...int) int {
|
|
result := a * b / GCF(a, b)
|
|
|
|
for i := 0; i < len(integers); i++ {
|
|
result = LCM(result, integers[i])
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// two returns minimum steps required until all routes end with '*Z'
|
|
func two(lines []string) int {
|
|
parselines(lines)
|
|
|
|
startpoints := make([]string, 0)
|
|
for k := range steps {
|
|
if strings.HasSuffix(k, "A") {
|
|
startpoints = append(startpoints, k)
|
|
}
|
|
}
|
|
|
|
nums := []int{}
|
|
for _, step := range startpoints {
|
|
num := 0
|
|
for i := 0; !strings.HasSuffix(step, "Z"); i, num = i+1, num+1 {
|
|
if i == len(directions) {
|
|
i = 0
|
|
}
|
|
if directions[i] == 'L' {
|
|
step = steps[step][LEFT]
|
|
} else {
|
|
step = steps[step][RIGHT]
|
|
}
|
|
}
|
|
nums = append(nums, num)
|
|
}
|
|
|
|
lcm := 0
|
|
lcm = LCM(nums[0], nums[1], nums[2:]...)
|
|
|
|
return lcm
|
|
}
|