|
1 | 1 | /* eslint-env mocha */
|
2 | 2 | const expect = require('chai').expect
|
| 3 | +const { |
| 4 | + parseLine |
| 5 | +} = require('./helpers') |
3 | 6 | const {
|
4 | 7 | Plants
|
5 | 8 | } = require('./plants')
|
6 | 9 |
|
7 | 10 | const initialState = '#..#.#..##......###...###'
|
| 11 | +const rules = `...## => # |
| 12 | +..#.. => # |
| 13 | +.#... => # |
| 14 | +.#.#. => # |
| 15 | +.#.## => # |
| 16 | +.##.. => # |
| 17 | +.#### => # |
| 18 | +#.#.# => # |
| 19 | +#.### => # |
| 20 | +##.#. => # |
| 21 | +##.## => # |
| 22 | +###.. => # |
| 23 | +###.# => # |
| 24 | +####. => #`.split('\n').map(parseLine) |
8 | 25 |
|
9 | 26 | describe('--- Day 12: Subterranean Sustainability ---', () => {
|
10 | 27 | describe('Plants:', () => {
|
@@ -42,5 +59,99 @@ describe('--- Day 12: Subterranean Sustainability ---', () => {
|
42 | 59 | expect(actual).to.deep.equal(expected)
|
43 | 60 | })
|
44 | 61 | })
|
| 62 | + describe('advance()', () => { |
| 63 | + it('advances the next generation', () => { |
| 64 | + const expected = [ |
| 65 | + { position: -2, state: '.' }, |
| 66 | + { position: -1, state: '.' }, |
| 67 | + { position: 0, state: '#' }, |
| 68 | + { position: 1, state: '.' }, |
| 69 | + { position: 2, state: '.' }, |
| 70 | + { position: 3, state: '.' }, |
| 71 | + { position: 4, state: '#' }, |
| 72 | + { position: 5, state: '.' }, |
| 73 | + { position: 6, state: '.' }, |
| 74 | + { position: 7, state: '.' }, |
| 75 | + { position: 8, state: '.' }, |
| 76 | + { position: 9, state: '#' }, |
| 77 | + { position: 10, state: '.' }, |
| 78 | + { position: 11, state: '.' }, |
| 79 | + { position: 12, state: '.' }, |
| 80 | + { position: 13, state: '.' }, |
| 81 | + { position: 14, state: '.' }, |
| 82 | + { position: 15, state: '#' }, |
| 83 | + { position: 16, state: '.' }, |
| 84 | + { position: 17, state: '.' }, |
| 85 | + { position: 18, state: '#' }, |
| 86 | + { position: 19, state: '.' }, |
| 87 | + { position: 20, state: '.' }, |
| 88 | + { position: 21, state: '#' }, |
| 89 | + { position: 22, state: '.' }, |
| 90 | + { position: 23, state: '.' }, |
| 91 | + { position: 24, state: '#' }, |
| 92 | + { position: 25, state: '.' }, |
| 93 | + { position: 26, state: '.' } |
| 94 | + ] |
| 95 | + let plantTracker = new Plants(initialState, rules) |
| 96 | + plantTracker.advance() |
| 97 | + const actual = plantTracker.generations[1] |
| 98 | + expect(actual).to.deep.equal(expected) |
| 99 | + }) |
| 100 | + }) |
| 101 | + describe('predictPlant(pattern)', () => { |
| 102 | + it('retrieves the expected state based on the specified pattern', () => { |
| 103 | + const pattern = '#.#.#' |
| 104 | + const expected = '#' |
| 105 | + let plantTracker = new Plants(initialState, rules) |
| 106 | + const actual = plantTracker.predictPlant(pattern) |
| 107 | + expect(actual).to.equal(expected) |
| 108 | + }) |
| 109 | + }) |
| 110 | + describe('getDisplay()', () => { |
| 111 | + it('gets a visual display of the generations', () => { |
| 112 | + let expected = `...#..#.#..##......###...###........... |
| 113 | + ...#...#....#.....#..#..#..#........... |
| 114 | + ...##..##...##....#..#..#..##..........` |
| 115 | + expected = expected.replace(/ /g, '') |
| 116 | + const plantTracker = new Plants(initialState, rules) |
| 117 | + for (let gen = 1; gen <= 2; gen++) { |
| 118 | + plantTracker.advance() |
| 119 | + } |
| 120 | + const actual = plantTracker.getDisplay(-3, 35) |
| 121 | + expect(actual).to.equal(expected) |
| 122 | + }) |
| 123 | + }) |
| 124 | + describe('getDisplay()', () => { |
| 125 | + it('supports optional boundaries', () => { |
| 126 | + let expected = `...#..#.#..##......###...###........... |
| 127 | + ...#...#....#.....#..#..#..#........... |
| 128 | + ...##..##...##....#..#..#..##.......... |
| 129 | + ..#.#...#..#.#....#..#..#...#.......... |
| 130 | + ...#.#..#...#.#...#..#..##..##......... |
| 131 | + ....#...##...#.#..#..#...#...#......... |
| 132 | + ....##.#.#....#...#..##..##..##........ |
| 133 | + ...#..###.#...##..#...#...#...#........ |
| 134 | + ...#....##.#.#.#..##..##..##..##....... |
| 135 | + ...##..#..#####....#...#...#...#....... |
| 136 | + ..#.#..#...#.##....##..##..##..##...... |
| 137 | + ...#...##...#.#...#.#...#...#...#...... |
| 138 | + ...##.#.#....#.#...#.#..##..##..##..... |
| 139 | + ..#..###.#....#.#...#....#...#...#..... |
| 140 | + ..#....##.#....#.#..##...##..##..##.... |
| 141 | + ..##..#..#.#....#....#..#.#...#...#.... |
| 142 | + .#.#..#...#.#...##...#...#.#..##..##... |
| 143 | + ..#...##...#.#.#.#...##...#....#...#... |
| 144 | + ..##.#.#....#####.#.#.#...##...##..##.. |
| 145 | + .#..###.#..#.#.#######.#.#.#..#.#...#.. |
| 146 | + .#....##....#####...#######....#.#..##.` |
| 147 | + expected = expected.replace(/ /g, '') |
| 148 | + const plantTracker = new Plants(initialState, rules) |
| 149 | + for (let gen = 1; gen <= 20; gen++) { |
| 150 | + plantTracker.advance() |
| 151 | + } |
| 152 | + const actual = plantTracker.getDisplay() |
| 153 | + expect(actual).to.equal(expected) |
| 154 | + }) |
| 155 | + }) |
45 | 156 | })
|
46 | 157 | })
|
0 commit comments