Skip to content

Commit efab4cb

Browse files
committed
Part 2 day 14
1 parent 5f9f72c commit efab4cb

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
input
2+
output
23
node_modules
34
go.work.sum

2024/go/day14/main.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,19 @@ func partTwo(contents []string) {
122122

123123
cacheMap := make(map[robotStruct][2]int)
124124

125-
seconds := 10_000
126-
eightInARowSecond := 0
125+
seconds := 10_000 // Try 10,000
126+
127+
// We'll output each second into a file ans then grep for when we have lots of robots in a line, hopefully this works
128+
fo, err := os.Create("output")
129+
if err != nil {
130+
panic(err)
131+
}
132+
// close fo on exit and check for its returned error
133+
defer func() {
134+
if err := fo.Close(); err != nil {
135+
panic(err)
136+
}
137+
}()
127138

128139
for i := 1; i <= seconds; i++ {
129140
for j, robot := range robots {
@@ -142,32 +153,14 @@ func partTwo(contents []string) {
142153

143154
}
144155

145-
// Let's see if we have 8 in a line, feels a bit arbitrary but might work?
146156
grid := buildMap(rows, columns, robots)
147-
for i := 0; i < rows; i++ {
148-
numInRow := 0
149-
for j := 0; j < columns; j++ {
150-
if grid[i][j] == '0' {
151-
numInRow++
152-
} else if numInRow > 0 {
153-
break
154-
}
155-
156-
if numInRow >= 8 {
157-
eightInARowSecond = seconds
158-
}
159-
}
160-
161-
if eightInARowSecond > 0 {
162-
break
163-
}
164-
}
157+
outputMap(&grid, fo, i)
165158
}
166159
// Not really sure how to do this one... maybe just output
167160
sharedstruct.PrintOutput(sharedstruct.Output{
168161
Day: 14,
169162
Part: 2,
170-
Value: eightInARowSecond,
163+
Value: "Run grep on the output and look for the tree! `grep 00000000000000000000 ./output -C 10`",
171164
})
172165
}
173166

@@ -214,6 +207,20 @@ func buildMap(rows int, columns int, robots []robotStruct) [][]byte {
214207

215208
}
216209

210+
func outputMap(grid *[][]byte, fo *os.File, seconds int) {
211+
fo.WriteString("Seconds: " + strconv.Itoa(seconds))
212+
fo.WriteString("\n")
213+
214+
for _, line := range *grid {
215+
// write a chunk
216+
if _, err := fo.Write(line); err != nil {
217+
panic(err)
218+
}
219+
220+
fo.WriteString("\n")
221+
}
222+
}
223+
217224
func displayMap(rows int, columns int, robots []robotStruct) {
218225
grid := buildMap(rows, columns, robots)
219226

0 commit comments

Comments
 (0)