Skip to content

Commit

Permalink
2023 14-2
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVlaanderen committed Dec 14, 2023
1 parent a7e3d18 commit 8edb0db
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 15 deletions.
11 changes: 10 additions & 1 deletion 2023/src/framework/data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package framework

import "strings"
import (
"github.com/samber/lo"
"strings"
)

func CharLines(data string) [][]rune {
return lo.Map(strings.Split(data, "\n"), func(item string, index int) []rune {
return []rune(item)
})
}

func Lines(data string) []string {
return strings.Split(data, "\n")
Expand Down
10 changes: 10 additions & 0 deletions 2023/src/framework/hash.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package framework

import (
"bytes"
"crypto/sha512"
"fmt"
)
Expand All @@ -10,3 +11,12 @@ type Hash64 [64]byte
func ComputeHash64(data interface{}) Hash64 {
return sha512.Sum512([]byte(fmt.Sprint(data)))
}

func ComputeSliceHash64[T any](list []T) [64]byte {
var buffer bytes.Buffer
for i, _ := range list {
buffer.WriteString(fmt.Sprintf("%v", list[i]))
buffer.WriteString(",")
}
return sha512.Sum512([]byte(buffer.String()))
}
5 changes: 3 additions & 2 deletions 2023/src/framework/range.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package framework

func Range(start, count, step int) (result []int) {
func Range(start, count, step int) []int {
result := make([]int, 0, count)
for i := 0; i < count; i++ {
result = append(result, start)
start += step
}
return
return result
}

func RangeGen(start, count, step int) <-chan int {
Expand Down
138 changes: 126 additions & 12 deletions 2023/src/tasks/day14/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,144 @@ package day14

import (
"2023/src/framework"
"github.com/samber/lo"
)

func Task1(data string) (result framework.Result[int]) {
lines := framework.Lines(data)
rocks := framework.CharLines(data)

result.Value = calculateLoad(lines)
moveNorth(&rocks)
result.Value = calculateLoad(&rocks)

return
}

func calculateLoad(lines []string) (result int) {
height := len(lines)
highest := framework.Range(height, len(lines[0]), 0)
const MaxIterations = 1000000000

for y, line := range lines {
for x, char := range line {
switch char {
func Task2(data string) (result framework.Result[int]) {
rocks := framework.CharLines(data)

var previousHashes = make([]framework.Hash64, 0)
var previousScores = make([]int, 0)

for i := 0; i < MaxIterations; i++ {
moveNorth(&rocks)
moveWest(&rocks)
moveSouth(&rocks)
moveEast(&rocks)

hash := framework.ComputeSliceHash64(lo.Map(rocks, func(item []rune, index int) string {
return string(item)
}))

startOfPeriod := lo.IndexOf(previousHashes, hash)
if startOfPeriod >= 0 {
sizeOfPeriod := i - startOfPeriod
target := (MaxIterations % sizeOfPeriod) - 1

if target > startOfPeriod {
result.Value = previousScores[target]
} else {
result.Value = previousScores[target+sizeOfPeriod]
}

break
}

score := calculateLoad(&rocks)

previousHashes = append(previousHashes, hash)
previousScores = append(previousScores, score)
}

return
}

func calculateLoad(data *[][]rune) (result int) {
height := len(*data)

for y, line := range *data {
for _, char := range line {
if char == 'O' {
result += height - y
}
}
}
return
}

func moveNorth(data *[][]rune) {
width := len((*data)[0])
height := len(*data)
limits := make([]int, width)

for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
switch (*data)[y][x] {
case 'O':
result += highest[x]
highest[x] -= 1
(*data)[y][x] = '.'
(*data)[limits[x]][x] = 'O'
limits[x] += 1
case '#':
highest[x] = height - y - 1
limits[x] = y + 1
}
}
}
}

func moveWest(data *[][]rune) {
width := len((*data)[0])
height := len(*data)
limits := make([]int, height)

for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
switch (*data)[y][x] {
case 'O':
(*data)[y][x] = '.'
(*data)[y][limits[y]] = 'O'
limits[y] += 1
case '#':
limits[y] = x + 1
}
}
}
}

func moveSouth(data *[][]rune) {
width := len((*data)[0])
height := len(*data)
limits := framework.Range(height-1, width, 0)

for y := height - 1; y >= 0; y-- {
for x := 0; x < width; x++ {
switch (*data)[y][x] {
case 'O':
(*data)[y][x] = '.'
(*data)[limits[x]][x] = 'O'
limits[x] -= 1
case '#':
limits[x] = y - 1
}
}
}
}

func moveEast(data *[][]rune) {
width := len((*data)[0])
height := len(*data)
limits := framework.Range(width-1, height, 0)

for x := width - 1; x >= 0; x-- {
for y := 0; y < height; y++ {
switch (*data)[y][x] {
case 'O':
(*data)[y][x] = '.'
(*data)[y][limits[y]] = 'O'
limits[y] -= 1
case '#':
limits[y] = x - 1
}
}
}
return
}
7 changes: 7 additions & 0 deletions 2023/src/tasks/day14/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ var taskDefinitions = []_testing.TaskDefinition[int]{
{"day14", 113525, _testing.RealData},
},
},
{
Task: Task2,
Tests: []_testing.TestDefinition[int]{
{"data", 64, _testing.TestData},
{"day14", 101292, _testing.RealData},
},
},
}

func TestDay14(t *testing.T) {
Expand Down

0 comments on commit 8edb0db

Please sign in to comment.