Compare commits
9 Commits
db7c31cb39
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| fa5bf2e85b | |||
| ea1b57b17e | |||
| 174671e6f5 | |||
| 40bcf3052f | |||
| 1e634b7ee9 | |||
| 6e625dcf06 | |||
| 141216920d | |||
| b685e81c58 | |||
| 1adc10ea88 |
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"
|
||||
|
||||
@@ -19,15 +19,16 @@ func ParseInput(filepath string) string {
|
||||
|
||||
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"
|
||||
var buffer []byte
|
||||
|
||||
for len(password) < 8 {
|
||||
buffer = strconv.AppendInt(buffer[:0], int64(index), 10)
|
||||
input := append(doorIDBytes, buffer...)
|
||||
hash := md5.Sum(input)
|
||||
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]
|
||||
@@ -41,5 +42,32 @@ func PartOne(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
|
||||
}
|
||||
|
||||
@@ -32,3 +32,27 @@ func TestPartOne(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
Reference in New Issue
Block a user