Skip to content

Commit dbec37c

Browse files
feat(2018 day-13): draw cart collisions on display
1 parent 416d3ad commit dbec37c

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

2018/day-13/tracks.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,27 @@ class Track {
77
this.setLayout(track)
88
}
99

10-
setLayout (track) {
11-
this.layout = track.split('\n')
12-
this.layout = this.layout.map((e) => { return e.split('') })
13-
this.extractCarts()
10+
/**
11+
* Displays the current state of the track with the carts placed
12+
*/
13+
display () {
14+
let output = ''
15+
const layout = JSON.parse(JSON.stringify(this.layout)) // Deep copy
16+
// Include the carts
17+
this.carts.forEach((cart) => {
18+
// If another cart is at the spot, draw a collision instead
19+
if (this.cartDirections.indexOf(layout[cart.y][cart.x]) >= 0) {
20+
layout[cart.y][cart.x] = 'X'
21+
} else {
22+
layout[cart.y][cart.x] = cart.direction
23+
}
24+
})
25+
layout.forEach((y) => {
26+
output += y.join('')
27+
output += '\n'
28+
})
29+
30+
return output.trim()
1431
}
1532

1633
/**
@@ -44,22 +61,10 @@ class Track {
4461
return this.layout[y][x]
4562
}
4663

47-
/**
48-
* Displays the current state of the track
49-
*/
50-
display () {
51-
let output = ''
52-
const layout = JSON.parse(JSON.stringify(this.layout)) // Deep copy
53-
// Include the carts
54-
this.carts.forEach((cart) => {
55-
layout[cart.y][cart.x] = cart.direction
56-
})
57-
layout.forEach((y) => {
58-
output += y.join('')
59-
output += '\n'
60-
})
61-
62-
return output.trim()
64+
setLayout (track) {
65+
this.layout = track.split('\n')
66+
this.layout = this.layout.map((e) => { return e.split('') })
67+
this.extractCarts()
6368
}
6469
}
6570

0 commit comments

Comments
 (0)