Skip to content

Commit 8808db6

Browse files
feat(2018 day-10): render animation frame to a buffer
1 parent 1a9ec32 commit 8808db6

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

2018/day-10/display.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Renders a frame to page
3+
* @param {Object} frame to render
4+
* @param {Number} dim [w,h] dimensions of display
5+
* @param {Array} origin [x,y] coordinates of origin
6+
*/
7+
const show = (frame, dim, origin) => {
8+
let buffer = ''
9+
for (let row = origin[1]; row < origin[1] + dim[1]; row++) {
10+
for (let col = origin[0]; col < origin[0] + dim[0]; col++) {
11+
let point = frame.contents.find((pt) => (col === pt.x && row === pt.y))
12+
buffer += (typeof point !== 'undefined') ? '#' : '.'
13+
}
14+
buffer += '\n'
15+
}
16+
buffer = buffer.trim() // Remove exces whitespace
17+
console.log(Array(dim[0]).fill('-').join('')) // border line
18+
console.log(buffer)
19+
console.log(Array(dim[0]).fill('-').join('')) // border line
20+
console.log(`Focus ratio: ${frame.meta.focus}`) // Metadata
21+
return buffer
22+
}
23+
24+
module.exports = {
25+
show
26+
}

2018/day-10/display.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* eslint-env mocha */
2+
const expect = require('chai').expect
3+
const {
4+
parseLine
5+
} = require('./helpers')
6+
const {
7+
Beacon
8+
} = require('./beacons')
9+
const {
10+
show
11+
} = require('./display')
12+
13+
describe('--- Day 10: The Stars Align ---', () => {
14+
describe('Display:', () => {
15+
describe('show(frame, dimension, origin)', () => {
16+
it('renders the frame to the display and returns it as a buffer', () => {
17+
const expected = `......................
18+
......................
19+
......................
20+
......................
21+
......#...#..###......
22+
......#...#...#.......
23+
......#...#...#.......
24+
......#####...#.......
25+
......#...#...#.......
26+
......#...#...#.......
27+
......#...#...#.......
28+
......#...#..###......
29+
......................
30+
......................
31+
......................
32+
......................`
33+
let testData = `position=< 9, 1> velocity=< 0, 2>
34+
position=< 7, 0> velocity=<-1, 0>
35+
position=< 3, -2> velocity=<-1, 1>
36+
position=< 6, 10> velocity=<-2, -1>
37+
position=< 2, -4> velocity=< 2, 2>
38+
position=<-6, 10> velocity=< 2, -2>
39+
position=< 1, 8> velocity=< 1, -1>
40+
position=< 1, 7> velocity=< 1, 0>
41+
position=<-3, 11> velocity=< 1, -2>
42+
position=< 7, 6> velocity=<-1, -1>
43+
position=<-2, 3> velocity=< 1, 0>
44+
position=<-4, 3> velocity=< 2, 0>
45+
position=<10, -3> velocity=<-1, 1>
46+
position=< 5, 11> velocity=< 1, -2>
47+
position=< 4, 7> velocity=< 0, -1>
48+
position=< 8, -2> velocity=< 0, 1>
49+
position=<15, 0> velocity=<-2, 0>
50+
position=< 1, 6> velocity=< 1, 0>
51+
position=< 8, 9> velocity=< 0, -1>
52+
position=< 3, 3> velocity=<-1, 1>
53+
position=< 0, 5> velocity=< 0, -1>
54+
position=<-2, 2> velocity=< 2, 0>
55+
position=< 5, -2> velocity=< 1, 2>
56+
position=< 1, 4> velocity=< 2, 1>
57+
position=<-2, 7> velocity=< 2, -2>
58+
position=< 3, 6> velocity=<-1, -1>
59+
position=< 5, 0> velocity=< 1, 0>
60+
position=<-6, 0> velocity=< 2, 0>
61+
position=< 5, 9> velocity=< 1, -2>
62+
position=<14, 7> velocity=<-2, 0>
63+
position=<-3, 6> velocity=< 2, -1>`.split('\n').map(parseLine)
64+
65+
const beaconTracker = new Beacon(testData)
66+
const actual = show(beaconTracker.getFrame(3), [22, 16], [-6, -4])
67+
expect(actual).to.equal(expected)
68+
})
69+
})
70+
})
71+
})

0 commit comments

Comments
 (0)