Public
Edited
Dec 6, 2021
Insert cell
Insert cell
Insert cell
function find_gamma_epsilon_rates(report) {

let gamma_binary = [];
let epsilon_binary = [];

for (let i = 0; i < report[0].length; ++i) {
let column = report.map((report) => report[i][0]).join('');
if (column.match(/1/g).length > column.match(/0/g).length) {
gamma_binary.push('1');
epsilon_binary.push('0');
}
else {
gamma_binary.push('0');
epsilon_binary.push('1');
}
}

let gamma_rate = parseInt(gamma_binary.join(''), 2)
let epsilon_rate = parseInt(epsilon_binary.join(''), 2)
return [gamma_rate, epsilon_rate]
}
Insert cell
Insert cell
find_gamma_epsilon_rates(test_diagnostic_report).reduce((a, b)=> a*b, 1) // Should return 198
Insert cell
Insert cell
find_gamma_epsilon_rates(diagnostic_report).reduce((a, b)=> a*b, 1)
Insert cell
Insert cell
function find_most_common_bits(report) {
let most_common_bits = [];
console.log('in fmcb', report[0]);
console.log('in fmcb', report[0].length);
for (let i = 0; i < report[0].length; ++i) {
let column = report.map((report) => report[i][0]).join('');
console.log('in fmcb: column', column);
if ((column.match(/0/g) || []).length > (column.match(/1/g) || []).length) {
most_common_bits.push('0');
}
else {
most_common_bits.push('1'); // ties go to 1, too
}
}

return most_common_bits
}
Insert cell
find_most_common_bits(test_diagnostic_report)
Insert cell
function find_least_common_bits(report) {
let least_common_bits = [];

for (let i = 0; i < report[0].length; ++i) {
let column = report.map((report) => report[i][0]).join('');
if ((column.match(/0/g)).length > (column.match(/1/g) || []).length) {
least_common_bits.push('1');
}
else {
least_common_bits.push('0'); // ties go to 0, too
}
}

return least_common_bits
}
Insert cell
function find_rating(report, col, type) {

// Stoping condition
if (report.length == 1) {
return report;
} else {
console.log('foo')
let most_common_bits = find_most_common_bits(report)
console.log('bar')
let least_common_bits = find_least_common_bits(report)
console.log(most_common_bits)
let filtered_report = []
console.log(typeof report)
console.log('report length: ', report.length)
for (let i = 0; i < report.length; ++i) {
if (type == 'oxygen generator') {
if (most_common_bits[col] == report[i][col]) {
filtered_report.push(report[i]);
}
}
else if (type == 'CO2 scrubber') {
if (least_common_bits[col] == report[i][col]) {
filtered_report.push(report[i]);
}
}
}

console.log('filtered_report: ', filtered_report)
console.log('filtered report length', filtered_report.length)
console.log(col+1)
return find_rating(filtered_report, col+1, type)
}
}
Insert cell
Insert cell
test_oxygen_generator_rating = find_rating(test_diagnostic_report, 0, 'oxygen generator')
Insert cell
test_CO2_scrubber_rating = find_rating(test_diagnostic_report, 0, 'CO2 scrubber')
Insert cell
parseInt(test_oxygen_generator_rating, 2) * parseInt(test_CO2_scrubber_rating, 2) // Should return 230
Insert cell
Insert cell
oxygen_generator_rating = find_rating(diagnostic_report, 0, 'oxygen generator')
Insert cell
CO2_scrubber_rating = find_rating(diagnostic_report, 0, 'CO2 scrubber')
Insert cell
parseInt(oxygen_generator_rating, 2) * parseInt(CO2_scrubber_rating, 2)
Insert cell
Insert cell
test_diagnostic_report = Array('00100', '11110', '10110', '10111', '10101', '01111', '00111', '11100', '10000', '11001', '00010', '01010')
Insert cell
file = FileAttachment("diagnostic_report.txt")
Insert cell
input_data = file.text()
Insert cell
diagnostic_report = input_data.split('\r\n')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more