Add solution day 23

This commit is contained in:
monoid 2024-12-23 15:39:05 +09:00
parent f7a3f19b9b
commit 096208c2e5
4 changed files with 3509 additions and 0 deletions

32
day_23/example.txt Normal file
View File

@ -0,0 +1,32 @@
kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn

3380
day_23/input.txt Normal file

File diff suppressed because it is too large Load Diff

44
day_23/solve_1.ts Normal file
View File

@ -0,0 +1,44 @@
export type Interconnected = {
[key: string]: string[]
}
export async function readData(path: string): Promise<Interconnected> {
const data = await Deno.readTextFile(path);
return data.trim().split('\n').reduce((acc, line) => {
const [key, value] = line.split('-');
const original = acc[key] ?? [];
acc[key] = [...original, value];
// Add the reverse
const original2 = acc[value] ?? [];
acc[value] = [...original2, key];
return acc;
}, {} as Interconnected);
}
function findConnected3(data: Interconnected, target: string): Set<string> {
const connected = new Set<string>();
const lst = data[target].toSorted();
for (let i = 0; i < lst.length; i++) {
for (let j = i + 1; j < lst.length; j++) {
if (data[lst[i]].includes(lst[j])) {
connected.add([target, lst[i], lst[j]].toSorted().join('-'));
}
}
}
return connected;
}
if (import.meta.main) {
const data = await readData('./input.txt');
let acc = new Set<string>();
for (const key in data) {
console.log(key, data[key]);
if (!key.startsWith("t")) continue;
const connected = findConnected3(data, key);
acc = acc.union(connected);
}
console.log(acc);
}

53
day_23/solve_2.ts Normal file
View File

@ -0,0 +1,53 @@
import { readData, Interconnected } from "./solve_1.ts";
function checkConnected(data: Interconnected, curConnected: Set<string>, target: string): boolean {
// console.log(curConnected, target);
if (curConnected.has(target)) return true;
for (const key of curConnected) {
// console.log(key, data[key], target);
if (!data[key].includes(target)) return false;
}
return true;
}
function findMaxConnected(data: Interconnected, target: string) {
const lst = data[target];
let connectedSet = new Set<string>();
for (let i = 0; i < lst.length; i++) {
const connected = new Set<string>();
connected.add(target);
connected.add(lst[i]);
for (let j = i+1; j < lst.length; j++) {
if (checkConnected(data, connected, lst[j])) {
connected.add(lst[j]);
}
}
if (connected.size > connectedSet.size) {
connectedSet = connected;
}
}
return connectedSet;
}
if (import.meta.main) {
const data = await readData('./input.txt');
const acc = new Set<string>();
for (const key in data) {
// console.log(key, data[key]);
const connected = findMaxConnected(data, key);
acc.add([...connected.keys()].toSorted().join("-"));
}
console.log(acc);
const max = [...acc].map(x => {
const keys = x.split("-");
return {
length: keys.length,
keys,
}
}).sort((a, b) => b.length - a.length)[0];
console.log(max);
const password = max.keys.join(",");
console.log(password);
}