Skip to content

Commit b35ab0b

Browse files
committed
solve day 18 part two
1 parent 08cf51f commit b35ab0b

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

day_18/main.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ func main() {
5151
log.Fatal(err)
5252
}
5353

54-
fmt.Println(PartOne(bytePositions))
54+
fmt.Println("solution to part one: ", PartOne(bytePositions))
55+
fmt.Println("solution to part two: ", PartTwo(bytePositions))
5556
}
5657

5758
func PartOne(bytePositions [][2]int) int {
@@ -100,3 +101,61 @@ func PartOne(bytePositions [][2]int) int {
100101

101102
return minSteps
102103
}
104+
105+
func PartTwo(bytePositions [][2]int) string {
106+
corrupted := make(map[[2]int]struct{})
107+
n := 1 << 10 // 1024
108+
gridSize := 71
109+
end := [2]int{gridSize - 1, gridSize - 1}
110+
111+
for i := range n {
112+
bytePos := bytePositions[i]
113+
corrupted[bytePos] = struct{}{}
114+
}
115+
116+
var blockingByte [2]int
117+
for ; n < len(bytePositions); n++ {
118+
seen := make(map[[2]int]struct{})
119+
queue := [][3]int{{}}
120+
bytePos := bytePositions[n]
121+
corrupted[bytePos] = struct{}{}
122+
123+
var minSteps int
124+
for len(queue) > 0 {
125+
curr := queue[0]
126+
pos := [2]int{curr[X], curr[Y]}
127+
queue = queue[1:]
128+
129+
if _, ok := seen[pos]; ok {
130+
continue
131+
}
132+
133+
if _, ok := corrupted[pos]; ok {
134+
continue
135+
}
136+
137+
if pos[X] < 0 || pos[X] >= gridSize || pos[Y] < 0 || pos[Y] >= gridSize {
138+
continue
139+
}
140+
141+
if pos == end {
142+
minSteps = curr[Steps]
143+
break
144+
}
145+
146+
for _, dir := range directions {
147+
queue = append(queue, [3]int{curr[X] + dir[X], curr[Y] + dir[Y], curr[Steps] + 1})
148+
}
149+
150+
seen[pos] = struct{}{}
151+
}
152+
153+
// we never reached the end square
154+
if minSteps == 0 {
155+
blockingByte = bytePos
156+
break
157+
}
158+
}
159+
160+
return fmt.Sprintf("%d,%d", blockingByte[X], blockingByte[Y])
161+
}

0 commit comments

Comments
 (0)