Skip to content

Commit 2cee91c

Browse files
committed
day18: solved part two
1 parent 238bf5c commit 2cee91c

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

day18/main.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,63 @@ func printSpace(space [][]int) {
1919
var max = len(space) - 1
2020
for y := range space {
2121
for x := range space[y] {
22+
var val string
2223
switch space[y][x] {
2324
case CORRUPTED:
24-
fmt.Print(" C")
25+
val = "C"
2526
case UNIVSITED:
26-
fmt.Print(" .")
27+
val = "."
2728
default:
28-
fmt.Printf("%2d", space[y][x])
29+
val = util.Itoa(space[y][x])
2930
}
3031
if max > 9 {
31-
fmt.Print(" ")
32+
fmt.Printf("%3s ", val)
33+
} else {
34+
fmt.Printf("%2s", val)
3235
}
3336
}
3437
fmt.Println()
3538
}
3639
fmt.Println()
3740
}
3841

39-
func readSpace(space [][]int, count int) {
42+
func readPoints() []util.Point {
4043
var scanner = bufio.NewScanner(os.Stdin)
4144
var pattern = regexp.MustCompile(`^(\d+),(\d+)$`)
4245

43-
var fallen int
44-
for scanner.Scan() && fallen < count {
46+
var points = []util.Point{}
47+
for scanner.Scan() {
4548
var line = scanner.Text()
4649
var matches = pattern.FindStringSubmatch(line)
4750
var x, y int
4851
if len(matches) > 0 {
4952
x = util.Atoi(matches[1])
5053
y = util.Atoi(matches[2])
51-
space[y][x] = CORRUPTED
52-
fallen++
54+
points = append(points, util.Point{Y: y, X: x})
5355
}
5456
}
5557
if err := scanner.Err(); err != nil {
5658
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
5759
os.Exit(1)
5860
}
61+
62+
return points
63+
}
64+
65+
func resetSpace(space [][]int) {
66+
for y := range space {
67+
for x := range space[y] {
68+
if space[y][x] > 0 {
69+
space[y][x] = 0
70+
}
71+
}
72+
}
73+
}
74+
75+
func fillSpace(space [][]int, points []util.Point) {
76+
for _, point := range points {
77+
space[point.Y][point.X] = CORRUPTED
78+
}
5979
}
6080

6181
func makeSpace(size int) [][]int {
@@ -106,10 +126,20 @@ func main() {
106126
var count = flag.Int("count", 1024, "number of bytes that have fallen")
107127
flag.Parse()
108128

129+
var points = readPoints()
109130
var space = makeSpace(*max)
110-
readSpace(space, *count)
131+
fillSpace(space, points[:*count])
111132
printSpace(space)
112133
var steps = searchPath(space) - 1
113134
fmt.Printf("found exit in %d steps\n", steps)
114135
printSpace(space)
136+
137+
for _, point := range points[*count:] {
138+
resetSpace(space)
139+
fillSpace(space, []util.Point{point})
140+
if searchPath(space) == 0 {
141+
fmt.Printf("no way out at coordinate %d,%d\n", point.X, point.Y)
142+
break
143+
}
144+
}
115145
}

0 commit comments

Comments
 (0)