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
|
||||
}
|
||||
35
2025/gareth/day01/day01_test.go
Normal file
35
2025/gareth/day01/day01_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package day01
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82`)
|
||||
assert.Equal(t, 3, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82`)
|
||||
assert.Equal(t, 6, r)
|
||||
}
|
||||
4059
2025/gareth/day01/input.txt
Normal file
4059
2025/gareth/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
49
2025/gareth/day01/poc.py
Normal file
49
2025/gareth/day01/poc.py
Normal file
@@ -0,0 +1,49 @@
|
||||
def part1(input_file):
|
||||
file = open(input_file, "r")
|
||||
line = file.readline()
|
||||
|
||||
position = 50
|
||||
output = 0
|
||||
while line:
|
||||
direction = line[0]
|
||||
amount = int(line[1:])
|
||||
|
||||
if direction == "R":
|
||||
position += amount
|
||||
while position > 99:
|
||||
position -= 100
|
||||
|
||||
elif direction == "L":
|
||||
position -= amount
|
||||
while position < 0:
|
||||
position += 100
|
||||
|
||||
if position == 0:
|
||||
output += 1
|
||||
|
||||
line = file.readline()
|
||||
|
||||
file.close()
|
||||
print("Part 1:", output)
|
||||
|
||||
def part2(input_file):
|
||||
position = 50
|
||||
output = 0
|
||||
|
||||
with open(input_file) as file:
|
||||
for line in file:
|
||||
line = line.strip()
|
||||
direction = line[0]
|
||||
amount = int(line[1:])
|
||||
|
||||
step = 1 if direction == "R" else -1
|
||||
|
||||
for _ in range(amount):
|
||||
position = (position + step) % 100
|
||||
if position == 0:
|
||||
output += 1
|
||||
|
||||
print("Part 2:", output)
|
||||
|
||||
part1("input.txt")
|
||||
part2("input.txt")
|
||||
Reference in New Issue
Block a user