Compare commits

...

20 Commits

Author SHA1 Message Date
34be9e0847 feat: solve part two using exhaustive search 2025-12-20 09:25:03 +01:00
6ea67eac0c test: add unit test for part two 2025-12-20 09:18:32 +01:00
bb366fbe17 refactor: extract reactPolymer to reuse it in PartTwo 2025-12-20 09:18:17 +01:00
681b7bae16 feat: solve part one 2025-12-20 09:15:52 +01:00
c8ded5c42d test: add unit test for part one 2025-12-20 09:12:51 +01:00
b37f1ec366 chore: add 20218D5 dataset 2025-12-20 09:08:46 +01:00
40e2e329e0 feat: register fifth day 2025-12-20 09:08:34 +01:00
fa5bf2e85b feat: refactor some code and solve part two 2025-12-14 10:14:18 +01:00
ea1b57b17e test: add unit test for part two 2025-12-14 10:10:30 +01:00
174671e6f5 feat: use min() and get rid of Reindeer struct 2025-12-14 10:02:57 +01:00
40bcf3052f test: add unit test for part one 2025-12-14 10:00:17 +01:00
1e634b7ee9 feat: implement partone in CalculateMaxDistance to make the test relevant 2025-12-14 10:00:11 +01:00
6e625dcf06 feat: register fourteenth day 2025-12-14 09:52:39 +01:00
141216920d chore: add 2015D14 dataset 2025-12-14 09:52:25 +01:00
b685e81c58 feat: solve part two 2025-12-12 18:35:11 +01:00
1adc10ea88 test: add test for part two 2025-12-12 18:35:05 +01:00
db7c31cb39 feat: solve part one using direct byte comparaison and efficient hex extraction 2025-12-12 18:30:25 +01:00
1ad1da1309 chore: add 2016D5 dataset 2025-12-12 18:30:07 +01:00
228392fe83 feat: register fifth day 2025-12-12 18:29:50 +01:00
4837cbf290 test: add unit test for part one 2025-12-12 18:29:20 +01:00
12 changed files with 362 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
package dayfourteen
import (
"advent-of-code/internal/registry"
"os"
"regexp"
"strconv"
"strings"
)
var reindeerPattern = regexp.MustCompile(`\w+ can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds\.`)
const raceTime = 2503
type Reindeer struct {
Speed int
FlyTime int
RestTime int
Points int
}
func init() {
registry.Register("2015D14", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) []string {
content, _ := os.ReadFile(filepath)
return strings.Split(string(content), "\n")
}
func parseReindeer(line string) Reindeer {
matches := reindeerPattern.FindStringSubmatch(line)
speed, _ := strconv.Atoi(matches[1])
flyTime, _ := strconv.Atoi(matches[2])
restTime, _ := strconv.Atoi(matches[3])
return Reindeer{Speed: speed, FlyTime: flyTime, RestTime: restTime}
}
func parseReindeers(data []string) []Reindeer {
reindeers := make([]Reindeer, 0, len(data))
for _, line := range data {
reindeers = append(reindeers, parseReindeer(line))
}
return reindeers
}
func (reindeer Reindeer) distanceAtTime(time int) int {
cycleTime := reindeer.FlyTime + reindeer.RestTime
fullCycles := time / cycleTime
distance := fullCycles * reindeer.Speed * reindeer.FlyTime
remainingTime := time % cycleTime
distance += reindeer.Speed * min(remainingTime, reindeer.FlyTime)
return distance
}
func calculateMaxDistance(data []string, time int) int {
reindeers := parseReindeers(data)
maxDistance := 0
for _, reindeer := range reindeers {
distance := reindeer.distanceAtTime(time)
if distance > maxDistance {
maxDistance = distance
}
}
return maxDistance
}
func calculateMaxPoints(data []string, time int) int {
reindeers := parseReindeers(data)
for second := 1; second <= time; second++ {
maxDistance := 0
distances := make([]int, len(reindeers))
for idx := range reindeers {
distance := reindeers[idx].distanceAtTime(second)
distances[idx] = distance
if distance > maxDistance {
maxDistance = distance
}
}
for idx := range reindeers {
if distances[idx] == maxDistance {
reindeers[idx].Points++
}
}
}
maxPoints := 0
for idx := range reindeers {
if reindeers[idx].Points > maxPoints {
maxPoints = reindeers[idx].Points
}
}
return maxPoints
}
func PartOne(data []string) int {
return calculateMaxDistance(data, raceTime)
}
func PartTwo(data []string) int {
return calculateMaxPoints(data, raceTime)
}

View File

@@ -0,0 +1,24 @@
package dayfourteen
import "testing"
var testInput = []string{
"Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
"Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.",
}
func TestPartOne(t *testing.T) {
expected := 1120
got := calculateMaxDistance(testInput, 1000)
if got != expected {
t.Errorf("calculateMaxDistance(testInput, 1000) = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 689
got := calculateMaxPoints(testInput, 1000)
if got != expected {
t.Errorf("calculateMaxPoints(testInput, 1000) = %d, want %d", got, expected)
}
}

View File

@@ -5,6 +5,7 @@ import (
_ "advent-of-code/internal/2015/DayEleven" _ "advent-of-code/internal/2015/DayEleven"
_ "advent-of-code/internal/2015/DayFive" _ "advent-of-code/internal/2015/DayFive"
_ "advent-of-code/internal/2015/DayFour" _ "advent-of-code/internal/2015/DayFour"
_ "advent-of-code/internal/2015/DayFourteen"
_ "advent-of-code/internal/2015/DayNine" _ "advent-of-code/internal/2015/DayNine"
_ "advent-of-code/internal/2015/DayOne" _ "advent-of-code/internal/2015/DayOne"
_ "advent-of-code/internal/2015/DaySeven" _ "advent-of-code/internal/2015/DaySeven"

View File

@@ -0,0 +1,73 @@
package dayfive
import (
"advent-of-code/internal/registry"
"crypto/md5"
"fmt"
"os"
"strconv"
)
func init() {
registry.Register("2016D5", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) string {
content, _ := os.ReadFile(filepath)
return string(content)
}
func PartOne(data string) int {
doorIDBytes := []byte(data)
doorIDLen := len(doorIDBytes)
input := make([]byte, doorIDLen, doorIDLen+20)
copy(input, doorIDBytes)
password := make([]byte, 0, 8)
index := 0
hexChars := "0123456789abcdef"
for len(password) < 8 {
indexBytes := strconv.AppendInt(input[:doorIDLen], int64(index), 10)
hash := md5.Sum(indexBytes)
if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 {
char := hexChars[hash[2]&0x0F]
password = append(password, char)
}
index++
}
fmt.Println(string(password))
return 0
}
func PartTwo(data string) int {
doorIDBytes := []byte(data)
doorIDLen := len(doorIDBytes)
input := make([]byte, doorIDLen, doorIDLen+20)
copy(input, doorIDBytes)
password := make([]byte, 8)
filled := make([]bool, 8)
filledCount := 0
index := 0
hexChars := "0123456789abcdef"
for filledCount < 8 {
indexBytes := strconv.AppendInt(input[:doorIDLen], int64(index), 10)
hash := md5.Sum(indexBytes)
if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 {
position := int(hash[2] & 0x0F)
if position < 8 && !filled[position] {
char := hexChars[hash[3]>>4]
password[position] = char
filled[position] = true
filledCount++
}
}
index++
}
fmt.Println(string(password))
return 0
}

View File

@@ -0,0 +1,58 @@
package dayfive
import (
"bytes"
"os"
"strings"
"testing"
)
var testInput = "abc"
func TestPartOne(t *testing.T) {
expected := "18f47a30"
oldStdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
PartOne(testInput)
_ = w.Close()
os.Stdout = oldStdout
var buffer bytes.Buffer
_, _ = buffer.ReadFrom(r)
got := strings.TrimSpace(buffer.String())
if got != expected {
t.Errorf("PartOne() printed %q, want %q", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := "05ace8e3"
oldStdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
PartTwo(testInput)
_ = w.Close()
os.Stdout = oldStdout
var buffer bytes.Buffer
_, _ = buffer.ReadFrom(r)
got := strings.TrimSpace(buffer.String())
if got != expected {
t.Errorf("PartTwo() printed %q, want %q", got, expected)
}
}

View File

@@ -1,6 +1,7 @@
package year2016 package year2016
import ( import (
_ "advent-of-code/internal/2016/DayFive"
_ "advent-of-code/internal/2016/DayFour" _ "advent-of-code/internal/2016/DayFour"
_ "advent-of-code/internal/2016/DayOne" _ "advent-of-code/internal/2016/DayOne"
_ "advent-of-code/internal/2016/DayThree" _ "advent-of-code/internal/2016/DayThree"

View File

@@ -0,0 +1,64 @@
package dayfive
import (
"advent-of-code/internal/registry"
"os"
"unicode"
)
func init() {
registry.Register("2018D5", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) string {
content, _ := os.ReadFile(filepath)
return string(content)
}
func reactPolymer(data string) int {
stack := []rune{}
for _, char := range data {
if len(stack) == 0 {
stack = append(stack, char)
continue
}
top := stack[len(stack)-1]
if unicode.ToLower(top) == unicode.ToLower(char) && top != char {
stack = stack[:len(stack)-1]
} else {
stack = append(stack, char)
}
}
return len(stack)
}
func PartOne(data string) int {
return reactPolymer(data)
}
func PartTwo(data string) int {
unitTypes := make(map[rune]bool)
for _, char := range data {
unitTypes[unicode.ToLower(char)] = true
}
shortestPolymerLength := len(data)
for unitType := range unitTypes {
filtered := make([]rune, 0, len(data))
for _, char := range data {
if unicode.ToLower(char) != unitType {
filtered = append(filtered, char)
}
}
length := reactPolymer(string(filtered))
if length < shortestPolymerLength {
shortestPolymerLength = length
}
}
return shortestPolymerLength
}

View File

@@ -0,0 +1,21 @@
package dayfive
import "testing"
var testInput = "dabAcCaCBAcCcaDA"
func TestPartOne(t *testing.T) {
expected := 10
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 4
got := PartTwo(testInput)
if got != expected {
t.Errorf("PartTwo() = %d, want %d", got, expected)
}
}

View File

@@ -1,6 +1,7 @@
package year2018 package year2018
import ( import (
_ "advent-of-code/internal/2018/DayFive"
_ "advent-of-code/internal/2018/DayFour" _ "advent-of-code/internal/2018/DayFour"
_ "advent-of-code/internal/2018/DayOne" _ "advent-of-code/internal/2018/DayOne"
_ "advent-of-code/internal/2018/DayThree" _ "advent-of-code/internal/2018/DayThree"

View File

@@ -0,0 +1,9 @@
Vixen can fly 19 km/s for 7 seconds, but then must rest for 124 seconds.
Rudolph can fly 3 km/s for 15 seconds, but then must rest for 28 seconds.
Donner can fly 19 km/s for 9 seconds, but then must rest for 164 seconds.
Blitzen can fly 19 km/s for 9 seconds, but then must rest for 158 seconds.
Comet can fly 13 km/s for 7 seconds, but then must rest for 82 seconds.
Cupid can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
Dasher can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
Dancer can fly 3 km/s for 16 seconds, but then must rest for 37 seconds.
Prancer can fly 25 km/s for 6 seconds, but then must rest for 143 seconds.

View File

@@ -0,0 +1 @@
reyedfim

File diff suppressed because one or more lines are too long