Skip to content

Commit

Permalink
Implementation for 13b
Browse files Browse the repository at this point in the history
  • Loading branch information
nlowe committed Dec 13, 2018
1 parent 543ba64 commit 1864500
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 48 deletions.
2 changes: 1 addition & 1 deletion aoc2018.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
day10.A,
day11.A, day11.B,
day12.A, day12.B,
day13.A,
day13.A, day13.B,
)

flags := rootCmd.PersistentFlags()
Expand Down
139 changes: 92 additions & 47 deletions day13/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ func (cs cartCollection) sortFunc(i, j int) bool {
c := cs[i]
other := cs[j]

if c == nil {
return false
}

if other == nil {
return true
}

return c.y < other.y || (c.y == other.y && c.x < other.x)
}

Expand All @@ -130,31 +138,60 @@ func (cs cartCollection) cartAt(x, y int) *cart {
return nil
}

func (cs cartCollection) hasCollision() (int, int) {
func (cs cartCollection) getCollidingCarts() []*cart {
var result []*cart

for i := 0; i < len(cs)-1; i++ {
if cs[i] == nil {
continue
}

for j := i + 1; j < len(cs); j++ {
if cs[j] == nil {
continue
}

if cs[i].x == cs[j].x && cs[i].y == cs[j].y {
return cs[i].x, cs[i].y
result = append(result, cs[i], cs[j])
}
}
}

return -1, -1
return result
}

func a(input *util.ChallengeInput) string {
var lines []string
carts, grid := initGrid(input)

for {
cx := -1
cy := -1
isFirstCrash := true
tick(carts, grid, func(collidingCarts cartCollection) bool {
if isFirstCrash {
isFirstCrash = false
cx = collidingCarts[0].x
cy = collidingCarts[0].y
}

return true
})
if cx > 0 && cy > 0 {
return fmt.Sprintf("%d,%d", cx, cy)
}
}
}

func initGrid(input *util.ChallengeInput) (cartCollection, [][]rune) {
var lines []string
w := 0
for line := range input.Lines() {
lines = append(lines, line)
if len(line) > w {
w = len(line)
}
}

var carts cartCollection

grid := make([][]rune, w)
for i := 0; i < w; i++ {
grid[i] = make([]rune, len(lines))
Expand Down Expand Up @@ -183,53 +220,61 @@ func a(input *util.ChallengeInput) string {
}
}
}
return carts, grid
}

for {
// sort carts
sort.Slice(carts, carts.sortFunc)
func tick(carts cartCollection, grid [][]rune, onCollisionCallback func(cartCollection) bool) {
// sort carts
sort.Slice(carts, carts.sortFunc)

for _, cart := range carts {
tile := grid[cart.x][cart.y]
for _, cart := range carts {
if cart == nil {
continue
}
tile := grid[cart.x][cart.y]

if tile == leftRightTrackPiece || tile == upDownTrackPiece {
if tile == leftRightTrackPiece || tile == upDownTrackPiece {
cart.goStraight()
} else if tile == aTurnTrackPiece {
// /
if cart.d == UP {
cart.turnRight()
} else if cart.d == RIGHT {
cart.turnLeft()
} else if cart.d == DOWN {
cart.turnRight()
} else {
cart.turnLeft()
}
} else if tile == bTurnTrackPiece {
// \
if cart.d == UP {
cart.turnLeft()
} else if cart.d == RIGHT {
cart.turnRight()
} else if cart.d == DOWN {
cart.turnLeft()
} else {
cart.turnRight()
}
} else if tile == intersectionTrackPiece {
if cart.nextAction == goLeft {
cart.turnLeft()
} else if cart.nextAction == goStraight {
cart.goStraight()
} else if tile == aTurnTrackPiece {
// /
if cart.d == UP {
cart.turnRight()
} else if cart.d == RIGHT {
cart.turnLeft()
} else if cart.d == DOWN {
cart.turnRight()
} else {
cart.turnLeft()
}
} else if tile == bTurnTrackPiece {
// \
if cart.d == UP {
cart.turnLeft()
} else if cart.d == RIGHT {
cart.turnRight()
} else if cart.d == DOWN {
cart.turnLeft()
} else {
cart.turnRight()
}
} else if tile == intersectionTrackPiece {
if cart.nextAction == goLeft {
cart.turnLeft()
} else if cart.nextAction == goStraight {
cart.goStraight()
} else {
cart.turnRight()
}

cart.nextAction++
cart.nextAction %= 3
} else {
cart.turnRight()
}

if x, y := carts.hasCollision(); x > 0 && y > 0 {
return fmt.Sprintf("%d,%d", x, y)
cart.nextAction++
cart.nextAction %= 3
}

if onCollisionCallback != nil {
if cs := carts.getCollidingCarts(); len(cs) > 0 {
if onCollisionCallback(cs) {
return
}
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions day13/b.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package day13

import (
"fmt"

"github.com/nlowe/aoc2018/util"
"github.com/spf13/cobra"
)

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

func b(input *util.ChallengeInput) string {
carts, grid := initGrid(input)

remainingCarts := len(carts)
for remainingCarts > 1 {
tick(carts, grid, func(cs cartCollection) bool {
for _, c := range cs {
for i, other := range carts {
if c == other {
carts[i] = nil
remainingCarts--
}
}
}

return false
})
}

var x, y int
for _, c := range carts {
if c != nil {
x = c.x
y = c.y
}
}

return fmt.Sprintf("%d,%d", x, y)
}
20 changes: 20 additions & 0 deletions day13/b_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package day13

import (
"testing"

"github.com/nlowe/aoc2018/util"
"github.com/stretchr/testify/require"
)

func TestB(t *testing.T) {
input := util.TestInput(`/>-<\
| |
| /<+-\
| | | v
\>+</ |
| ^
\<->/`)

require.Equal(t, "6,4", b(input))
}

0 comments on commit 1864500

Please sign in to comment.