Compare commits

..

16 Commits

10 changed files with 285 additions and 4 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

@@ -19,15 +19,16 @@ func ParseInput(filepath string) string {
func PartOne(data string) int { func PartOne(data string) int {
doorIDBytes := []byte(data) doorIDBytes := []byte(data)
doorIDLen := len(doorIDBytes)
input := make([]byte, doorIDLen, doorIDLen+20)
copy(input, doorIDBytes)
password := make([]byte, 0, 8) password := make([]byte, 0, 8)
index := 0 index := 0
hexChars := "0123456789abcdef" hexChars := "0123456789abcdef"
var buffer []byte
for len(password) < 8 { for len(password) < 8 {
buffer = strconv.AppendInt(buffer[:0], int64(index), 10) indexBytes := strconv.AppendInt(input[:doorIDLen], int64(index), 10)
input := append(doorIDBytes, buffer...) hash := md5.Sum(indexBytes)
hash := md5.Sum(input)
if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 { if hash[0] == 0 && hash[1] == 0 && hash[2] < 16 {
char := hexChars[hash[2]&0x0F] char := hexChars[hash[2]&0x0F]
@@ -41,5 +42,32 @@ func PartOne(data string) int {
} }
func PartTwo(data string) int { 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 return 0
} }

View File

@@ -32,3 +32,27 @@ func TestPartOne(t *testing.T) {
t.Errorf("PartOne() printed %q, want %q", 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

@@ -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.

File diff suppressed because one or more lines are too long