Skip to content

Commit

Permalink
day09p2
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 9, 2024
1 parent 6e289ef commit e0c8c87
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
93 changes: 93 additions & 0 deletions day09p2/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package day09p2

import (
"io"
"strconv"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
line := lines[0]

return chksum(defrag(line))
}

func defrag(input string) []int {
var result []int

id := 0

for i, c := range input {
convInt, _ := strconv.Atoi(string(c))
storageblock := i%2 == 0
for j := 0; j < convInt; j++ {
if storageblock {
result = append(result, id)
} else {
result = append(result, -1)
}
}
if storageblock {
id++
}
}

fileCount := id
for fileID := fileCount - 1; fileID >= 0; fileID-- {
fileStart, fileEnd := -1, -1
for i := 0; i < len(result); i++ {
if result[i] == fileID {
if fileStart == -1 {
fileStart = i
}
fileEnd = i
}
}
if fileStart == -1 {
continue
}

fileLength := fileEnd - fileStart + 1

freeStart, freeLength := -1, 0
for i := 0; i < len(result); i++ {
if result[i] == -1 {
if freeStart == -1 {
freeStart = i
}
freeLength++
if freeLength >= fileLength {
break
}
} else {
freeStart = -1
freeLength = 0
}
}

if freeLength >= fileLength && freeStart != -1 && freeStart < fileStart {
for i := 0; i < fileLength; i++ {
result[freeStart+i] = fileID
}
for i := fileStart; i <= fileEnd; i++ {
result[i] = -1
}
}
}

return result
}

func chksum(input []int) int {
sum := 0

for i, id := range input {
if id != -1 {
sum += i * id
}
}

return sum
}
33 changes: 33 additions & 0 deletions day09p2/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day09p2

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `2333133121414131402`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 2858},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}

0 comments on commit e0c8c87

Please sign in to comment.