mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/alexchao26/advent-of-code-go/cast"
|
|
"github.com/alexchao26/advent-of-code-go/util"
|
|
)
|
|
|
|
func main() {
|
|
var part int
|
|
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
|
flag.Parse()
|
|
fmt.Println("Running part", part)
|
|
|
|
if part == 1 {
|
|
ans := part1(util.ReadFile("./input.txt"))
|
|
fmt.Println("Output:", ans)
|
|
} else {
|
|
ans := part2(util.ReadFile("./input.txt"))
|
|
fmt.Println("Output:", ans)
|
|
}
|
|
}
|
|
|
|
func part1(input string) int {
|
|
maze := parseInput(input)
|
|
|
|
var index, steps int
|
|
for index >= 0 && index < len(maze) {
|
|
maze[index]++
|
|
index += maze[index] - 1
|
|
steps++
|
|
}
|
|
|
|
return steps
|
|
}
|
|
|
|
func part2(input string) int {
|
|
maze := parseInput(input)
|
|
|
|
var index, steps int
|
|
for index >= 0 && index < len(maze) {
|
|
nextIndex := maze[index]
|
|
if maze[index] >= 3 {
|
|
maze[index]--
|
|
} else {
|
|
maze[index]++
|
|
}
|
|
index += nextIndex
|
|
steps++
|
|
}
|
|
|
|
return steps
|
|
}
|
|
|
|
func parseInput(input string) (ans []int) {
|
|
lines := strings.Split(input, "\n")
|
|
for _, l := range lines {
|
|
ans = append(ans, cast.ToInt(l))
|
|
}
|
|
return ans
|
|
}
|