Skip to content

Commit

Permalink
Merge pull request #4 from amclin/feature/2018-day-13
Browse files Browse the repository at this point in the history
Feature/2018 day 13 part 2
  • Loading branch information
amclin authored Dec 22, 2018
2 parents 37bda76 + 05b1200 commit c151fa5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
14 changes: 13 additions & 1 deletion 2018/day-13/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ const init = (data) => {
const answer = [track.collision.x, track.collision.y]
// console.log(`Track state:`)
// console.log(track.display())
const answer2 = ''

// Execute again, this time, removing crashed carts instead of stopping
const track2 = new Track(data, { removeCrashedCarts: true })
while (track2.carts.length > 1) {
track2.advance()
}

// console.log(track2.display())
const remaining = track2.carts[0]
// console.log(`${remaining.length} cart(s) of ${track2.carts.length} remaining at frame ${track2.frame}`)
// console.log(remaining)
const answer2 = [remaining.x, remaining.y]

console.log(`-- Part 1 --`)
console.log(`Answer: ${answer}`)
console.log(`-- Part 2 --`)
Expand Down
39 changes: 28 additions & 11 deletions 2018/day-13/tracks.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const { dynamicSortMultiple } = require('../day-04/helpers')

class Track {
constructor (track) {
constructor (track, options) {
this.layout = []
this.carts = []
this.cartDirections = ['^', '>', 'v', '<']
this.collision = false
this.frame = 0
this.interSectionOrder = [-1, 0, 1]
this.options = options || {
removeCrashedCarts: false
}
this.trackTurns = ['\\', '/']
this.trackTypes = this.trackTurns.concat(['-', '|', '+'])
this.setLayout(track)
Expand Down Expand Up @@ -81,14 +84,16 @@ class Track {
*/
advance () {
this.frame++
this.carts.sort(dynamicSortMultiple('y', 'x')).forEach((c) => {
try {
this.moveCart(c)
} catch (err) {
console.error(`Problem moving cart in frame ${this.frame}`)
console.error(err)
}
})
while (this.carts.filter((c) => c.moved === this.frame).length < this.carts.length) {
this.carts.filter((c) => c.moved !== this.frame).sort(dynamicSortMultiple('y', 'x')).forEach((c) => {
try {
this.moveCart(c)
} catch (err) {
console.error(`Problem moving cart in frame ${this.frame}`)
console.error(err)
}
})
}
}

/**
Expand Down Expand Up @@ -157,6 +162,7 @@ class Track {
const l = (d % 3 === 0) ? -1 : 1 // (+/-) distance of travel on the axis
// move the cart
cart[a] = cart[a] + l
cart.moved = this.frame
const s = this.getSegment(cart.x, cart.y) // Segment of track the cart is now on

// Make sure cart hasn't run off the rails
Expand All @@ -166,17 +172,28 @@ class Track {
// Check for collision
if (this._isCollision(cart.x, cart.y)) {
this.collision = { x: cart.x, y: cart.y }
throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything
console.log(`Collision in frame ${this.frame}. removeCrashedCarts is ${this.options.removeCrashedCarts}`)

// Handle crashed carts
if (this.options.removeCrashedCarts) {
this.carts.filter((c) => c.x === cart.x && c.y === cart.y).forEach((c) => {
this.carts.splice(this.carts.indexOf(c), 1)
})
} else {
throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything
}
}

// rotate the cart when entering a turn
if (this._isTurn(s)) {
cart.direction = this._rotate(s, a, d)
return
return true
}
// rotate (or not) the cart when entering an intersection
if (this._isIntersection(s)) {
cart.direction = this._intersect(cart)
}
return true
}

/**
Expand Down
29 changes: 29 additions & 0 deletions 2018/day-13/tracks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,33 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
})
})
})
describe('Part 2:', () => {
describe('new Track(layout, options)', () => {
it('removes crashed carts when enabled', () => {
const testData = `/>-<\\
| |
| /<+-\\
| | | v
\\>+</ |
| ^
\\<->/`
const expected = `/---\\
| |
| /-+-\\
| | | |
\\-+-/ ^
| |
\\---/`.trim()
const track = new Track(testData, { removeCrashedCarts: true })
while (track.carts.length > 1) {
track.advance()
}
const actual = track.display().trim()
expect(actual).to.equal(expected)
expect(track.carts[0].x).to.equal(6)
expect(track.carts[0].y).to.equal(4)
expect(track.frame).to.equal(3)
})
})
})
})

0 comments on commit c151fa5

Please sign in to comment.