Day01
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
2024
|
||||
2023/*
|
||||
|
||||
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")
|
||||
11
2025/gareth/go.mod
Normal file
11
2025/gareth/go.mod
Normal file
@@ -0,0 +1,11 @@
|
||||
module aoc/2025
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require github.com/stretchr/testify v1.11.1
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
9
2025/gareth/go.sum
Normal file
9
2025/gareth/go.sum
Normal file
@@ -0,0 +1,9 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
17
2025/gareth/main.go
Normal file
17
2025/gareth/main.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"aoc/2025/day01"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
data, _ := os.ReadFile("day01/input.txt")
|
||||
fmt.Printf("part 1: %d\n", day01.Part1(string(data)))
|
||||
fmt.Printf("part 2: %d\n", day01.Part2(string(data)))
|
||||
elapsed := time.Since(start)
|
||||
fmt.Printf("Execution time: %s\n", elapsed)
|
||||
}
|
||||
Reference in New Issue
Block a user