Compare commits
22 Commits
bc76283458
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 34be9e0847 | |||
| 6ea67eac0c | |||
| bb366fbe17 | |||
| 681b7bae16 | |||
| c8ded5c42d | |||
| b37f1ec366 | |||
| 40e2e329e0 | |||
| fa5bf2e85b | |||
| ea1b57b17e | |||
| 174671e6f5 | |||
| 40bcf3052f | |||
| 1e634b7ee9 | |||
| 6e625dcf06 | |||
| 141216920d | |||
| b685e81c58 | |||
| 1adc10ea88 | |||
| db7c31cb39 | |||
| 1ad1da1309 | |||
| 228392fe83 | |||
| 4837cbf290 | |||
| 8503cee52b | |||
| b16b052115 |
108
internal/2015/DayFourteen/code.go
Normal file
108
internal/2015/DayFourteen/code.go
Normal 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)
|
||||
}
|
||||
24
internal/2015/DayFourteen/code_test.go
Normal file
24
internal/2015/DayFourteen/code_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
_ "advent-of-code/internal/2015/DayEleven"
|
||||
_ "advent-of-code/internal/2015/DayFive"
|
||||
_ "advent-of-code/internal/2015/DayFour"
|
||||
_ "advent-of-code/internal/2015/DayFourteen"
|
||||
_ "advent-of-code/internal/2015/DayNine"
|
||||
_ "advent-of-code/internal/2015/DayOne"
|
||||
_ "advent-of-code/internal/2015/DaySeven"
|
||||
|
||||
73
internal/2016/DayFive/code.go
Normal file
73
internal/2016/DayFive/code.go
Normal 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
|
||||
}
|
||||
58
internal/2016/DayFive/code_test.go
Normal file
58
internal/2016/DayFive/code_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package year2016
|
||||
|
||||
import (
|
||||
_ "advent-of-code/internal/2016/DayFive"
|
||||
_ "advent-of-code/internal/2016/DayFour"
|
||||
_ "advent-of-code/internal/2016/DayOne"
|
||||
_ "advent-of-code/internal/2016/DayThree"
|
||||
|
||||
64
internal/2018/DayFive/code.go
Normal file
64
internal/2018/DayFive/code.go
Normal 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
|
||||
}
|
||||
21
internal/2018/DayFive/code_test.go
Normal file
21
internal/2018/DayFive/code_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package year2018
|
||||
|
||||
import (
|
||||
_ "advent-of-code/internal/2018/DayFive"
|
||||
_ "advent-of-code/internal/2018/DayFour"
|
||||
_ "advent-of-code/internal/2018/DayOne"
|
||||
_ "advent-of-code/internal/2018/DayThree"
|
||||
|
||||
91
internal/2025/DayTen/code.go
Normal file
91
internal/2025/DayTen/code.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package dayten
|
||||
|
||||
import (
|
||||
"advent-of-code/internal/registry"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.Register("2025D10", ParseInput, PartOne, PartTwo)
|
||||
}
|
||||
|
||||
func ParseInput(filepath string) []string {
|
||||
content, _ := os.ReadFile(filepath)
|
||||
return strings.Split(string(content), "\n")
|
||||
}
|
||||
|
||||
func parseMachine(line string) ([]bool, [][]int) {
|
||||
bracketRegex := regexp.MustCompile(`\[([.#]+)\]`)
|
||||
parenthesisRegex := regexp.MustCompile(`\(([^)]+)\)`)
|
||||
|
||||
bracketMatch := bracketRegex.FindStringSubmatch(line)
|
||||
targetString := bracketMatch[1]
|
||||
target := make([]bool, len(targetString))
|
||||
for idx, char := range targetString {
|
||||
target[idx] = char == '#'
|
||||
}
|
||||
|
||||
parenthesisMatches := parenthesisRegex.FindAllStringSubmatch(line, -1)
|
||||
buttons := make([][]int, 0, len(parenthesisMatches))
|
||||
|
||||
for _, match := range parenthesisMatches {
|
||||
buttonString := match[1]
|
||||
parts := strings.Split(buttonString, ",")
|
||||
button := make([]int, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
light, _ := strconv.Atoi(part)
|
||||
button = append(button, light)
|
||||
}
|
||||
buttons = append(buttons, button)
|
||||
}
|
||||
|
||||
return target, buttons
|
||||
}
|
||||
|
||||
func PartOne(data []string) int {
|
||||
total := 0
|
||||
for _, line := range data {
|
||||
target, buttons := parseMachine(line)
|
||||
numberOfLights := len(target)
|
||||
numberOfButtons := len(buttons)
|
||||
minimumPresses := numberOfButtons + 1
|
||||
|
||||
for mask := 0; mask < (1 << numberOfButtons); mask++ {
|
||||
lights := make([]bool, numberOfLights)
|
||||
presses := 0
|
||||
|
||||
for buttonIdx := range numberOfButtons {
|
||||
if mask&(1<<buttonIdx) != 0 {
|
||||
presses++
|
||||
for _, light := range buttons[buttonIdx] {
|
||||
if light < numberOfLights {
|
||||
lights[light] = !lights[light]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
matches := true
|
||||
for idx := range numberOfLights {
|
||||
if lights[idx] != target[idx] {
|
||||
matches = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if matches && presses < minimumPresses {
|
||||
minimumPresses = presses
|
||||
}
|
||||
}
|
||||
total += minimumPresses
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func PartTwo(data []string) int {
|
||||
return 0
|
||||
}
|
||||
@@ -15,3 +15,11 @@ func TestPartOne(t *testing.T) {
|
||||
t.Errorf("PartOne() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartTwo(t *testing.T) {
|
||||
expected := 33
|
||||
got := PartTwo(testInput)
|
||||
if got != expected {
|
||||
t.Errorf("PartTwo() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
9
internal/data/2015/DayFourteen/input.txt
Normal file
9
internal/data/2015/DayFourteen/input.txt
Normal 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.
|
||||
1
internal/data/2016/DayFive/input.txt
Normal file
1
internal/data/2016/DayFive/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
reyedfim
|
||||
1
internal/data/2018/DayFive/input.txt
Normal file
1
internal/data/2018/DayFive/input.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user