Skip to content

Commit 3a56d73

Browse files
feature(2018 day-13): support removing carts when crashed
1 parent 375135b commit 3a56d73

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

2018/day-13/solution.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ 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.filter((c) => c.ghost !== true).length > 1 && track2.frame < 10) {
20+
track2.advance()
21+
}
22+
23+
console.log(`Only one cart remaining at frame ${track2.frame}`)
24+
// console.log(track2.display())
25+
const remaining = track2.carts.find((c) => c.ghost !== true)
26+
const answer2 = [remaining.x, remaining.y]
27+
1728
console.log(`-- Part 1 --`)
1829
console.log(`Answer: ${answer}`)
1930
console.log(`-- Part 2 --`)

2018/day-13/tracks.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
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)
1417
}
1518

16-
_isCollision (x, y) { return (this.carts.filter((c) => c.x === x && c.y === y).length > 1) }
19+
_isCollision (x, y) { return (this.carts.filter((c) => c.x === x && c.y === y && c.ghost !== true).length > 1) }
1720
_isIntersection (s) { return s === '+' }
1821
_isTurn (s) { return this.trackTurns.indexOf(s) >= 0 }
1922

@@ -98,7 +101,7 @@ class Track {
98101
let output = ''
99102
const layout = JSON.parse(JSON.stringify(this.layout)) // Deep copy
100103
// Include the carts
101-
this.carts.forEach((cart) => {
104+
this.carts.filter((c) => c.ghost !== true).forEach((cart) => {
102105
// If another cart is at the spot, draw a collision instead
103106
if (this.cartDirections.indexOf(layout[cart.y][cart.x]) >= 0) {
104107
layout[cart.y][cart.x] = 'X'
@@ -166,17 +169,26 @@ class Track {
166169
// Check for collision
167170
if (this._isCollision(cart.x, cart.y)) {
168171
this.collision = { x: cart.x, y: cart.y }
169-
throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything
172+
173+
if (!this.options.removeCrashedCarts) {
174+
throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything
175+
}
176+
this.carts.filter((c) => c.x === cart.x && c.y === cart.y).forEach((c) => {
177+
c.ghost = true // Ghost carts are dead and no longer collide
178+
// we leave them in the array so that it doesn't mess up the loops
179+
// necessary to finish out each cycle tick
180+
})
170181
}
171182
// rotate the cart when entering a turn
172183
if (this._isTurn(s)) {
173184
cart.direction = this._rotate(s, a, d)
174-
return
185+
return true
175186
}
176187
// rotate (or not) the cart when entering an intersection
177188
if (this._isIntersection(s)) {
178189
cart.direction = this._intersect(cart)
179190
}
191+
return true
180192
}
181193

182194
/**

0 commit comments

Comments
 (0)