Skip to content

Commit 8179a3e

Browse files
Merge pull request #16 from fullstackzach/day-3
feat: day3 part 2
2 parents 45c87b4 + 0290242 commit 8179a3e

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

day3/day3.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,60 @@ function mostCommon(arr, bitIndex) {
7676
return -1
7777
}
7878

79+
function reduceTargetBit(data, index, targetBit) {
80+
return data.reduce((matchingVals, current) => {
81+
if (Number.parseInt(current[index]) === targetBit) {
82+
matchingVals.push(current)
83+
}
84+
return matchingVals
85+
}, [])
86+
}
87+
88+
function flipAbit(bit) {
89+
return bit === 0 ? 1 : 0
90+
}
91+
92+
function oxygenGeneratorRating(data, index = 0) {
93+
const result = mostCommon(data, index)
94+
const commonBit = result === -1 ? 1 : result
95+
96+
if (data.length === 1) {
97+
return binaryStringToBase10(data[0])
98+
}
99+
100+
const arr = reduceTargetBit(data, index, commonBit)
101+
102+
return oxygenGeneratorRating(arr, ++index)
103+
}
104+
105+
function co2scrubberRating(data, index = 0) {
106+
const result = mostCommon(data, index)
107+
const leastCommonBit = result === -1 ? 0 : flipAbit(result)
108+
109+
if (data.length === 1) {
110+
return binaryStringToBase10(data[0])
111+
}
112+
113+
const arr = reduceTargetBit(data, index, leastCommonBit)
114+
115+
return co2scrubberRating(arr, ++index)
116+
}
117+
118+
function part2solution() {
119+
const data = parse(diagnostics)
120+
const oxyRating = oxygenGeneratorRating(data)
121+
const co2Rating = co2scrubberRating(data)
122+
return oxyRating * co2Rating
123+
}
124+
79125
export {
80126
parse,
81127
calcGammaBits,
82128
flipBits,
83129
binaryStringToBase10,
84130
part1solution,
85131
mostCommon,
132+
oxygenGeneratorRating,
133+
co2scrubberRating,
134+
part2solution,
86135
}

day3/day3.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
binaryStringToBase10,
66
part1solution,
77
mostCommon,
8+
oxygenGeneratorRating,
9+
co2scrubberRating,
10+
part2solution,
811
} from './day3.js'
912

1013
describe('parse data', () => {
@@ -129,3 +132,45 @@ describe('Part 2 - mostCommon', () => {
129132
expect(mostCommon(parse(exampleData), 0)).toEqual(0)
130133
})
131134
})
135+
136+
describe('Part 2 - oxygenGeneratorRating', () => {
137+
test('with example data', () => {
138+
const exampleData = `00100
139+
11110
140+
10110
141+
10111
142+
10101
143+
01111
144+
00111
145+
11100
146+
10000
147+
11001
148+
00010
149+
01010`
150+
expect(oxygenGeneratorRating(parse(exampleData))).toEqual(23)
151+
})
152+
})
153+
154+
describe('Part 2 - co2scrubberRating', () => {
155+
test('with example data', () => {
156+
const exampleData = `00100
157+
11110
158+
10110
159+
10111
160+
10101
161+
01111
162+
00111
163+
11100
164+
10000
165+
11001
166+
00010
167+
01010`
168+
expect(co2scrubberRating(parse(exampleData))).toEqual(10)
169+
})
170+
})
171+
172+
describe('Part 2 - final solution', () => {
173+
test('with solution data', () => {
174+
expect(part2solution()).toEqual(6677951)
175+
})
176+
})

0 commit comments

Comments
 (0)