Day01
This commit is contained in:
64
2025/gareth/day01/day01.go
Normal file
64
2025/gareth/day01/day01.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package day01
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
position := 50
|
||||
output := 0
|
||||
|
||||
for _, line := range lines {
|
||||
direction := line[0]
|
||||
amount, _ := strconv.Atoi(line[1:])
|
||||
|
||||
if direction == 'R' {
|
||||
position += amount
|
||||
for position > 99 {
|
||||
position -= 100
|
||||
}
|
||||
} else if direction == 'L' {
|
||||
position -= amount
|
||||
for position < 0 {
|
||||
position += 100
|
||||
}
|
||||
}
|
||||
|
||||
if position == 0 {
|
||||
output++
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
position := 50
|
||||
output := 0
|
||||
|
||||
for _, line := range lines {
|
||||
direction := line[0]
|
||||
amount, _ := strconv.Atoi(line[1:])
|
||||
|
||||
step := 1
|
||||
if direction == 'L' {
|
||||
step = -1
|
||||
}
|
||||
|
||||
for i := 0; i < amount; i++ {
|
||||
position = (position + step) % 100
|
||||
if position < 0 {
|
||||
position += 100
|
||||
}
|
||||
if position == 0 {
|
||||
output++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
Reference in New Issue
Block a user