Skip to content

Commit e9e45dc

Browse files
feat(2018 day-13): parse, store, and display track with carts
1 parent 064a743 commit e9e45dc

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

2018/day-13/solution.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const {
2+
Track
3+
} = require('./tracks')
4+
5+
/* eslint-disable */
6+
const data = `/->-\\
7+
| | /----\\
8+
| /-+--+-\\ |
9+
| | | | v |
10+
\\-+-/ \\-+--/
11+
\\------/ `
12+
/* eslint-enable */
13+
14+
const mytrack = new Track(data)
15+
const actual = mytrack.display()
16+
console.log(actual)

2018/day-13/tracks.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Track {
2+
constructor (track) {
3+
this.layout = []
4+
this.carts = []
5+
this.cartDirections = ['^', '>', 'v', '<']
6+
this.frame = 0
7+
this.setLayout(track)
8+
}
9+
10+
setLayout (track) {
11+
this.layout = track.split('\n')
12+
this.layout = this.layout.map((e) => { return e.split('') })
13+
this.extractCarts()
14+
}
15+
16+
/**
17+
* Locates carts on the imported layout and pops them out to the carts list
18+
*/
19+
extractCarts () {
20+
this.layout = this.layout.map((y, idy) => {
21+
return y.map((x, idx) => {
22+
// Pop the cart into the cart list with its location
23+
if (this.cartDirections.indexOf(x) >= 0) {
24+
this.carts.push({
25+
x: idx,
26+
y: idy,
27+
direction: x
28+
})
29+
// Replace the cart on the track with a track segment
30+
// (Assuming cart initial states aren't on instersections)
31+
x = (this.cartDirections.indexOf(x) % 2 === 0) ? '|' : '-'
32+
}
33+
return x
34+
})
35+
})
36+
}
37+
38+
/**
39+
* Gets the track segment at the specified coordinates
40+
* @param {*} x Number
41+
* @param {*} y Number
42+
*/
43+
getSegmentType (x, y) {
44+
return this.layout[y][x]
45+
}
46+
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()
63+
}
64+
}
65+
66+
module.exports = {
67+
Track
68+
}

2018/day-13/tracks.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* eslint-env mocha */
2+
const expect = require('chai').expect
3+
const {
4+
Track
5+
} = require('./tracks')
6+
7+
const data = `/->-\\
8+
| | /----\\
9+
| /-+--+-\\ |
10+
| | | | v |
11+
\\-+-/ \\-+--/
12+
\\------/ `.trim()
13+
14+
describe('--- Day 13: Mine Cart Madness ---', () => {
15+
describe('Part 1:', () => {
16+
describe('new Track(layout)', () => {
17+
it('Initializes a new track with layout', () => {
18+
const track = new Track(data)
19+
expect(track.layout[4][12]).to.equal('/')
20+
})
21+
it('splits out and stores the carts as addressable data', () => {
22+
const expected = [{
23+
x: 2,
24+
y: 0,
25+
direction: '>'
26+
}, {
27+
x: 9,
28+
y: 3,
29+
direction: 'v'
30+
}]
31+
const track = new Track(data)
32+
const actual = track.carts
33+
expect(actual).to.deep.equal(expected)
34+
})
35+
})
36+
describe('advance()', () => {
37+
it.skip('iterates the state of the track')
38+
})
39+
describe('display()', () => {
40+
it('renders the current track state', () => {
41+
const expected = data
42+
const track = new Track(data)
43+
const actual = track.display()
44+
expect(actual).to.equal(expected)
45+
})
46+
})
47+
describe('getSegmentType(x,y)', () => {
48+
it.skip('queries the type of segment at location x,y', () => {
49+
const expected = '-'
50+
const track = new Track(data)
51+
const actual = track.getSegmentType(5, 2)
52+
expect(actual).to.equal(expected)
53+
})
54+
})
55+
})
56+
})

0 commit comments

Comments
 (0)