From e0c8c870b0cb65173ed7acbd64f9e6a59e7950c3 Mon Sep 17 00:00:00 2001 From: Simon Date: Mon, 9 Dec 2024 20:24:12 +0100 Subject: [PATCH] day09p2 --- day09p2/solution.go | 93 ++++++++++++++++++++++++++++++++++++++++ day09p2/solution_test.go | 33 ++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 day09p2/solution.go create mode 100644 day09p2/solution_test.go diff --git a/day09p2/solution.go b/day09p2/solution.go new file mode 100644 index 0000000..32c9947 --- /dev/null +++ b/day09p2/solution.go @@ -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 +} diff --git a/day09p2/solution_test.go b/day09p2/solution_test.go new file mode 100644 index 0000000..78551cd --- /dev/null +++ b/day09p2/solution_test.go @@ -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) + } + } +}