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 8558a42 commit 9d61b27
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 13 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
118 changes: 106 additions & 12 deletions 2023/src/tasks/day14/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,124 @@ 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)
func Task2(data string) (result framework.Result[int]) {
rocks := framework.CharLines(data)

for y, line := range lines {
for x, char := range line {
switch char {
lo.RepeatBy(1000, func(index int) bool {
moveNorth(&rocks)
moveWest(&rocks)
moveSouth(&rocks)
moveEast(&rocks)
return true
})
result.Value = calculateLoad(&rocks)

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 := make([]int, width)
for i := 0; i < width; i++ {
limits[i] = height - 1
}

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 := make([]int, height)
for i := 0; i < height; i++ {
limits[i] = width - 1
}

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 TestDay13(t *testing.T) {
Expand Down

0 comments on commit 9d61b27

Please sign in to comment.