Skip to content

Feature/2018 day 13 part 2 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})
})
})
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy