-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2021-day-06): count how many laternfish exist after 80 days
solves part 1
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
let _fishes = [] | ||
const NewFishAge = 8 // age of newly spawned fish | ||
const FishSpawnAge = 0 // age when the fish spawns | ||
const ResetFishAge = 6 // age of the original fish after spawning | ||
|
||
const ageFish = (age) => { | ||
if (age > NewFishAge) { throw new Error('Fish is too young') } | ||
if (age < FishSpawnAge) { throw new Error('Fish is too old') } | ||
if (age === FishSpawnAge) { return ResetFishAge } | ||
return age - 1 | ||
} | ||
|
||
const spawn = (qty) => { | ||
console.debug(`spawning ${qty} fish`) | ||
const newFishes = [...new Array(qty)].map(() => NewFishAge) | ||
_fishes.push(...newFishes) | ||
} | ||
|
||
const school = { | ||
get state () { | ||
return _fishes | ||
}, | ||
set state (state) { | ||
_fishes = state | ||
}, | ||
|
||
advance: () => { | ||
// Calculate how many will spawn | ||
const toSpawn = _fishes.filter((x) => x === FishSpawnAge).length | ||
// Iterate each fish | ||
_fishes = _fishes.map(ageFish) | ||
// Spawn the new fish | ||
spawn(toSpawn) | ||
} | ||
} | ||
|
||
module.exports = { | ||
school, | ||
ageFish, | ||
spawn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-env mocha */ | ||
const { expect } = require('chai') | ||
const { school, ageFish, spawn } = require('./fish') | ||
|
||
describe('--- Day 6: Lanternfish ---', () => { | ||
describe('Part 1', () => { | ||
beforeEach(() => { | ||
// ensure flushed state | ||
school.state = [3, 4, 3, 1, 2] | ||
expect(school.state).to.deep.equal([3, 4, 3, 1, 2]) | ||
}) | ||
describe('spawn()', () => { | ||
it('adds new fish to the end of the list', () => { | ||
spawn(4) | ||
expect(school.state).to.deep.equal([3, 4, 3, 1, 2, 8, 8, 8, 8]) | ||
}) | ||
}) | ||
describe('ageFish()', () => { | ||
it('ages a particular fish', () => { | ||
expect(ageFish(6)).to.equal(5) | ||
expect(ageFish(5)).to.equal(4) | ||
expect(ageFish(4)).to.equal(3) | ||
expect(ageFish(3)).to.equal(2) | ||
expect(ageFish(2)).to.equal(1) | ||
expect(ageFish(1)).to.equal(0) | ||
expect(ageFish(0)).to.equal(6) | ||
expect(ageFish(8)).to.equal(7) | ||
expect(ageFish(7)).to.equal(6) | ||
}) | ||
it('throws an error if the fish is out of range', () => { | ||
expect(() => { ageFish(9) }).to.throw('Fish is too young') | ||
expect(() => { ageFish(-1) }).to.throw('Fish is too old') | ||
}) | ||
}) | ||
describe('advance()', () => { | ||
it('advances one day', () => { | ||
school.state = [3, 4, 3, 1, 2] | ||
school.advance() | ||
expect(school.state).to.deep.equal([2, 3, 2, 0, 1]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([1, 2, 1, 6, 0, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([0, 1, 0, 5, 6, 7, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([6, 0, 6, 4, 5, 6, 7, 8, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([5, 6, 5, 3, 4, 5, 6, 7, 7, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([4, 5, 4, 2, 3, 4, 5, 6, 6, 7]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([3, 4, 3, 1, 2, 3, 4, 5, 5, 6]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([2, 3, 2, 0, 1, 2, 3, 4, 4, 5]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([1, 2, 1, 6, 0, 1, 2, 3, 3, 4, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([0, 1, 0, 5, 6, 0, 1, 2, 2, 3, 7, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([6, 0, 6, 4, 5, 6, 0, 1, 1, 2, 6, 7, 8, 8, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([5, 6, 5, 3, 4, 5, 6, 0, 0, 1, 5, 6, 7, 7, 7, 8, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([4, 5, 4, 2, 3, 4, 5, 6, 6, 0, 4, 5, 6, 6, 6, 7, 7, 8, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([3, 4, 3, 1, 2, 3, 4, 5, 5, 6, 3, 4, 5, 5, 5, 6, 6, 7, 7, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([2, 3, 2, 0, 1, 2, 3, 4, 4, 5, 2, 3, 4, 4, 4, 5, 5, 6, 6, 7]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([1, 2, 1, 6, 0, 1, 2, 3, 3, 4, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([0, 1, 0, 5, 6, 0, 1, 2, 2, 3, 0, 1, 2, 2, 2, 3, 3, 4, 4, 5, 7, 8]) | ||
school.advance() | ||
expect(school.state).to.deep.equal([6, 0, 6, 4, 5, 6, 0, 1, 1, 2, 6, 0, 1, 1, 1, 2, 2, 3, 3, 4, 6, 7, 8, 8, 8, 8]) | ||
school.advance() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const console = require('../helpers') | ||
require('./solution') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1,1,3,5,1,3,2,1,5,3,1,4,4,4,1,1,1,3,1,4,3,1,2,2,2,4,1,1,5,5,4,3,1,1,1,1,1,1,3,4,1,2,2,5,1,3,5,1,3,2,5,2,2,4,1,1,1,4,3,3,3,1,1,1,1,3,1,3,3,4,4,1,1,5,4,2,2,5,4,5,2,5,1,4,2,1,5,5,5,4,3,1,1,4,1,1,3,1,3,4,1,1,2,4,2,1,1,2,3,1,1,1,4,1,3,5,5,5,5,1,2,2,1,3,1,2,5,1,4,4,5,5,4,1,1,3,3,1,5,1,1,4,1,3,3,2,4,2,4,1,5,5,1,2,5,1,5,4,3,1,1,1,5,4,1,1,4,1,2,3,1,3,5,1,1,1,2,4,5,5,5,4,1,4,1,4,1,1,1,1,1,5,2,1,1,1,1,2,3,1,4,5,5,2,4,1,5,1,3,1,4,1,1,1,4,2,3,2,3,1,5,2,1,1,4,2,1,1,5,1,4,1,1,5,5,4,3,5,1,4,3,4,4,5,1,1,1,2,1,1,2,1,1,3,2,4,5,3,5,1,2,2,2,5,1,2,5,3,5,1,1,4,5,2,1,4,1,5,2,1,1,2,5,4,1,3,5,3,1,1,3,1,4,4,2,2,4,3,1,1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const filePath = path.join(__dirname, 'input.txt') | ||
const { parseData } = require('../../2018/inputParser') | ||
const { school } = require('./fish') | ||
|
||
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => { | ||
if (err) throw err | ||
|
||
initData = parseData(initData.trim()) | ||
|
||
const resetInput = () => { | ||
// Deep copy to ensure we aren't mutating the original data | ||
return JSON.parse(JSON.stringify(initData)) | ||
} | ||
|
||
const part1 = () => { | ||
const data = resetInput() | ||
school.state = data | ||
// Advance the designated time | ||
for (let x = 0; x < 80; x++) { | ||
school.advance() | ||
} | ||
// Count how many fish we have | ||
return school.state.length | ||
} | ||
|
||
const part2 = () => { | ||
const data = resetInput() | ||
console.debug(data) | ||
return 'No answer yet' | ||
} | ||
const answers = [] | ||
answers.push(part1()) | ||
answers.push(part2()) | ||
|
||
answers.forEach((ans, idx) => { | ||
console.info(`-- Part ${idx + 1} --`) | ||
console.info(`Answer: ${ans}`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
require('./2021/day-05/solution') | ||
require('./2021/day-06/solution') |