Files
adventofcode/2025/go/day01/day01.go
2025-12-01 14:04:11 +00:00

129 lines
2.4 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 Part1(input string) int {
lines := strings.Split(input, "\n")
position := 50
code := 0
Instructions := make([]Instruction, 0)
for _, line := range lines {
var steps int
var dir Dir
switch line[0] {
case 'L':
steps = utils.MustAtoi(line[1:])
dir = Left
case 'R':
steps = utils.MustAtoi(line[1:])
dir = Right
default:
fmt.Println("Invalid direction")
continue
}
if steps > 100 {
steps = steps % 100
}
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
}
fmt.Print(Instructions)
for _, instr := range Instructions {
if instr.Turn == Left {
if position-instr.Steps < 0 {
position = 100 + (position - instr.Steps)
} else {
position -= instr.Steps
}
} else if instr.Turn == Right {
if position+instr.Steps >= 100 {
position = (position + instr.Steps) - 100
} else {
position += instr.Steps
}
}
fmt.Println("\n", instr.Turn, instr.Steps, "->", position)
if position == 0 {
code++
}
}
return code
}
func Part2(input string) int {
lines := strings.Split(input, "\n")
position := 50
code := 0
Instructions := make([]Instruction, 0)
for _, line := range lines {
var steps int
var dir Dir
switch line[0] {
case 'L':
steps = utils.MustAtoi(line[1:])
dir = Left
case 'R':
steps = utils.MustAtoi(line[1:])
dir = Right
default:
fmt.Println("Invalid direction")
continue
}
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
}
fmt.Print(Instructions)
for _, instr := range Instructions {
if instr.Steps > 100 {
code = code + (instr.Steps / 100)
instr.Steps = instr.Steps % 100
}
if instr.Turn == Left {
if position-instr.Steps < 0 {
if position != 0 {
code++
}
position = 100 + (position - instr.Steps)
} else {
position -= instr.Steps
if position == 0 {
code++
}
}
} else if instr.Turn == Right {
if position+instr.Steps > 99 {
if position != 0 {
code++
}
position = (position + instr.Steps) - 100
} else {
position += instr.Steps
if position == 0 {
code++
}
}
}
fmt.Println("\n", instr.Turn, instr.Steps, "->", position, "code:", code)
}
return code
}