Skip to content

Commit c151fa5

Browse files
Merge pull request #4 from amclin/feature/2018-day-13
Feature/2018 day 13 part 2
2 parents 37bda76 + 05b1200 commit c151fa5

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

2018/day-13/solution.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@ const init = (data) => {
1313
const answer = [track.collision.x, track.collision.y]
1414
// console.log(`Track state:`)
1515
// console.log(track.display())
16-
const answer2 = ''
16+
17+
// Execute again, this time, removing crashed carts instead of stopping
18+
const track2 = new Track(data, { removeCrashedCarts: true })
19+
while (track2.carts.length > 1) {
20+
track2.advance()
21+
}
22+
23+
// console.log(track2.display())
24+
const remaining = track2.carts[0]
25+
// console.log(`${remaining.length} cart(s) of ${track2.carts.length} remaining at frame ${track2.frame}`)
26+
// console.log(remaining)
27+
const answer2 = [remaining.x, remaining.y]
28+
1729
console.log(`-- Part 1 --`)
1830
console.log(`Answer: ${answer}`)
1931
console.log(`-- Part 2 --`)

2018/day-13/tracks.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
const { dynamicSortMultiple } = require('../day-04/helpers')
22

33
class Track {
4-
constructor (track) {
4+
constructor (track, options) {
55
this.layout = []
66
this.carts = []
77
this.cartDirections = ['^', '>', 'v', '<']
88
this.collision = false
99
this.frame = 0
1010
this.interSectionOrder = [-1, 0, 1]
11+
this.options = options || {
12+
removeCrashedCarts: false
13+
}
1114
this.trackTurns = ['\\', '/']
1215
this.trackTypes = this.trackTurns.concat(['-', '|', '+'])
1316
this.setLayout(track)
@@ -81,14 +84,16 @@ class Track {
8184
*/
8285
advance () {
8386
this.frame++
84-
this.carts.sort(dynamicSortMultiple('y', 'x')).forEach((c) => {
85-
try {
86-
this.moveCart(c)
87-
} catch (err) {
88-
console.error(`Problem moving cart in frame ${this.frame}`)
89-
console.error(err)
90-
}
91-
})
87+
while (this.carts.filter((c) => c.moved === this.frame).length < this.carts.length) {
88+
this.carts.filter((c) => c.moved !== this.frame).sort(dynamicSortMultiple('y', 'x')).forEach((c) => {
89+
try {
90+
this.moveCart(c)
91+
} catch (err) {
92+
console.error(`Problem moving cart in frame ${this.frame}`)
93+
console.error(err)
94+
}
95+
})
96+
}
9297
}
9398

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

162168
// Make sure cart hasn't run off the rails
@@ -166,17 +172,28 @@ class Track {
166172
// Check for collision
167173
if (this._isCollision(cart.x, cart.y)) {
168174
this.collision = { x: cart.x, y: cart.y }
169-
throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything
175+
console.log(`Collision in frame ${this.frame}. removeCrashedCarts is ${this.options.removeCrashedCarts}`)
176+
177+
// Handle crashed carts
178+
if (this.options.removeCrashedCarts) {
179+
this.carts.filter((c) => c.x === cart.x && c.y === cart.y).forEach((c) => {
180+
this.carts.splice(this.carts.indexOf(c), 1)
181+
})
182+
} else {
183+
throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything
184+
}
170185
}
186+
171187
// rotate the cart when entering a turn
172188
if (this._isTurn(s)) {
173189
cart.direction = this._rotate(s, a, d)
174-
return
190+
return true
175191
}
176192
// rotate (or not) the cart when entering an intersection
177193
if (this._isIntersection(s)) {
178194
cart.direction = this._intersect(cart)
179195
}
196+
return true
180197
}
181198

182199
/**

2018/day-13/tracks.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,33 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
201201
})
202202
})
203203
})
204+
describe('Part 2:', () => {
205+
describe('new Track(layout, options)', () => {
206+
it('removes crashed carts when enabled', () => {
207+
const testData = `/>-<\\
208+
| |
209+
| /<+-\\
210+
| | | v
211+
\\>+</ |
212+
| ^
213+
\\<->/`
214+
const expected = `/---\\
215+
| |
216+
| /-+-\\
217+
| | | |
218+
\\-+-/ ^
219+
| |
220+
\\---/`.trim()
221+
const track = new Track(testData, { removeCrashedCarts: true })
222+
while (track.carts.length > 1) {
223+
track.advance()
224+
}
225+
const actual = track.display().trim()
226+
expect(actual).to.equal(expected)
227+
expect(track.carts[0].x).to.equal(6)
228+
expect(track.carts[0].y).to.equal(4)
229+
expect(track.frame).to.equal(3)
230+
})
231+
})
232+
})
204233
})

0 commit comments

Comments
 (0)
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