This commit is contained in:
2025-12-01 14:04:11 +00:00
parent d730d3c444
commit 358249e78a
17 changed files with 6079 additions and 0 deletions

128
2025/go/day01/day01.go Normal file
View File

@@ -0,0 +1,128 @@
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
}

View File

@@ -0,0 +1,34 @@
package day01
import (
"testing"
"github.com/stretchr/testify/require")
func TestPart1(t *testing.T) {
r := Part1(`L68
L30
R48
L5
R60
L55
L1
L99
R14
L82`)
require.Equal(t, 3, r)
}
func TestPart2(t *testing.T) {
r := Part2(`L68
L30
R48
L5
R60
L55
L1
L99
R14
L82`)
require.Equal(t, 6, r)
}

4777
2025/go/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff