Skip to content

Commit

Permalink
Implementation for 15b
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 16, 2019
1 parent 51703a1 commit 762d1c9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
56 changes: 56 additions & 0 deletions challenge/day15/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package day15

import (
"fmt"

"github.com/nlowe/aoc2019/challenge"
"github.com/spf13/cobra"
)

var B = &cobra.Command{
Use: "15b",
Short: "Day 15, Problem B",
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("Answer: %d\n", b(challenge.FromFile()))
},
}

func b(challenge *challenge.Input) int {
s, _, _ := mapShip(challenge)

m := 0
for tryFill(s) {
m++
}

return m
}

func tryFill(s *ship) bool {
var toFill []*tile
s.each(func(t *tile) {
if t.t != statusOxygenSystemFound {
return
}

for _, delta := range []struct {
x int
y int
}{
{-1, 0},
{1, 0},
{0, -1},
{0, 1},
} {
if candidate := s.tileAt(t.x+delta.x, t.y+delta.y); candidate != nil && candidate.t == statusOk {
toFill = append(toFill, candidate)
}
}
})

for _, t := range toFill {
t.t = statusOxygenSystemFound
}

return len(toFill) > 0
}
8 changes: 8 additions & 0 deletions challenge/day15/ship.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ func (s *ship) tileAt(x, y int) *tile {

return s.m[x][y]
}

func (s *ship) each(f func(t *tile)) {
for _, r := range s.m {
for _, t := range r {
f(t)
}
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func init() {
day12.A, day12.B,
day13.A, day13.B,
day14.A, day14.B,
day15.A,
day15.A, day15.B,
)

flags := rootCmd.PersistentFlags()
Expand Down

0 comments on commit 762d1c9

Please sign in to comment.