mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2025-04-20 03:03:47 +01:00
Compare commits
No commits in common. "af4701dab7c1bbea0194e979622de9fdcd05d335" and "c6dd5475fafc6a2cb2e3bd0cbc1efefa1e87b402" have entirely different histories.
af4701dab7
...
c6dd5475fa
@ -1,3 +0,0 @@
|
|||||||
module github.com/onyx-and-iris/aoc2023/day-8
|
|
||||||
|
|
||||||
go 1.20
|
|
28
day-8/one.go
28
day-8/one.go
@ -1,28 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
const (
|
|
||||||
LEFT = iota // 0
|
|
||||||
RIGHT
|
|
||||||
)
|
|
||||||
|
|
||||||
var directions string
|
|
||||||
var steps = map[string][]string{}
|
|
||||||
|
|
||||||
// one returns the number of steps to reach 'ZZZ'
|
|
||||||
func one(lines []string) int {
|
|
||||||
parselines(lines)
|
|
||||||
|
|
||||||
num := 0
|
|
||||||
for i, step := 0, "AAA"; step != "ZZZ"; 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]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return num
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
INPUT="input.txt"
|
|
||||||
|
|
||||||
cat $INPUT | go run .
|
|
@ -1,21 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
log.SetLevel(log.InfoLevel)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
lines := readlines()
|
|
||||||
|
|
||||||
ans := one(lines)
|
|
||||||
fmt.Printf("solution one: %d\n", ans)
|
|
||||||
|
|
||||||
ans = two(lines)
|
|
||||||
fmt.Printf("solution two: %d\n", ans)
|
|
||||||
}
|
|
59
day-8/two.go
59
day-8/two.go
@ -1,59 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
// readlines reads lines from stdin.
|
|
||||||
// returns input as an array of strings
|
|
||||||
func readlines() []string {
|
|
||||||
lines := []string{}
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
|
||||||
for scanner.Scan() {
|
|
||||||
lines = append(lines, scanner.Text())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return lines
|
|
||||||
}
|
|
||||||
|
|
||||||
var r = regexp.MustCompile(`(?P<identifier>[A-Z0-9]{3}) = \((?P<entry>[A-Z0-9]{3}), (?P<exit>[A-Z0-9]{3})\)`)
|
|
||||||
|
|
||||||
func parselines(lines []string) {
|
|
||||||
directions = lines[0]
|
|
||||||
|
|
||||||
steps = make(map[string][]string, len(lines[2:]))
|
|
||||||
for _, line := range lines[2:] {
|
|
||||||
m := r.FindStringSubmatch(line)
|
|
||||||
if len(m) > 0 {
|
|
||||||
steps[m[1]] = []string{m[2], m[3]}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user