feat(day_3): part B done in a realistic way
This commit is contained in:
@@ -6,4 +6,4 @@
|
|||||||
| :-: | :-------------- | :----: | :----: |
|
| :-: | :-------------- | :----: | :----: |
|
||||||
| 1 | Secret Entrance | ⭐ | ⭐ |
|
| 1 | Secret Entrance | ⭐ | ⭐ |
|
||||||
| 2 | Gift Shop | ⭐ | ⭐ |
|
| 2 | Gift Shop | ⭐ | ⭐ |
|
||||||
| 3 | Lobby | ⭐ | |
|
| 3 | Lobby | ⭐ | ⭐ |
|
||||||
|
|||||||
@@ -48,33 +48,56 @@ export function find_largest_joltage_sum_unlimited(
|
|||||||
let total_joltage = 0;
|
let total_joltage = 0;
|
||||||
|
|
||||||
battey_bank_list.forEach((bank) => {
|
battey_bank_list.forEach((bank) => {
|
||||||
let max_binomial_bank = 0;
|
if (bank.length === 0) {
|
||||||
|
return;
|
||||||
for (let nc = 0; nc <= Math.pow(2, bank.length); nc += 1) {
|
|
||||||
// NOTE: this only works with possitibe numbers, for negative: (nc >>> 0).toString(2)
|
|
||||||
const binary_nc = nc.toString(2).padStart(bank.length, "0").split("");
|
|
||||||
// PERF: REAL dirt over here
|
|
||||||
if (
|
|
||||||
binary_nc.reduce((sum, n) => sum + (n === "1" ? 1 : 0), 0) !== LIMIT
|
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const summatory = parseInt(bank.reduce((sum, battery, i) => {
|
|
||||||
const result = `${sum}${binary_nc[i] === "1" ? battery : ""}`;
|
|
||||||
return result;
|
|
||||||
}, ""));
|
|
||||||
|
|
||||||
if (summatory > max_binomial_bank) {
|
|
||||||
max_binomial_bank = summatory;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
total_joltage += max_binomial_bank;
|
let bank_joltage_str = "";
|
||||||
|
for (let limit = LIMIT - 1; limit >= 0; limit--) {
|
||||||
|
// [>] Find biggest number
|
||||||
|
let biggest_battery_index = 0;
|
||||||
|
let biggest_battery = bank[biggest_battery_index];
|
||||||
|
for (let b_index = 0; b_index < bank.length - limit; b_index++) {
|
||||||
|
if (bank[b_index] > biggest_battery) {
|
||||||
|
biggest_battery_index = b_index;
|
||||||
|
biggest_battery = bank[b_index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// [>] Slice array
|
||||||
|
bank = bank.splice(biggest_battery_index + 1);
|
||||||
|
bank_joltage_str += biggest_battery.toString();
|
||||||
|
}
|
||||||
|
total_joltage += parseInt(bank_joltage_str);
|
||||||
});
|
});
|
||||||
|
|
||||||
return total_joltage;
|
return total_joltage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NOTE: this was fun but REALLY inefficient
|
||||||
|
function find_largest_bank_joltage(bank: BatteryBank, limit: number): number {
|
||||||
|
let max_unlimited_bank = 0;
|
||||||
|
for (let nc = 0; nc <= BigInt(Math.pow(2, bank.length)); nc += 1) {
|
||||||
|
// NOTE: this only works with possitibe numbers, for negative: (nc >>> 0).toString(2)
|
||||||
|
const binary_nc = nc.toString(2).padStart(bank.length, "0").split("");
|
||||||
|
// PERF: REAL dirt over here
|
||||||
|
if (
|
||||||
|
binary_nc.reduce((sum, n) => sum + (n === "1" ? 1 : 0), 0) !== limit
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const summatory = parseInt(bank.reduce((sum, battery, i) => {
|
||||||
|
const result = `${sum}${binary_nc[i] === "1" ? battery : ""}`;
|
||||||
|
return result;
|
||||||
|
}, ""));
|
||||||
|
|
||||||
|
if (summatory > max_unlimited_bank) {
|
||||||
|
max_unlimited_bank = summatory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max_unlimited_bank;
|
||||||
|
}
|
||||||
|
|
||||||
export async function read_battery_joltage_list(
|
export async function read_battery_joltage_list(
|
||||||
path: string,
|
path: string,
|
||||||
): Promise<BatteryBank[]> {
|
): Promise<BatteryBank[]> {
|
||||||
|
|||||||
Reference in New Issue
Block a user