Skip to content

Commit 4f34c30

Browse files
feat(2021-day-01): use a sampled depth measurement for depth calculations
Solves part #2
1 parent bdb7944 commit 4f34c30

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

2021/day-01/solution.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
44
const { inputToArray } = require('../../2018/inputParser')
5-
const { countIncreasingDepth } = require('./sonarSweep')
5+
const { countIncreasingDepth, countIncreasingSampledDepth } = require('./sonarSweep')
66

77
fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
88
if (err) throw err
@@ -21,8 +21,7 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
2121

2222
const part2 = () => {
2323
const data = resetInput()
24-
console.debug(data)
25-
return 'No answer yet'
24+
return countIncreasingSampledDepth(data)
2625
}
2726
const answers = []
2827
answers.push(part1())

2021/day-01/sonarSweep.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,37 @@ const countIncreasingDepth = (pings) => {
1717
return result
1818
}
1919

20+
const sampledDepth = (depths, target) => {
21+
// console.debug(`sum of ${depths[target]} + ${depths[target + 1]} + ${depths[target + 2]}`)
22+
return depths[target] + depths[target + 1] + depths[target + 2]
23+
}
24+
25+
const countIncreasingSampledDepth = (pings) => {
26+
const result = pings.reduce((total, curr, idx, pings) => {
27+
// skip first
28+
if (idx <= 0) {
29+
return 0
30+
}
31+
32+
// Skip measureent if inusfficient remaining samples
33+
if (pings.length - idx < 3) {
34+
return total
35+
}
36+
37+
// Increment count when sampled depth is larger than the previous sampled depth
38+
if (sampledDepth(pings, idx) > sampledDepth(pings, idx - 1)) {
39+
total++
40+
return total
41+
}
42+
43+
// Otherwise don't increase
44+
return total
45+
}, 0)
46+
47+
return result
48+
}
49+
2050
module.exports = {
21-
countIncreasingDepth
51+
countIncreasingDepth,
52+
countIncreasingSampledDepth
2253
}

2021/day-01/sonarSweep.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { countIncreasingDepth } = require('./sonarSweep')
3+
const { countIncreasingDepth, countIncreasingSampledDepth } = require('./sonarSweep')
44

55
const pings = [
66
199,
@@ -19,6 +19,12 @@ const singlePing = [
1919
199
2020
]
2121

22+
const triplePing = [
23+
269,
24+
260,
25+
263
26+
]
27+
2228
describe('--- Day 1: Sonar Sweep ---', () => {
2329
describe('Part 1', () => {
2430
describe('countIncreasingDepth()', () => {
@@ -31,4 +37,20 @@ describe('--- Day 1: Sonar Sweep ---', () => {
3137
})
3238
})
3339
})
40+
41+
describe('Part 2', () => {
42+
describe('countIncreasingSampledDepth()', () => {
43+
it('counts how many times the depth increases between two sequential measurements in an array with measurments averaged over 3 samples', () => {
44+
expect(countIncreasingSampledDepth(pings)).to.equal(5)
45+
})
46+
47+
it('skips the first measurement since there are no previous', () => {
48+
expect(countIncreasingSampledDepth(singlePing)).to.equal(0)
49+
})
50+
51+
it('skips the measurements when there aren`t enough values ot measure', () => {
52+
expect(countIncreasingSampledDepth(triplePing)).to.equal(0)
53+
})
54+
})
55+
})
3456
})

0 commit comments

Comments
 (0)