-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (162 loc) · 3.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"fmt"
"github.com/Alex-Waring/AoC/utils"
)
func part1(input []string) {
defer utils.Timer("part1")()
data := input[0]
diskData := []string{}
id := 0
for i := 0; i < len(data); i++ {
length := utils.IntegerOf(string(data[i]))
if i%2 == 0 {
for j := 0; j < length; j++ {
diskData = append(diskData, fmt.Sprintf("%d", id))
}
id++
} else {
for j := 0; j < length; j++ {
diskData = append(diskData, ".")
}
}
}
compressedData := []string{}
compression_loop:
for {
// If we start with a number, add that list of numbers to the output
if diskData[0] != "." {
compressedData = append(compressedData, diskData[0])
diskData = diskData[1:]
} else {
// Otherwise take the end number
suffix_loop:
for {
if diskData[len(diskData)-1] == "." {
diskData = diskData[:len(diskData)-1]
} else {
break suffix_loop
}
}
compressedData = append(compressedData, diskData[len(diskData)-1])
diskData = diskData[1 : len(diskData)-1]
}
if len(diskData) == 0 {
break compression_loop
}
}
total := 0
for i := 0; i < len(compressedData); i++ {
total += i * utils.IntegerOf(compressedData[i])
}
fmt.Println(total)
}
func part2(input []string) {
defer utils.Timer("part2")()
data := input[0]
type File struct {
size int
ID int
blank bool
}
diskData := []File{}
id := 0
for i := 0; i < len(data); i++ {
length := utils.IntegerOf(string(data[i]))
if i%2 == 0 {
diskData = append(diskData, File{
size: length,
ID: id,
blank: false,
})
id++
} else {
diskData = append(diskData, File{
size: length,
blank: true,
})
}
}
id--
for {
// Find the file with ID
var loc int
var file_to_move File
for i, file := range diskData {
if file.ID == id {
loc = i
file_to_move = file
}
}
if file_to_move.size == 0 {
break
}
// Move up through the compressed data and try and place it
for i, file := range diskData {
if !file.blank {
continue
}
if !(file.size >= file_to_move.size) {
continue
}
// If we've passed the file, move on
if i >= loc {
break
}
// Construct the new list which is:
// start up to space
// moved file
// empty space remaining
// list up until old moved file loc
// empty space for old file
// list past old file loc
new_data := make([]File, 0)
for j := 0; j < i; j++ {
new_data = append(new_data, diskData[j])
}
new_data = append(new_data, file_to_move)
if file_to_move.size < file.size {
new_data = append(new_data, File{
size: file.size - file_to_move.size,
blank: true,
})
}
new_data = append(new_data, diskData[i+1:loc]...)
new_data = append(new_data, File{
size: file_to_move.size,
blank: true,
})
new_data = append(new_data, diskData[loc+1:]...)
diskData = new_data
break
}
if id == 0 {
break
} else {
id--
}
}
compressed_data := []string{}
for _, file := range diskData {
for i := 0; i < file.size; i++ {
if file.blank {
compressed_data = append(compressed_data, ".")
} else {
compressed_data = append(compressed_data, fmt.Sprintf("%d", file.ID))
}
}
}
total := 0
for i := 0; i < len(compressed_data); i++ {
if compressed_data[i] == "." {
continue
}
total += i * utils.IntegerOf(compressed_data[i])
}
fmt.Println(total)
}
func main() {
input := utils.ReadInput("input.txt")
part1(input)
part2(input)
}