day1
This commit is contained in:
128
2025/go/day01/day01.go
Normal file
128
2025/go/day01/day01.go
Normal 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
|
||||
}
|
||||
34
2025/go/day01/day01_test.go
Normal file
34
2025/go/day01/day01_test.go
Normal 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
4777
2025/go/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user