Skip to content

Commit 288a206

Browse files
committed
Solve day 03 challenge
1 parent fbf30f1 commit 288a206

File tree

4 files changed

+1172
-0
lines changed

4 files changed

+1172
-0
lines changed

day03/answer.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import fs from "fs"
2+
3+
const readInput = fs.readFileSync(__dirname + '/input.txt', 'utf-8');
4+
5+
const formattedInput = readInput.trim().split('\n');
6+
7+
export const calculatePower = (diagnosticReport) => {
8+
let gamma = ""
9+
let epsilon = ""
10+
11+
const singleReportLength = diagnosticReport[0].length
12+
13+
for (let i = 0; i < singleReportLength; i++) {
14+
let zeroCount = 0
15+
let oneCount = 0
16+
17+
for (let j = 0; j < diagnosticReport.length; j++) {
18+
diagnosticReport[j][i] === "0" ? zeroCount += 1 : oneCount += 1
19+
}
20+
21+
if (zeroCount > oneCount) {
22+
gamma += "0"
23+
epsilon += "1"
24+
} else {
25+
gamma += "1"
26+
epsilon += "0"
27+
}
28+
}
29+
30+
return parseInt(gamma, 2) * parseInt(epsilon, 2)
31+
}
32+
33+
export const calculateLifeSupportRating = (diagnosticReport) => {
34+
35+
let oxygenRating;
36+
let co2Rating;
37+
38+
let oxyIndex = 1
39+
let co2Index = 1
40+
41+
42+
const selectRating = (values, index) => {
43+
44+
let zeroCount = 0
45+
let oneCount = 0
46+
47+
for (let value of values) {
48+
value[index] === "0" ? zeroCount += 1 : oneCount += 1
49+
}
50+
51+
if (zeroCount > oneCount) {
52+
oxygenRating = values.filter(val => val[index] === "0")
53+
co2Rating = values.filter(val => val[index] === "1")
54+
} else if (zeroCount < oneCount) {
55+
oxygenRating = values.filter(val => val[index] === "1")
56+
co2Rating = values.filter(val => val[index] === "0")
57+
} else {
58+
oxygenRating = values.filter(val => val[index] === "1")
59+
co2Rating = values.filter(val => val[index] === "0")
60+
}
61+
}
62+
63+
const initialize = () => selectRating(diagnosticReport, 0)
64+
65+
initialize()
66+
67+
while (oxygenRating.length !== 1) {
68+
selectRating(oxygenRating, oxyIndex)
69+
oxyIndex += 1
70+
}
71+
72+
const finalOxyRating = oxygenRating[0]
73+
74+
initialize()
75+
76+
while (co2Rating.length !== 1) {
77+
selectRating(co2Rating, co2Index)
78+
co2Index += 1
79+
}
80+
81+
const finalCo2Rating = co2Rating[0]
82+
83+
return parseInt(finalOxyRating, 2) * parseInt(finalCo2Rating, 2)
84+
}
85+
86+
console.log("The power consumption is: ", calculatePower(formattedInput))
87+
console.log("The life support rating is: ", calculateLifeSupportRating(formattedInput))

day03/challenge.txt

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--- Day 3: Binary Diagnostic ---
2+
3+
The submarine has been making some odd creaking noises, so you ask it to produce a diagnostic report just in case.
4+
5+
The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the power consumption.
6+
7+
You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.
8+
9+
Each bit in the gamma rate can be determined by finding the most common bit in the corresponding position of all numbers in the diagnostic report. For example, given the following diagnostic report:
10+
11+
00100
12+
11110
13+
10110
14+
10111
15+
10101
16+
01111
17+
00111
18+
11100
19+
10000
20+
11001
21+
00010
22+
01010
23+
Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.
24+
25+
The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.
26+
27+
The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.
28+
29+
So, the gamma rate is the binary number 10110, or 22 in decimal.
30+
31+
The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or 9 in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, 198.
32+
33+
Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the submarine? (Be sure to represent your answer in decimal, not binary.)
34+
35+
To begin, get your puzzle input.
36+
37+
Answer:
38+
39+
--- Part Two ---
40+
41+
Next, you should verify the life support rating, which can be determined by multiplying the oxygen generator rating by the CO2 scrubber rating.
42+
43+
Both the oxygen generator rating and the CO2 scrubber rating are values that can be found in your diagnostic report - finding them is the tricky part. Both values are located using a similar process that involves filtering out values until only one remains. Before searching for either rating value, start with the full list of binary numbers from your diagnostic report and consider just the first bit of those numbers. Then:
44+
45+
Keep only numbers selected by the bit criteria for the type of rating value for which you are searching. Discard numbers which do not match the bit criteria.
46+
If you only have one number left, stop; this is the rating value for which you are searching.
47+
Otherwise, repeat the process, considering the next bit to the right.
48+
The bit criteria depends on which type of rating value you want to find:
49+
50+
To find oxygen generator rating, determine the most common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 1 in the position being considered.
51+
To find CO2 scrubber rating, determine the least common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 0 in the position being considered.
52+
For example, to determine the oxygen generator rating value using the same example diagnostic report from above:
53+
54+
Start with all 12 numbers and consider only the first bit of each number. There are more 1 bits (7) than 0 bits (5), so keep only the 7 numbers with a 1 in the first position: 11110, 10110, 10111, 10101, 11100, 10000, and 11001.
55+
Then, consider the second bit of the 7 remaining numbers: there are more 0 bits (4) than 1 bits (3), so keep only the 4 numbers with a 0 in the second position: 10110, 10111, 10101, and 10000.
56+
In the third position, three of the four numbers have a 1, so keep those three: 10110, 10111, and 10101.
57+
In the fourth position, two of the three numbers have a 1, so keep those two: 10110 and 10111.
58+
In the fifth position, there are an equal number of 0 bits and 1 bits (one each). So, to find the oxygen generator rating, keep the number with a 1 in that position: 10111.
59+
As there is only one number left, stop; the oxygen generator rating is 10111, or 23 in decimal.
60+
Then, to determine the CO2 scrubber rating value from the same example above:
61+
62+
Start again with all 12 numbers and consider only the first bit of each number. There are fewer 0 bits (5) than 1 bits (7), so keep only the 5 numbers with a 0 in the first position: 00100, 01111, 00111, 00010, and 01010.
63+
Then, consider the second bit of the 5 remaining numbers: there are fewer 1 bits (2) than 0 bits (3), so keep only the 2 numbers with a 1 in the second position: 01111 and 01010.
64+
In the third position, there are an equal number of 0 bits and 1 bits (one each). So, to find the CO2 scrubber rating, keep the number with a 0 in that position: 01010.
65+
As there is only one number left, stop; the CO2 scrubber rating is 01010, or 10 in decimal.
66+
Finally, to find the life support rating, multiply the oxygen generator rating (23) by the CO2 scrubber rating (10) to get 230.
67+
68+
Use the binary numbers in your diagnostic report to calculate the oxygen generator rating and CO2 scrubber rating, then multiply them together. What is the life support rating of the submarine? (Be sure to represent your answer in decimal, not binary.)
69+
70+
Answer:
71+
72+
Although it hasn't changed, you can still get your puzzle input.

0 commit comments

Comments
 (0)