Skip to content

Commit 9b9f3cf

Browse files
fix(2018 day-13): correct order for intersections is left, straight, right, repeat
1 parent 5b2e83f commit 9b9f3cf

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

2018/day-13/tracks.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Track {
77
this.cartDirections = ['^', '>', 'v', '<']
88
this.collision = false
99
this.frame = 0
10+
this.interSectionOrder = [-1, 0, 1]
1011
this.trackTurns = ['\\', '/']
1112
this.trackTypes = this.trackTurns.concat(['-', '|', '+'])
1213
this.setLayout(track)
@@ -18,14 +19,19 @@ class Track {
1819

1920
/**
2021
* Determines the next direction for a cart rotating at an intersection
21-
* Order of rotations is left (counterclockwise), straigh, right (clockwise), straight
22+
* Order of rotations is left (counterclockwise), straight, right (clockwise)
2223
* @private
2324
* @param {Object} cart the cart being turned
2425
* @returns {String} value of new direction
2526
*/
2627
_intersect (cart) {
28+
const i = this.interSectionOrder
2729
let l = cart.lastIntersections
28-
let r = (l[0] !== 0) ? 0 : l[1] * -1
30+
31+
// Figure out the new rotation
32+
let r = i.indexOf(l[0])
33+
r = (r + 1 >= i.length) ? i[0] : i[r + 1]
34+
2935
// Track the intersections
3036
cart.lastIntersections.pop()
3137
cart.lastIntersections.splice(0, 0, r)
@@ -105,7 +111,7 @@ class Track {
105111
output += '\n'
106112
})
107113

108-
return output.trim()
114+
return output
109115
}
110116

111117
/**
@@ -120,7 +126,7 @@ class Track {
120126
x: idx,
121127
y: idy,
122128
direction: x,
123-
lastIntersections: [0, 1] // Assume the first intersection the cart will turn left
129+
lastIntersections: [1, 0] // Assume the first ntersection the cart will turn left
124130
})
125131
// Replace the cart on the track with a track segment
126132
// (Assuming cart initial states aren't on instersections)

2018/day-13/tracks.test.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
2323
x: 2,
2424
y: 0,
2525
direction: '>',
26-
lastIntersections: [0, 1]
26+
lastIntersections: [1, 0]
2727
}, {
2828
x: 9,
2929
y: 3,
3030
direction: 'v',
31-
lastIntersections: [0, 1]
31+
lastIntersections: [1, 0]
3232
}]
3333
const track = new Track(data)
3434
const actual = track.carts
@@ -51,7 +51,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
5151
\\------/ `.trim()
5252
const track = new Track(test)
5353
track.advance()
54-
const actual = track.display()
54+
const actual = track.display().trim()
5555
expect(actual).to.equal(expected)
5656
expect(track.frame).to.equal(1)
5757
})
@@ -62,31 +62,31 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
6262
const track = new Track(test)
6363
const expected = `-->-`
6464
track.moveCart(track.carts[0])
65-
const actual = track.display()
65+
const actual = track.display().trim()
6666
expect(actual).to.equal(expected)
6767
})
6868
it('moves an individual cart left', () => {
6969
const test = `--<-`
7070
const track = new Track(test)
7171
const expected = `-<--`
7272
track.moveCart(track.carts[0])
73-
const actual = track.display()
73+
const actual = track.display().trim()
7474
expect(actual).to.equal(expected)
7575
})
7676
it('moves an individual cart down', () => {
7777
const test = `|\nv\n|\n|`
7878
const track = new Track(test)
7979
const expected = `|\n|\nv\n|`
8080
track.moveCart(track.carts[0])
81-
const actual = track.display()
81+
const actual = track.display().trim()
8282
expect(actual).to.equal(expected)
8383
})
8484
it('moves an individual cart up', () => {
8585
const test = `|\n|\n^\n|`
8686
const track = new Track(test)
8787
const expected = `|\n^\n|\n|`
8888
track.moveCart(track.carts[0])
89-
const actual = track.display()
89+
const actual = track.display().trim()
9090
expect(actual).to.equal(expected)
9191
})
9292
it('rotates a cart when it enters turns', () => {
@@ -114,7 +114,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
114114
const track = new Track(test)
115115
track.moveCart(track.carts[0])
116116
track.moveCart(track.carts[0])
117-
const actual = track.display()
117+
const actual = track.display().trim()
118118
expect(actual).to.equal(expected[idx])
119119
})
120120
})
@@ -124,32 +124,32 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
124124
const expected = `---^-`
125125
track.moveCart(track.carts[0])
126126
track.moveCart(track.carts[0])
127-
const actual = track.display()
127+
const actual = track.display().trim()
128128
expect(actual).to.equal(expected)
129129
})
130-
it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right, straight', () => {
130+
it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right', () => {
131131
const expected = `
132-
^
133-
|
134-
+-+-+
132+
^
133+
|
134+
+-+
135135
|
136136
+
137137
|
138138
---+`.trim()
139139
const test = `
140-
|
141-
|
142-
+-+-+
140+
|
141+
|
142+
+-+
143143
|
144144
+
145145
|
146146
->-+`
147147
const track = new Track(test)
148148

149-
for (let i = 0; i < 12; i++) {
149+
for (let i = 0; i < 10; i++) {
150150
track.moveCart(track.carts[0])
151151
}
152-
const actual = track.display()
152+
const actual = track.display().trim()
153153
expect(actual).to.equal(expected)
154154
})
155155
it('only moves the specified cart', () => {
@@ -158,7 +158,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
158158
const track = new Track(test)
159159
track.moveCart(track.carts[0])
160160
track.moveCart(track.carts[0])
161-
const actual = track.display()
161+
const actual = track.display().trim()
162162
expect(actual).to.equal(expected)
163163
})
164164
it('throws an error if the cart runs off the rails', () => {
@@ -178,7 +178,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
178178
track.moveCart(track.carts[0])
179179
track.moveCart(track.carts[0])
180180
} catch (err) {
181-
const actual = track.display()
181+
const actual = track.display().trim()
182182
expect(actual).to.equal(expected)
183183
expect(track.collision).to.deep.equal({ x: 3, y: 0 })
184184
}
@@ -188,7 +188,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => {
188188
it('renders the current track state', () => {
189189
const expected = data.trim()
190190
const track = new Track(data)
191-
const actual = track.display()
191+
const actual = track.display().trim()
192192
expect(actual).to.equal(expected)
193193
})
194194
})

0 commit comments

Comments
 (0)