101 lines
1.6 KiB
Go
101 lines
1.6 KiB
Go
package day01
|
|
|
|
import (
|
|
"adventofcode2025/utils"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Dir int
|
|
|
|
const (
|
|
Left Dir = iota
|
|
Right
|
|
Safe
|
|
Unsafe
|
|
)
|
|
|
|
type Instruction struct {
|
|
Turn Dir
|
|
Steps int
|
|
}
|
|
|
|
func parseInput(input string) []Instruction {
|
|
Instructions := make([]Instruction, 0)
|
|
lines := strings.Split(input, "\n")
|
|
for _, line := range lines {
|
|
var steps int
|
|
var dir Dir
|
|
steps = utils.MustAtoi(line[1:])
|
|
switch line[0] {
|
|
case 'L':
|
|
dir = Left
|
|
case 'R':
|
|
dir = Right
|
|
default:
|
|
fmt.Println("Invalid direction")
|
|
continue
|
|
}
|
|
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
|
|
}
|
|
return Instructions
|
|
}
|
|
|
|
func Part1(input string) int {
|
|
|
|
Instructions := parseInput(input)
|
|
position := 50
|
|
code := 0
|
|
|
|
for _, instr := range Instructions {
|
|
switch instr.Turn {
|
|
case Left:
|
|
position -= instr.Steps % 100
|
|
case Right:
|
|
position += instr.Steps % 100
|
|
}
|
|
if position < 0 {
|
|
position += 100
|
|
} else if position > 99 {
|
|
position -= 100
|
|
}
|
|
if position == 0 {
|
|
code++
|
|
}
|
|
}
|
|
return code
|
|
}
|
|
|
|
func Part2(input string) int {
|
|
Instructions := parseInput(input)
|
|
position := 50
|
|
code := 0
|
|
new_position := position
|
|
|
|
for _, instr := range Instructions {
|
|
switch instr.Turn {
|
|
case Left:
|
|
new_position = position - instr.Steps%100
|
|
case Right:
|
|
new_position = position + instr.Steps%100
|
|
}
|
|
code += instr.Steps / 100
|
|
if new_position == 0 {
|
|
code++
|
|
}
|
|
if new_position < 0 {
|
|
new_position += 100
|
|
if position != 0 {
|
|
code++
|
|
}
|
|
} else if new_position > 99 {
|
|
new_position -= 100
|
|
if position != 0 {
|
|
code++
|
|
}
|
|
}
|
|
position = new_position
|
|
}
|
|
return code
|
|
}
|