From e9e45dc1d6c33b9c5cf6402c62a532463e5f7a5e Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 21 Dec 2018 08:56:29 -0800 Subject: [PATCH 1/7] feat(2018 day-13): parse, store, and display track with carts --- 2018/day-13/solution.js | 16 +++++++++ 2018/day-13/tracks.js | 68 ++++++++++++++++++++++++++++++++++++++ 2018/day-13/tracks.test.js | 56 +++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 2018/day-13/solution.js create mode 100644 2018/day-13/tracks.js create mode 100644 2018/day-13/tracks.test.js diff --git a/2018/day-13/solution.js b/2018/day-13/solution.js new file mode 100644 index 0000000..c280d7d --- /dev/null +++ b/2018/day-13/solution.js @@ -0,0 +1,16 @@ +const { + Track +} = require('./tracks') + +/* eslint-disable */ +const data = `/->-\\ +| | /----\\ +| /-+--+-\\ | +| | | | v | +\\-+-/ \\-+--/ + \\------/ ` +/* eslint-enable */ + +const mytrack = new Track(data) +const actual = mytrack.display() +console.log(actual) diff --git a/2018/day-13/tracks.js b/2018/day-13/tracks.js new file mode 100644 index 0000000..1e437fe --- /dev/null +++ b/2018/day-13/tracks.js @@ -0,0 +1,68 @@ +class Track { + constructor (track) { + this.layout = [] + this.carts = [] + this.cartDirections = ['^', '>', 'v', '<'] + this.frame = 0 + this.setLayout(track) + } + + setLayout (track) { + this.layout = track.split('\n') + this.layout = this.layout.map((e) => { return e.split('') }) + this.extractCarts() + } + + /** + * Locates carts on the imported layout and pops them out to the carts list + */ + extractCarts () { + this.layout = this.layout.map((y, idy) => { + return y.map((x, idx) => { + // Pop the cart into the cart list with its location + if (this.cartDirections.indexOf(x) >= 0) { + this.carts.push({ + x: idx, + y: idy, + direction: x + }) + // Replace the cart on the track with a track segment + // (Assuming cart initial states aren't on instersections) + x = (this.cartDirections.indexOf(x) % 2 === 0) ? '|' : '-' + } + return x + }) + }) + } + + /** + * Gets the track segment at the specified coordinates + * @param {*} x Number + * @param {*} y Number + */ + getSegmentType (x, y) { + return this.layout[y][x] + } + + /** + * Displays the current state of the track + */ + display () { + let output = '' + const layout = JSON.parse(JSON.stringify(this.layout)) // Deep copy + // Include the carts + this.carts.forEach((cart) => { + layout[cart.y][cart.x] = cart.direction + }) + layout.forEach((y) => { + output += y.join('') + output += '\n' + }) + + return output.trim() + } +} + +module.exports = { + Track +} diff --git a/2018/day-13/tracks.test.js b/2018/day-13/tracks.test.js new file mode 100644 index 0000000..d6d9d6c --- /dev/null +++ b/2018/day-13/tracks.test.js @@ -0,0 +1,56 @@ +/* eslint-env mocha */ +const expect = require('chai').expect +const { + Track +} = require('./tracks') + +const data = `/->-\\ +| | /----\\ +| /-+--+-\\ | +| | | | v | +\\-+-/ \\-+--/ + \\------/ `.trim() + +describe('--- Day 13: Mine Cart Madness ---', () => { + describe('Part 1:', () => { + describe('new Track(layout)', () => { + it('Initializes a new track with layout', () => { + const track = new Track(data) + expect(track.layout[4][12]).to.equal('/') + }) + it('splits out and stores the carts as addressable data', () => { + const expected = [{ + x: 2, + y: 0, + direction: '>' + }, { + x: 9, + y: 3, + direction: 'v' + }] + const track = new Track(data) + const actual = track.carts + expect(actual).to.deep.equal(expected) + }) + }) + describe('advance()', () => { + it.skip('iterates the state of the track') + }) + describe('display()', () => { + it('renders the current track state', () => { + const expected = data + const track = new Track(data) + const actual = track.display() + expect(actual).to.equal(expected) + }) + }) + describe('getSegmentType(x,y)', () => { + it.skip('queries the type of segment at location x,y', () => { + const expected = '-' + const track = new Track(data) + const actual = track.getSegmentType(5, 2) + expect(actual).to.equal(expected) + }) + }) + }) +}) From 416d3ad1051413e5b6042307bfb4ccea08ee24aa Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 21 Dec 2018 08:59:48 -0800 Subject: [PATCH 2/7] test(2018 day-13): enable unit test --- 2018/day-13/tracks.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2018/day-13/tracks.test.js b/2018/day-13/tracks.test.js index d6d9d6c..0c17d56 100644 --- a/2018/day-13/tracks.test.js +++ b/2018/day-13/tracks.test.js @@ -45,7 +45,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { }) }) describe('getSegmentType(x,y)', () => { - it.skip('queries the type of segment at location x,y', () => { + it('queries the type of segment at location x,y', () => { const expected = '-' const track = new Track(data) const actual = track.getSegmentType(5, 2) From dbec37c842428961b626bcfc78ac20719ff8742d Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 21 Dec 2018 09:52:59 -0800 Subject: [PATCH 3/7] feat(2018 day-13): draw cart collisions on display --- 2018/day-13/tracks.js | 45 ++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/2018/day-13/tracks.js b/2018/day-13/tracks.js index 1e437fe..a18493b 100644 --- a/2018/day-13/tracks.js +++ b/2018/day-13/tracks.js @@ -7,10 +7,27 @@ class Track { this.setLayout(track) } - setLayout (track) { - this.layout = track.split('\n') - this.layout = this.layout.map((e) => { return e.split('') }) - this.extractCarts() + /** + * Displays the current state of the track with the carts placed + */ + display () { + let output = '' + const layout = JSON.parse(JSON.stringify(this.layout)) // Deep copy + // Include the carts + this.carts.forEach((cart) => { + // If another cart is at the spot, draw a collision instead + if (this.cartDirections.indexOf(layout[cart.y][cart.x]) >= 0) { + layout[cart.y][cart.x] = 'X' + } else { + layout[cart.y][cart.x] = cart.direction + } + }) + layout.forEach((y) => { + output += y.join('') + output += '\n' + }) + + return output.trim() } /** @@ -44,22 +61,10 @@ class Track { return this.layout[y][x] } - /** - * Displays the current state of the track - */ - display () { - let output = '' - const layout = JSON.parse(JSON.stringify(this.layout)) // Deep copy - // Include the carts - this.carts.forEach((cart) => { - layout[cart.y][cart.x] = cart.direction - }) - layout.forEach((y) => { - output += y.join('') - output += '\n' - }) - - return output.trim() + setLayout (track) { + this.layout = track.split('\n') + this.layout = this.layout.map((e) => { return e.split('') }) + this.extractCarts() } } From d9d8489b9a2032fb378cb01fec1528985c88cbca Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 21 Dec 2018 12:08:22 -0800 Subject: [PATCH 4/7] feat(2018 day-13): individual cart movement --- 2018/day-13/tracks.js | 106 ++++++++++++++++++++++++++++ 2018/day-13/tracks.test.js | 138 ++++++++++++++++++++++++++++++++++++- 2 files changed, 241 insertions(+), 3 deletions(-) diff --git a/2018/day-13/tracks.js b/2018/day-13/tracks.js index a18493b..50ee0ab 100644 --- a/2018/day-13/tracks.js +++ b/2018/day-13/tracks.js @@ -3,10 +3,77 @@ class Track { this.layout = [] this.carts = [] this.cartDirections = ['^', '>', 'v', '<'] + this.trackTurns = ['\\', '/'] + this.trackTypes = this.trackTurns.concat(['-', '|', '+']) + this.collision = false this.frame = 0 this.setLayout(track) } + _isCollision (x, y) { return (this.carts.filter((c) => c.x === x && c.y === y).length > 1) } + _isIntersection (s) { return s === '+' } + _isTurn (s) { return this.trackTurns.indexOf(s) >= 0 } + + /** + * Determines the next direction for a cart rotating at an intersection + * Order of rotations is left (counterclockwise), right (clockwise), straight + * @private + * @param {Object} cart the cart being turned + * @returns {String} value of new direction + */ + _intersect (cart) { + let r = 0 + if (typeof cart.lastIntersection === 'undefined') { + r = -1 + } + if (cart.lastIntersection === -1) { + r = 1 + } + + cart.lastIntersection = r + return this.cartDirections[this._roationDirection(this.cartDirections.indexOf(cart.direction), r)] + } + + /** + * Rotates the cart + * @private + * @param {String} s track segment the cart is now on + * @param {String} a x||y axis of travel + * @param {Number} d Index of absolute cart direction + * @returns {String} value of new direction + */ + _rotate (s, a, d) { + // Determine which way we're rotating + let r = ( + (this.trackTurns.indexOf(s) === 1 && a === 'y') || // vertical turns clockwise + (this.trackTurns.indexOf(s) === 0 && a === 'x') // horizontal turns clockwise + // (this.trackTurns.indexOf(s) === 0 && a === 'x') // horizontal turns counter-clockwise + // (this.trackTurns.indexOf(s) === 1 && a === 'y') // vertical turns counter-clockwise + ) ? 1 : -1 + console.log(`${s} for ${a} is turning ${r}`) + // Find the value of the new direction + return this.cartDirections[this._roationDirection(d, r)] + } + + /** + * Determines the next clockwise or counter-clockwise direction + * @private + * @param {Number} d Index of current direction + * @param {Number} r +1 for clockwise (turn right), -1 for counterclockwise (turn left) + * @returns {Number} Index of new direction + */ + _roationDirection (d, r) { + console.log(`rotating ${d}, ${r}`) + if (d + r > this.cartDirections.length - 1) { + return 0 + } + if (d + r < 0) { + return this.cartDirections.length - 1 + } + console.log(`new direction is ${d + r}`) + return d + r + } + /** * Displays the current state of the track with the carts placed */ @@ -61,6 +128,45 @@ class Track { return this.layout[y][x] } + /** + * Moves the specified cart in the direction of travel, observing all rules + * @param {Objc} cart from the list of carts + */ + moveCart (cart) { + // Determine the direction of travel + const d = this.cartDirections.indexOf(cart.direction) // Absolute direction + const a = (d % 2 === 0) ? 'y' : 'x' // axis of travel + const l = (d % 3 === 0) ? -1 : 1 // (+/-) distance of travel on the axis + // move the cart + cart[a] = cart[a] + l + const s = this.layout[cart.y][cart.x] // Segment of track the cart is now on + + // Make sure cart hasn't run off the rails + if (this.trackTypes.indexOf(this.layout[cart.y][cart.x]) < 0) { + return new Error(`cart ran off the track at ${cart.x}, ${cart.y}`) + } + // Check for collision + if (this._isCollision(cart.x, cart.y)) { + this.collision = { x: cart.x, y: cart.y } + return new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything + } + // rotate the cart when entering a turn + if (this._isTurn(s)) { + console.log(`Cart direction was ${cart.direction}`) + cart.direction = this._rotate(s, a, d) + console.log(`Cart direction is ${cart.direction}`) + return + } + // rotate (or not) the cart when entering an intersection + if (this._isIntersection(s)) { + cart.direction = this._intersect(cart) + } + } + + /** + * Stores the provided track + * @param {String} track + */ setLayout (track) { this.layout = track.split('\n') this.layout = this.layout.map((e) => { return e.split('') }) diff --git a/2018/day-13/tracks.test.js b/2018/day-13/tracks.test.js index 0c17d56..c429e33 100644 --- a/2018/day-13/tracks.test.js +++ b/2018/day-13/tracks.test.js @@ -9,7 +9,7 @@ const data = `/->-\\ | /-+--+-\\ | | | | | v | \\-+-/ \\-+--/ - \\------/ `.trim() + \\------/ ` describe('--- Day 13: Mine Cart Madness ---', () => { describe('Part 1:', () => { @@ -34,11 +34,143 @@ describe('--- Day 13: Mine Cart Madness ---', () => { }) }) describe('advance()', () => { - it.skip('iterates the state of the track') + it('moves forward all carts on the layout', () => { + const test = `/---v +| | /----\\ +| /-+--+-\\ | +| | | | | | +\\-+-/ \\-+>-/ + \\------/ ` + const expected = `/---\ +| v /----\\ +| /-+--+-\\ | +| | | | | | +\\-+-/ \\-+->/ + \\------/ `.trim() + const track = new Track(test) + track.moveCart() + const actual = track.display() + expect(actual).to.equal(expected) + }) + }) + describe('moveCart(cart)', () => { + it('moves an individual cart right', () => { + const test = `->--` + const track = new Track(test) + const expected = `-->-` + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected) + }) + it('moves an individual cart left', () => { + const test = `--<-` + const track = new Track(test) + const expected = `-<--` + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected) + }) + it('moves an individual cart down', () => { + const test = `|\nv\n|\n|` + const track = new Track(test) + const expected = `|\n|\nv\n|` + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected) + }) + it('moves an individual cart up', () => { + const test = `|\n|\n^\n|` + const track = new Track(test) + const expected = `|\n^\n|\n|` + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected) + }) + it('rotates a cart when it enters turns', () => { + const tests = [ + `->-\\-`, + `->-/-`, + `-/-<-`, + `-\\-<-`, + `|\nv\n|\n\\\n|`, + `|\nv\n|\n/\n|`, + `|\n/\n|\n^\n|`, + `|\n\\\n|\n^\n|` + ] + const expected = [ + `---v-`, + `---^-`, + `-v---`, + `-^---`, + `|\n|\n|\n>\n|`, + `|\n|\n|\n<\n|`, + `|\n>\n|\n|\n|`, + `|\n<\n|\n|\n|` + ] + tests.forEach((test, idx) => { + const track = new Track(test) + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected[idx]) + }) + }) + it('rotates a cart when it enters an intersection', () => { + const test = `->-+-` + const track = new Track(test) + const expected = `---^-` + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected) + }) + it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right', () => { + const test = ` + | + -+-+-- + | +->-+-` + const track = new Track(test) + const expected = ` + | + -+-+-> + | +---+-`.trim() + for (let i = 0; i < 8; i++) { + track.moveCart(track.carts[0]) + } + const actual = track.display() + expect(actual).to.equal(expected) + }) + it('only moves the specified cart', () => { + const test = `->--<-` + const expected = `---><-` + const track = new Track(test) + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected) + }) + it('throws an error if the cart runs off the rails', () => { + const test = `->- -` + const track = new Track(test) + track.moveCart(track.carts[0]) + expect(track.moveCart(track.carts[0])).to.be.an('error') + }) + it('registers a collision', () => { + const test = `->-<-` + const expected = `---X-` + const track = new Track(test) + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + const actual = track.display() + expect(actual).to.equal(expected) + expect(track.collision).to.deep.equal({ x: 3, y: 0 }) + }) }) describe('display()', () => { it('renders the current track state', () => { - const expected = data + const expected = data.trim() const track = new Track(data) const actual = track.display() expect(actual).to.equal(expected) From 5b2e83fc6ed6538619983f8cf2cf4cb7ba26d305 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 21 Dec 2018 13:32:14 -0800 Subject: [PATCH 5/7] feat(2018 day-13): advance all carts each turn and correctly handle intersection rotation logic --- 2018/day-13/solution.js | 13 ++++--- 2018/day-13/tracks.js | 52 ++++++++++++++++----------- 2018/day-13/tracks.test.js | 74 +++++++++++++++++++++++--------------- 3 files changed, 84 insertions(+), 55 deletions(-) diff --git a/2018/day-13/solution.js b/2018/day-13/solution.js index c280d7d..e6796dc 100644 --- a/2018/day-13/solution.js +++ b/2018/day-13/solution.js @@ -2,15 +2,18 @@ const { Track } = require('./tracks') -/* eslint-disable */ const data = `/->-\\ | | /----\\ | /-+--+-\\ | | | | | v | \\-+-/ \\-+--/ \\------/ ` -/* eslint-enable */ -const mytrack = new Track(data) -const actual = mytrack.display() -console.log(actual) +const track = new Track(data) +while (track.collision === false) { + track.advance() +} + +console.error(`Reached a collision at ${track.collision.x},${track.collision.y} on frame ${track.frame}`) +console.log(`Track state:`) +console.log(track.display()) diff --git a/2018/day-13/tracks.js b/2018/day-13/tracks.js index 50ee0ab..90c882c 100644 --- a/2018/day-13/tracks.js +++ b/2018/day-13/tracks.js @@ -1,12 +1,14 @@ +const { dynamicSortMultiple } = require('../day-04/helpers') + class Track { constructor (track) { this.layout = [] this.carts = [] this.cartDirections = ['^', '>', 'v', '<'] - this.trackTurns = ['\\', '/'] - this.trackTypes = this.trackTurns.concat(['-', '|', '+']) this.collision = false this.frame = 0 + this.trackTurns = ['\\', '/'] + this.trackTypes = this.trackTurns.concat(['-', '|', '+']) this.setLayout(track) } @@ -16,21 +18,18 @@ class Track { /** * Determines the next direction for a cart rotating at an intersection - * Order of rotations is left (counterclockwise), right (clockwise), straight + * Order of rotations is left (counterclockwise), straigh, right (clockwise), straight * @private * @param {Object} cart the cart being turned * @returns {String} value of new direction */ _intersect (cart) { - let r = 0 - if (typeof cart.lastIntersection === 'undefined') { - r = -1 - } - if (cart.lastIntersection === -1) { - r = 1 - } + let l = cart.lastIntersections + let r = (l[0] !== 0) ? 0 : l[1] * -1 + // Track the intersections + cart.lastIntersections.pop() + cart.lastIntersections.splice(0, 0, r) - cart.lastIntersection = r return this.cartDirections[this._roationDirection(this.cartDirections.indexOf(cart.direction), r)] } @@ -50,7 +49,6 @@ class Track { // (this.trackTurns.indexOf(s) === 0 && a === 'x') // horizontal turns counter-clockwise // (this.trackTurns.indexOf(s) === 1 && a === 'y') // vertical turns counter-clockwise ) ? 1 : -1 - console.log(`${s} for ${a} is turning ${r}`) // Find the value of the new direction return this.cartDirections[this._roationDirection(d, r)] } @@ -63,17 +61,30 @@ class Track { * @returns {Number} Index of new direction */ _roationDirection (d, r) { - console.log(`rotating ${d}, ${r}`) if (d + r > this.cartDirections.length - 1) { return 0 } if (d + r < 0) { return this.cartDirections.length - 1 } - console.log(`new direction is ${d + r}`) return d + r } + /** + * Advances the state of the entire layout + */ + advance () { + this.frame++ + this.carts.sort(dynamicSortMultiple('y', 'x')).forEach((c) => { + try { + this.moveCart(c) + } catch (err) { + console.error(`Problem moving cart in frame ${this.frame}`) + console.error(err) + } + }) + } + /** * Displays the current state of the track with the carts placed */ @@ -108,7 +119,8 @@ class Track { this.carts.push({ x: idx, y: idy, - direction: x + direction: x, + lastIntersections: [0, 1] // Assume the first intersection the cart will turn left }) // Replace the cart on the track with a track segment // (Assuming cart initial states aren't on instersections) @@ -124,7 +136,7 @@ class Track { * @param {*} x Number * @param {*} y Number */ - getSegmentType (x, y) { + getSegment (x, y) { return this.layout[y][x] } @@ -139,22 +151,20 @@ class Track { const l = (d % 3 === 0) ? -1 : 1 // (+/-) distance of travel on the axis // move the cart cart[a] = cart[a] + l - const s = this.layout[cart.y][cart.x] // Segment of track the cart is now on + const s = this.getSegment(cart.x, cart.y) // Segment of track the cart is now on // Make sure cart hasn't run off the rails if (this.trackTypes.indexOf(this.layout[cart.y][cart.x]) < 0) { - return new Error(`cart ran off the track at ${cart.x}, ${cart.y}`) + throw new Error(`cart ran off the track at ${cart.x}, ${cart.y}`) } // Check for collision if (this._isCollision(cart.x, cart.y)) { this.collision = { x: cart.x, y: cart.y } - return new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything + throw new Error(`collision at ${cart.x}, ${cart.y}`) // Stop everything } // rotate the cart when entering a turn if (this._isTurn(s)) { - console.log(`Cart direction was ${cart.direction}`) cart.direction = this._rotate(s, a, d) - console.log(`Cart direction is ${cart.direction}`) return } // rotate (or not) the cart when entering an intersection diff --git a/2018/day-13/tracks.test.js b/2018/day-13/tracks.test.js index c429e33..35d8988 100644 --- a/2018/day-13/tracks.test.js +++ b/2018/day-13/tracks.test.js @@ -22,11 +22,13 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const expected = [{ x: 2, y: 0, - direction: '>' + direction: '>', + lastIntersections: [0, 1] }, { x: 9, y: 3, - direction: 'v' + direction: 'v', + lastIntersections: [0, 1] }] const track = new Track(data) const actual = track.carts @@ -35,22 +37,23 @@ describe('--- Day 13: Mine Cart Madness ---', () => { }) describe('advance()', () => { it('moves forward all carts on the layout', () => { - const test = `/---v + const test = `/---\\ | | /----\\ -| /-+--+-\\ | -| | | | | | -\\-+-/ \\-+>-/ +| /-+>-+-\\ | +| | | | | ^ +\\-+-/ \\-+--/ \\------/ ` - const expected = `/---\ -| v /----\\ -| /-+--+-\\ | + const expected = `/---\\ +| | /----\\ +| /-+->+-\\ ^ | | | | | | -\\-+-/ \\-+->/ +\\-+-/ \\-+--/ \\------/ `.trim() const track = new Track(test) - track.moveCart() + track.advance() const actual = track.display() expect(actual).to.equal(expected) + expect(track.frame).to.equal(1) }) }) describe('moveCart(cart)', () => { @@ -124,19 +127,26 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const actual = track.display() expect(actual).to.equal(expected) }) - it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right', () => { - const test = ` + it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right, straight', () => { + const expected = ` + ^ + | + +-+-+ | - -+-+-- + + | -->-+-` - const track = new Track(test) - const expected = ` +---+`.trim() + const test = ` + | + | + +-+-+ | - -+-+-> + + | ----+-`.trim() - for (let i = 0; i < 8; i++) { +->-+` + const track = new Track(test) + + for (let i = 0; i < 12; i++) { track.moveCart(track.carts[0]) } const actual = track.display() @@ -154,18 +164,24 @@ describe('--- Day 13: Mine Cart Madness ---', () => { it('throws an error if the cart runs off the rails', () => { const test = `->- -` const track = new Track(test) - track.moveCart(track.carts[0]) - expect(track.moveCart(track.carts[0])).to.be.an('error') + try { + track.moveCart(track.carts[0]) + } catch (err) { + expect(err).to.be.an('error') + } }) it('registers a collision', () => { const test = `->-<-` const expected = `---X-` const track = new Track(test) - track.moveCart(track.carts[0]) - track.moveCart(track.carts[0]) - const actual = track.display() - expect(actual).to.equal(expected) - expect(track.collision).to.deep.equal({ x: 3, y: 0 }) + try { + track.moveCart(track.carts[0]) + track.moveCart(track.carts[0]) + } catch (err) { + const actual = track.display() + expect(actual).to.equal(expected) + expect(track.collision).to.deep.equal({ x: 3, y: 0 }) + } }) }) describe('display()', () => { @@ -176,11 +192,11 @@ describe('--- Day 13: Mine Cart Madness ---', () => { expect(actual).to.equal(expected) }) }) - describe('getSegmentType(x,y)', () => { + describe('getSegment(x,y)', () => { it('queries the type of segment at location x,y', () => { const expected = '-' const track = new Track(data) - const actual = track.getSegmentType(5, 2) + const actual = track.getSegment(5, 2) expect(actual).to.equal(expected) }) }) From 9b9f3cf3e2e2dbeda327f6383fba559fcc29fb64 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 21 Dec 2018 14:33:33 -0800 Subject: [PATCH 6/7] fix(2018 day-13): correct order for intersections is left, straight, right, repeat --- 2018/day-13/tracks.js | 14 +++++++++---- 2018/day-13/tracks.test.js | 42 +++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/2018/day-13/tracks.js b/2018/day-13/tracks.js index 90c882c..afe89f1 100644 --- a/2018/day-13/tracks.js +++ b/2018/day-13/tracks.js @@ -7,6 +7,7 @@ class Track { this.cartDirections = ['^', '>', 'v', '<'] this.collision = false this.frame = 0 + this.interSectionOrder = [-1, 0, 1] this.trackTurns = ['\\', '/'] this.trackTypes = this.trackTurns.concat(['-', '|', '+']) this.setLayout(track) @@ -18,14 +19,19 @@ class Track { /** * Determines the next direction for a cart rotating at an intersection - * Order of rotations is left (counterclockwise), straigh, right (clockwise), straight + * Order of rotations is left (counterclockwise), straight, right (clockwise) * @private * @param {Object} cart the cart being turned * @returns {String} value of new direction */ _intersect (cart) { + const i = this.interSectionOrder let l = cart.lastIntersections - let r = (l[0] !== 0) ? 0 : l[1] * -1 + + // Figure out the new rotation + let r = i.indexOf(l[0]) + r = (r + 1 >= i.length) ? i[0] : i[r + 1] + // Track the intersections cart.lastIntersections.pop() cart.lastIntersections.splice(0, 0, r) @@ -105,7 +111,7 @@ class Track { output += '\n' }) - return output.trim() + return output } /** @@ -120,7 +126,7 @@ class Track { x: idx, y: idy, direction: x, - lastIntersections: [0, 1] // Assume the first intersection the cart will turn left + lastIntersections: [1, 0] // Assume the first ntersection the cart will turn left }) // Replace the cart on the track with a track segment // (Assuming cart initial states aren't on instersections) diff --git a/2018/day-13/tracks.test.js b/2018/day-13/tracks.test.js index 35d8988..7c9ff5c 100644 --- a/2018/day-13/tracks.test.js +++ b/2018/day-13/tracks.test.js @@ -23,12 +23,12 @@ describe('--- Day 13: Mine Cart Madness ---', () => { x: 2, y: 0, direction: '>', - lastIntersections: [0, 1] + lastIntersections: [1, 0] }, { x: 9, y: 3, direction: 'v', - lastIntersections: [0, 1] + lastIntersections: [1, 0] }] const track = new Track(data) const actual = track.carts @@ -51,7 +51,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { \\------/ `.trim() const track = new Track(test) track.advance() - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) expect(track.frame).to.equal(1) }) @@ -62,7 +62,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const track = new Track(test) const expected = `-->-` track.moveCart(track.carts[0]) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) it('moves an individual cart left', () => { @@ -70,7 +70,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const track = new Track(test) const expected = `-<--` track.moveCart(track.carts[0]) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) it('moves an individual cart down', () => { @@ -78,7 +78,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const track = new Track(test) const expected = `|\n|\nv\n|` track.moveCart(track.carts[0]) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) it('moves an individual cart up', () => { @@ -86,7 +86,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const track = new Track(test) const expected = `|\n^\n|\n|` track.moveCart(track.carts[0]) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) it('rotates a cart when it enters turns', () => { @@ -114,7 +114,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const track = new Track(test) track.moveCart(track.carts[0]) track.moveCart(track.carts[0]) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected[idx]) }) }) @@ -124,32 +124,32 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const expected = `---^-` track.moveCart(track.carts[0]) track.moveCart(track.carts[0]) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) - it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right, straight', () => { + it('tracks the direction through multiple intersections following the sequential rotation rules: left, straight, right', () => { const expected = ` - ^ - | - +-+-+ + ^ + | + +-+ | + | ---+`.trim() const test = ` - | - | - +-+-+ + | + | + +-+ | + | ->-+` const track = new Track(test) - for (let i = 0; i < 12; i++) { + for (let i = 0; i < 10; i++) { track.moveCart(track.carts[0]) } - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) it('only moves the specified cart', () => { @@ -158,7 +158,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { const track = new Track(test) track.moveCart(track.carts[0]) track.moveCart(track.carts[0]) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) it('throws an error if the cart runs off the rails', () => { @@ -178,7 +178,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { track.moveCart(track.carts[0]) track.moveCart(track.carts[0]) } catch (err) { - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) expect(track.collision).to.deep.equal({ x: 3, y: 0 }) } @@ -188,7 +188,7 @@ describe('--- Day 13: Mine Cart Madness ---', () => { it('renders the current track state', () => { const expected = data.trim() const track = new Track(data) - const actual = track.display() + const actual = track.display().trim() expect(actual).to.equal(expected) }) }) From 375135bfcfea8fb4e0abfb3a9c136871713b75f0 Mon Sep 17 00:00:00 2001 From: Anthony McLin Date: Fri, 21 Dec 2018 14:35:13 -0800 Subject: [PATCH 7/7] feat(2018 day-13): part 1 solution --- 2018/day-13/helpers.js | 17 +++++ 2018/day-13/input.txt | 150 ++++++++++++++++++++++++++++++++++++++++ 2018/day-13/solution.js | 36 +++++----- 3 files changed, 187 insertions(+), 16 deletions(-) create mode 100644 2018/day-13/helpers.js create mode 100644 2018/day-13/input.txt diff --git a/2018/day-13/helpers.js b/2018/day-13/helpers.js new file mode 100644 index 0000000..67488fc --- /dev/null +++ b/2018/day-13/helpers.js @@ -0,0 +1,17 @@ +const fs = require('fs') +const path = require('path') +const filePath = path.join(__dirname, 'input.txt') + +const loadInput = (callback) => { + fs.readFile(filePath, { encoding: 'utf8' }, (err, data) => { + if (err) throw err + + if (typeof callback === 'function') { + callback(data) + } + }) +} + +module.exports = { + loadInput +} diff --git a/2018/day-13/input.txt b/2018/day-13/input.txt new file mode 100644 index 0000000..802a0fa --- /dev/null +++ b/2018/day-13/input.txt @@ -0,0 +1,150 @@ + /--------------------------------------------------------------------------------------------------\ + | /-------------------------------------------------------\ |/-----------\ + | /-----------------------------------+-----------------------------------\ | /++---------\ | + /-----+-+--------------\ | /--+---------------\ | ||| | | + | | |/-------------+--------------------+---\ | | | | ||| | | + | /-+-++-------------+--------------------+---+----<----------------------\| | | | ||| | | + | | | || | /------------------+---+---------------------------++--+---------------+---+---+++--\ | | + /-----------+---+-+-++-------->----+-+------------------+---+---------------------------++--+-\ /-----+--\| ||| | | | + | /----+---+-+-++-------------+-+------------------+---+------\ || | | | | || ||| | | | + | | | /+-+-++-------------+-+------------------+---+------+--------------------++--+-+-------+-----+--++---+++--+-\ | | + | | | || |/++------------\| | /+---+------+-----------\ || | | | | || ||| | | | | + | | | || |||| || | /++---+--\ | /--------+--------++--+-+-------+-----+--++---+++--+-+----+-+-------------\ + | |/---+--++\|||| || | /--+++---+--+---+--+--------+--------++--+-+-------+-----+--++---+++--+-+----+-+----\ | + | /----++---+--+++++++-----\ || | /-------+--+++---+--+---+--+--------+--------++--+-+-------+-----+--++---+++--+-+----+-+----+--------+--\ + | | || | ||||||| | || | | /-+--+++---+--+---+--+--------+--------++--+-+-------+-----+--++---+++--+-+----+-+----+---\ | | + | | || | ||||||| | || | | |/+--+++---+--+---+--+--------+--------++--+-+-------+-----+-\|| ||| | | | | | | | | + | | || | ||||||| | || | | ||| ||| | | | | | /-----++--+-+-------+-----+-+++---+++--+-+----+\| | | | | + | | || | ||||||| | || | | ||| ||| | | | | | | || | | | | ||| ||| |/+----+++----+---+--\ | | + | | || | ||||||| | || | | /--+++--+++---+--+---+--+--------+--+-----++--+-+-------+-----+-+++---+++--+++----+++-\ | | | | | + | | ||/--+--+++++++-----+------++-+-----+--+--+++--+++---+--+---+--+--------+--+-----++--+-+------\| | ||| ||| ||| ||| | | | | | | + | | ||| | ||||||| | || | | | ||| ||| | | | | | | |\--+-+------++-----/ ||| ||| ||| ||| | | | | | | +/-+-+----+++--+--+++++++-----+------++-+---\ | | ||| \++---+--/ | | | | | | | || ||| ||| ||| ||| | | | | | | +| | | ||| | ||||||| | || | | | | ||| || | | | | /+-----+---+-+------++-------+++--\||| ||| ||| |/-+---+--+-+-\| +| | | ||| | ||||||| | || | | | | ||| \+---+------+--+--------/ || /---+---+-+------++-------+++--++++--+++----+++-++-+-\ | | | || +| | | ||| | ||||||| | || | | | | ||| | | | | /--------++-+---+---+-+------++-------+++-\|||| ||| ||| || | | | | | || +| | | ||| | ||||||\-----+------++-+---+-+--+--+++----+---/ | | | /--++-+---+---+-+------++------\||| ||\++--+++----/|| || | | | | | || +| | | ||| \--++++++------+------+/ | | | | ||| | | | | | || | | | | || |||| || || ||| || || | | | | | || +| |/+----+++--\ |||||\------+------+--+---+-+--+--+++----+----------+--+-+-----+--++-+---+---/ | || |||| || || ||| || || | | | | | || +|/+++----+++--+--+++++-------+\ | | | |/-+--+++----+----------+--+-+-----+--++-+---+-----+------++------++++-++-++--+++-----++-++-+-+-+\ | | || +||||| ||| | ||||| || | | | || | ||| | | | | /--+--++-+---+-----+------++------++++-++-++--+++\ || || | | || | | || +||||| \++--+--+++++-------++-----+--+---+-++-+--+++----+----------/ | | | | || | | | /----++------++++-++-++--++++----++-++-+-+\|| | | || +||||| || | ||||| || |/-+---+-++-+--+++----+-------------+-+--+--+--++-+--\| | | || |||| || || |||| || || | |||| | | || +||||| || | ||||| || || | | || | ||| /--+-------------+-+--+--+--++-+--++-----+-+-\ || |||| || || |||| || || | |||| | | || +||||| || | ||||| || /---++-+---+-++-+-<+++-+--+-----<-------+-+--+--+--++-+--++-\ | | | || |||| || || |||| || || | |||| | | || +||||| |\--+--+++++-------++-+---++-+---+-++-+--+++-+--+-------------+-+--+--+--++-+--++-+---+-+-+--/| |||| || || |||| || || | |||| | | || +||||\-----+---+--+++++-------/| | || | | || | ||| | | | | | | || | || | | | | | |||| || || |||| || || | |||| | | || +|||\------+---/ /+++++--------+-+-\ || | | || | ||| | | | | | | || | || | | | | | |||| || || |||| || || | |||| | | || +||| | |||||| /------+-+-+-++-+---+-++-+--+++-+--+-------------+-+--+--+--++-+--++-+---+-+-+---+------++++\|| || |||| || || | |||| | | || +||| | /---++++++-+------+-+\| || | |/++-+--+++-+--+-------------+-+--+--+--++-+--++-+---+-+-+---+------+++++++-++--++++-\ || || | |||| | | || +||| | | |||||| | | ||| || | |||| | ||| | | | | | | /++-+--++-+---+-+-+-\ | /----+++++++-++--++++-+--++-++-+-++++-+-+\|| +||| | | |||||| | | ||| || |/--++++-+--+++-+--+-------------+-+--+--+-+++-+--++-+---+-+-+-+-+-+---\||||||| || |||| | || || | |||| | |||| +|||/------+-+---++++++-+---\ | ||| || || /++++-+--+++-+--+-------------+-+--+--+-+++-+--++-+\ | | | | | | |||||||| || |||| | || || | |||| | |||| +|||| | | |||||| | /-+--+-+++-++-++-+++++-+--+++-+--+-------------+-+--+--+-+++\| || || | | | | | | |||||||| |\--++++-+--+/ || | |||| | |||| +|||| | | |||||| | | | | ||| || || ||||| | ||| | | | | | | ||||| || || | \-+-+-+-+---++++++++-+---++++-+--+--++-+-+/|| | |||| +|||| | | /++++++-+-+-+--+-+++-++-++-+++++-+--+++-+--+--\ | | | | ||||| || || | | | | | |||||||| | |||| | | || | | || | |||| +|||| | | ||||||| | | | | ||| || || ||||| | ||| | | | /-+-+--+-\| ||||| || || | | | | | |||||||| | |||| | | || | | || | |||| +||||/-----+-+--+++++++-+-+-+--+-+++-++-++-+++++-+--+++-+--+--+----\ | | | |/++-+++++--++-++--+---+-+-+-+---++++++++-+--\|||| | /+--++-+-+\|| | |||| +||||| | | |||||\+-+-+-+--+-+++-++-++-+++++-+--+++-+--+--+----+---+-+-+--++++-+++++--++-++--+---+-+-+-+---++++++++-/ ||||| | || || | |||| | |||| +||||| /-+-+--+++++-+-+-+-+--+-+++-++-++-+++++\| ||\-+--+--+----+---+-+-+--++++-+++++--++-++--+---+-+-+-+---++++++++----+++++-+-++--++-/ |||| | |||| +||||| |/+-+--+++++-+-+-+-+--+-+++-++-++-+++++++--++--+--+--+-\ | |/+-+--++++-+++++--++-++--+---+-+-+-+---++++++++----+++++-+-++--++-\ |||| | |||| +||||| ||| | ||||| | | | | | ||| || || ||||||| /++--+--+--+-+--+-\ ||| | |||| ||||| || || |/--+-+-+-+---++++++++----+++++-+-++--++-+-++++\| |||| +||||| ||| | ||||| | | | | | ||| || || ||||||| ||| | | | | | | ||| | |||| ||||| || || || | | | | |||||||| ||\++-+-++--++-+-+++++/ |||| +||||| ||| | ||||| | | | |/-+-+++-++-++-+++++++-+++--+--+--+-+--+-+\||| | ||||/+++++--++-++--++--+-+-+-+---++++++++----++-++-+-++--++-+-+++++\ |||| +||||| ||| | ||||| | | | || | ||| || || ||||||| ||| | | | | | ||||| | |||||||||| ^| || || | | | | |||||||| || || | || || | |||||| |||| +||||| ||| | ||||| | | | || | ||| ||/++-+++++++-+++--+--+--+\| | ||||| | \+++++++++--++-++--++--+-+-+-+---++++++++----++-+/ | || || | |||||| |||| +||||| ||| | ||||| | | |/++-+-+++-+++++-+++++++-+++--+--+--+++--+-+++++-+---+++++++++--++-++--++--+-+-+-+---++++++++----++-+-\| || || | |||||| |||| +||||| ||| | ||||| | | |||| | ||| ||||| ||||||| |||/-+--+--+++--+-+++++-+---+++++++++--++-++--++--+-+-+\| |||||||| || | || || || | |||||| |||| +||||| |||/+--+++++-+-+-++++-+-+++-+++++-+++++++-++++-+--+--+++-\| ||||| | ||\++++++--++-++--++--+-+-+++---+/|||||| || | || || || | |||||| |||| +||||| ||||| ||||| | ^ |||| | ||| |||\+-+++++++-++++-+--+--+++-++-+++++-+---++-++++++--++-++--++--+-+-+++---+-++++++<---+/ | || || || | |||||| |||| +||||| ||||| ||||| | | \+++-+-+++-+++-+-+++++++-++++-+--+--+++-++-+++++-+---++-++++/| || || || | | ||| | |||||| | | || || || | |||||| |||| +||\++---+++++--+++++-+-+--+++-+-+++-+++-+-+++++++-++++-+--+--+++-++-+++++-+---++-++++-+--++-++--/| | | ||| | |||||| | | || || || | |||||| |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |||| | | ||| || |||||/+---++-++++-+--++-++---+--+-+-+++---+-++++++----+--+-++-++--++-+\|||||| |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |||| | | ||| || ||||||| || ||||/+--++-++---+--+-+-+++---+-++++++----+-\| || || || |||||||| |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |||| | | ||| || ||||||| || \+++++--++-++---+--+-+-+++---+-++++++--->+-++-++-++--++-+++++++/ |||| +|| || ||||| ||||| | | ||| | ||| ||| | ||||||| |\++-+--+--+++-++-+++++++---++--+++++--++-++---+--+-+-+++---+-++++++----+-++-++-++--++-++++/|| |||| +\+-++---+++++--+++++-+-+--+++-+-+++-+++-+-+/||||| | || | | ||| || ||||||| || ||||| || || | | | ||| | |||||| | || || || || |||| || |||| +/+-++---+++++--+++++-+-+--+++-+-+++-+++-+-+-+++++-+-++-+--+-\||| || ||||||| || ||||| || || | | | ||| | |||||| | || || || || |||| || |||| +|| || ||||| ||||| | | ||| | ||| ||| | | ||||| |/++-+--+-++++-++-+++++++---++--+++++--++-++---+--+-+\||\---+-++++++----+-++-++-++--++-++++-++--+/|| +|| || ||||| ||||| | | \++-+-+++-+++-+-+-+++++-++++-+--+-++++-++-+++++++---++--+++++--++-++---+--+-++++----+-++++++----+-++-/| || || |||| || | || +|| || ||||| ||^\+-+-+---++-+-+++-+++-+-+-+++++-++++-+--+-++++-++-+++++++---++--+++++--+/ || | | |||| | ||||||/---+-++--+-++--++-++++-++\ | || +|| || ||||| ||| | | | || | ||| ||| | | ||||\-++++-+--+-++++-++-+++++++---++--+++++--+--++---+--+-++++----+-+++++++---+-++--+-++--/| |||| ||| | || +|| || ||||| ||| | | | || |/+++-+++-+\| |||| |||| | | |||| || ||||||| || ||||| | || | | |||| | ||||||| | || | || | |||| ||| | || +|| || ||||| ||| | | | || ||||| ||| ||| \+++--++++-+--+-++++-++-+++++++---++--+++++--+--++---+--+-++++----+-+++++++---+-++--/ || | |||| ||| | || +|^ || ||||| ||| | | | || ||||| ||| ||| ||| |||| | | |||| || ||||||| || ||||| | || | | |||| | ||||||| | || || | |||| ||| | || +|| || ||||| ||| | | | || ||||| ||| ||| ||| |||| | /+-++++-++-+++++++---++--+++++--+--++---+--+-++++----+-+++++++---+-++----++---+-++++-+++\| || +|| || /+++++--+++-+-+-+---++-+++++-+++-+++--+++--++++-+-++-++++-++\||||||| || |||\+--+--++---+--+-++++----+-+++++++---+-/| || | |||| ||||| || +|| || |||||| /+++-+-+-+---++\||||| ||| ||| ||| |||| | || |||| |||||||||| \+--+++-+--+--++---+--+-++++----+-+++++++---/ | || | |||| ||||| || +|| || |||||| |||| | | | |||||||| ||| ||| ||| |||| | |\-++++-++++++++++----+--+++-+--+--++---+--+-++++----+-++/|||| | || | |||| ||||| || +\+-++--++++++-++++-+-+-+---++++++++-+++-+++--+++--++++-+-+--/||| |||||||||| | \++-+--+--++---+--+-/||| /-+-++-++++-\ | || | |||| ||||| || + |/++--++++++-++++-+-+-+---++++++++-+++-+++--+++--++++\| | /+++-++++++++++----+---++-+--+-\|| | | ||| | | || ||||/+----+---\|| | |||| ||||| || + |||| |||||| |||| | | | |||||||| ||| ||| ||| |||||| | |||| |||||||||| | || | | ||| | | ||| | | || |||||| | ||| | |||| ||||| || + |||| |||||| |||| | | | |||||||| ||| ||| \++--++++++-+--++++-++++++++++----+---++-+--+-+++---+--+--+++--+-+-++-++++++----+---+++---+-++++-+++++-+/ + |||| |||\++-++++-/ | \---++++++++-+++-+++---++--++++++-+--++++-++++++++++----+---++-+--+-+++---+--+--+++--+-+-++-/||||| | ||| | |||| ||||| | + |||| ||| || |||| | |||||||| ||| ||| || |||||\-+--++++-++++++++++----+---++-+--+-+++---+--/ ||| | | || ||||| | ||| | |||| ||||| | + |||| ||| || |||| | ||||||||/+++-+++---++--+++++--+--++++-++++++++++----+---++-+--+-+++--\| ||| | | || ||||| | ||| | |||| ||||| | + |||| ||| || |||| /+-----++++++++++++-+++---++--+++++--+--++++-++++++++++----+---++-+\ | ||| || ||| | | || ||\++----+---+++---+-++++-++/|| | + |||\--+++-++-++++--++-----++++++++++++-+++---++--+++++--+--++++-+/|||||||| | || || | ||| || ||| | | || || || | ||| | |||| || || | + ||| ||| || |||| || |||||||||||| ||| || ||||| |/-++++-+-++++++++----+---++-++-+-+++--++-----+++-\| | || || || | ||| | |||| || || | + ||| ||| || |||| || |||||||||||| ||| || ||||| || |||| | |||||||| | || || | ||| || ||| || | || || || | ||| | |||| || || | + ||| ||| || |||| || |||||||||||| ||| || /+++++--++-++++-+-++++++++----+---++-++-+-+++--++-----+++-++-+-++--++-++----+---+++---+\|||| || || | + |||/--+++-++-++++--++-----++++++++++++-+++---++-++++++--++-++++-+-++++++++----+---++-++-+-+++\ || ||| || | || || || | ||| |||||| || || | + |||| ||| || |||| || |||||||||||| ||| || |||||| || |||| | |||||||| | || || | |||| || ||| || | || || || | ||| |||||| || || | + |||| ||| || |||| || |||||||||||| ||| || |||||| || |||| | |||||||| | || \+-+-++++-++-----+++-++-+-++--++-++----+---+++---++++/| || || | + |||| ||| || |||| || |||||||||||| ||| || |||||| || |||| | |||||||| | |\--+-+-++++-++-----+++-++-+-++--++-++----+---++/ |||| | || || | + \+++--+++-++-++++--++-----+++/|||||||| ||| || |||||| || |||| | |||||||| | | | | |||| |^ ||| || | || || || | || |||| | || || | + ||| ||| || |||| || ||| |||||||| ||| || ||\+++--++-++++-+-++++++++----+---+---+-+-++++-++-----/|| || | || || ^| | || |||| | || || | + ||| \++-++-++++--++-----+++-++++++++-+++---++-++-+++--++-++++-+-/||||||| | | | | |||| || || || | || || || | || |||| | || || | + ||| || || |||| || ||| |||||||| ||| || || ||| || |||| | ||\++++----/ | |/+-++++-++------++-++-+-++--++-++---\| || |||| | || || | + ||| || || |||| || ||| |||||||| ||| || || ||| || |||| | || |||| /-+---+++-++++-++------++-++-+-++--++-++---++---++----++++-+-++\|| | + ||| || || |||| || ||| |||||||| ||| || || ||| || \+++-+--++-++++------+-+---+++-/||| || || || | || || || || || |||| | ||||| | + ||| /++-++-++++--++-----+++-++++++++-+++---++-++-+++--++--+++-+--++-++++------+-+---+++--+++-++\/----++-++-+-++--++-++---++---++----++++-+\||||| | + ||| ||| || |||| || ||| |||||||\-+++---++-++-+++--++--+/| | || |||| | | ||| ||| |||| || || | || || || || || |||| ||||||| | + ||| ||| || |||\--++-----+++-+++++++--+++---++-++-+++--++--+-+-+--++-++++------+-+---+++--+++-++++----++-++-+-++--++-++---+/ || |||| ||||||| | + ||| ||| || ||| || ||| ||||||| ||| || || \++--++--+-+-+--++-++++------+-+---+++--+++-++++----++-++-+-/| || || | || |||| ||||||| | + \++--+++-++-+++---++-----+++-+++++++--+++---++-++--+/ || |/+-+--++-++++------+-+---+++--+++-++++----++\|| | | || || | || |||| ||||||| | + || ||| || ||| \+-----+++-+++++++--+++---++-++--+---++--+++-+--++-++++------+-+---/|| ||| |||| ||||| | | || || | || |||| ||||||| | + || ||| || ||| \-----+++-+++++/| ||| || || \---++--+++-+--++-++++------+-+----++--+++-++++----+/||| | | || || | || |||| ||||||| | + || ||| || ||| ||| \++++-+--+/| \+-++------++--+++-+--++-++++------+-+----++--+++-++++----+-+++-+--+--++-++---+----++----++++-++/|||| | + || ||| || ||| ||| |||| | | | | || || ||| | /++-++++------+-+-\ || ||| |||| | ||| | | || || | || |||| || |||| | + || ||| || |||/---------+++--++++-+--+-+----+-++------++--+++-+-+++-++++------+-+-+--++-\||| |||| | ||| | | || || | || |||| || |||| | + || ||| || |||| ||| |||| | \-+----+-++------++--+++-+-+++-++++------+-+-+--++-++++-++++----+-+++-/ | || || | || |||| || |||| | + || ||| || |||| ||| |||| | | | || || ||| | ||| |\++------+-+-+--++-++++-++++----+-+++----+--++-++---+----++----++++-++-+++/ | + || ||| || |||| ||| |||| \----+----+-++------++--+++-+-+++-+-++------+-+-+--+/ |||| |||| | ||| | || || | || |||| || ||| | + || ||| |\-++++---------+++--+/|| | | || || ||| | ||| | || | | | | |||| |||| \-+++----/ || || | || \+++-++-+++--/ + || ||| | |||| /------+++--+-++\ | | || || ||| | ||| | \+------+-+-+--+--++++-++++------+++-------++-++---+----++-----++/ || ||| + || ||| | |||| | ||| \-+++-----+----+-++------++--+++-+-+++-+--+------+-+-+--+--+/|| |||| ||| || || | || || || ||| + || ||| | |||| | ||| ||| | | || || ||| | ||| | | | | | | | || |||| ||| || || | || || || ||| + || ||| | |||| /+------+++----+++-----+----+-++------++--+++-+-+++-+--+------+-+-+--+--+-++-++++------+++-----\ || || | || || || ||| + || ||| | |||| || |\+----+++-----+----+-++------++--+++-+-++/ | | | |/+--+--+-++-++++------+++-----+-++-++---+----++----\|| || ||| + || ||| | |||| || | | ||| | | || || |\+-+-++--+--+------+-+++--+--+-++-++++------/|| /--+-++-++---+\ || ||| || ||| + \+--+++-+--++++-++------/ | ||| \----+-++------++--+-+-+-++--+--+------+-+++--+--+-/| |||| || | | || || || || ||| || ||| + | ||| | |\++-++--------+----+++----------+-++------++--/ | | \+--+--+------+-++/ | | | |||| || | | || || || || ||| || ||| + | ||| | | || || /---+----+++----------+\|| || | | | | | | || | | | |||| || | | || || || || ||| || ||| + | ||| | | || || | | ||| |||| || | | | | | | || \--+--+-++++-------++--+--+-++-++---/| || ||| || ||| + | ||| | | || || | | ||| |||| || | | | | | | \+------+--+-++++-------++--+--+-+/ \+----+---/| ||| || ||| + | ||| | | || || | | ||| |||| || | | | | \------+--+------+--+-++++-------++--+--+-/ | | | ||| || ||| + \--+++-+--+-++-++----+---+----+++----------++++------++----+-+--+--+----<----+--+------+--/ |||| || | | | | | ||| || ||| + \++-+--+-++-++----+---+----+++----------++++------++----+-+--+--+---------+--+------+----++/| || | | | | | ||| || ||| + || | | || || | | ||| /----++++------++----+-+--+--+---------+--+------+----++-+-------++--+-\| | | | ||| || ||| + || | | || ||/---+---+----+++-----+----++++------++----+-+--+--+---------+--+------+----++-+-------++--+-++-----+----+-\ | ||| || ||| + || | | || ||| | | ||| | |||| || | | | | \--+------+----++-+-------++--+-++-----+----+-+--+----+++--++-+/| + |\-+--+-++-+++---+---+----+++-----+----++++------++-<--/ | | | | | || | || | || | | | | ||| || | | + | | | || ||| | | ||| | |||| || | | | | | || \-------++--+-++-----+----+-+--+----+++--+/ | | + /+--+--+-++-+++---+---+----+++-----+----++++------++------+--+--+------------+----\ | || || | || | | | | ||| | | | + || | | |\-+++---+---+----+++-----+----++++------++------+--+--+------------+----+-/ || || | || | | | | ||| | | v + || | | | ||| | | |\+-----+----++++------++------+--+--+------------+----+------/| || | || | | | | ||| | | | + || | | | \++---+---+----+-+-----+----++++------++------+--+--+------------+----+-------+---------++--+-+/ | | | | ||| | | | + || | \-+---++---+---/ | | \----++++------++------+--+--+------------+----+-------+---------++--+-/ | | | | ||| | | | + || | \---++---+--------/ | |||| || | | | | | | |\--+--------/ | | | ||| | | | + || | || | | |||| || | | | \----+-------+---------+---+-------------+-+--+----/|| | | | + || \--------++---+----------+----------++++------++------/ | | | | | | | | | || | | | + || || | | |||| || | \-----------------+-------+---------+---+----<--------+-+--+-----+/ | | | + || |\---+----------+----------++++------++---------+--------------------+-------+---------+---+-------------+-/ | | | | | + || | | | |||\------++---------/ | | | | | | | | | | + |\-----------+----+----------+----------/|| || | \---------+---+-------------+----+-----+---+--/ | + \------------+----+----------+-----------++-------++------------------------------/ | | | \-----+---/ | + | | | || \+------------------------------------------------+---+-------------+----------+--------/ + | \----------+-----------/| \------------------------------------------------/ | | | + \---------------/ | \-------------/ | + \--------------------------------<-----------------------------------------------------/ diff --git a/2018/day-13/solution.js b/2018/day-13/solution.js index e6796dc..c6a5aab 100644 --- a/2018/day-13/solution.js +++ b/2018/day-13/solution.js @@ -1,19 +1,23 @@ -const { - Track -} = require('./tracks') +const { loadInput } = require('./helpers') +const { Track } = require('./tracks') -const data = `/->-\\ -| | /----\\ -| /-+--+-\\ | -| | | | v | -\\-+-/ \\-+--/ - \\------/ ` - -const track = new Track(data) -while (track.collision === false) { - track.advance() +const init = (data) => { + const track = new Track(data) + while (track.collision === false) { + try { + track.advance() + } catch (err) { + console.error(`Reached a collision at ${track.collision.x},${track.collision.y} on frame ${track.frame}`) + } + } + const answer = [track.collision.x, track.collision.y] + // console.log(`Track state:`) + // console.log(track.display()) + const answer2 = '' + console.log(`-- Part 1 --`) + console.log(`Answer: ${answer}`) + console.log(`-- Part 2 --`) + console.log(`Answer: ${answer2}`) } -console.error(`Reached a collision at ${track.collision.x},${track.collision.y} on frame ${track.frame}`) -console.log(`Track state:`) -console.log(track.display()) +loadInput(init) 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