Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
day 7 done
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Dec 7, 2024
1 parent 9cab978 commit 71bde8b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
51 changes: 47 additions & 4 deletions day7/mod.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
export function parse(data_r: string): [number, number[]][] {
const data = data_r.trim();
return data.split("\n").map((line) => {
const ecase = line.split(":");
return [Number(ecase[0]), ecase[1].split(",").map(Number)];
const [a, b] = line.split(":");
return [Number(a.trim()), b.trim().split(" ").map(Number)];
});
}

function solve1_util(value: number, remaining: number[]): boolean {
if (remaining.length === 1) {
return remaining[0] === value;
}
const [head, ...tail] = remaining;
// input is reversed
// tail + head = value
// tail * head = value
return (head <= value && solve1_util(value - head, tail)) ||
(value % head == 0 && solve1_util(value / head, tail));
}

function solve2_util(value: number, remaining: number[]): boolean {
if (remaining.length === 1) {
return remaining[0] === value;
}
const [head, ...tail] = remaining;
// input is reversed
// tail + head = value
// tail * head = value
// tail | head = value
const value_str = value.toString();
const head_str = head.toString();
if (value_str.length > head_str.length && value_str.endsWith(head_str)) {
const new_value_str = value_str.slice(
0,
value_str.length - head_str.length,
);
const new_value = Number(new_value_str);
if (solve2_util(new_value, tail)) {
return true;
}
}
return (head <= value && solve2_util(value - head, tail)) ||
(value % head == 0 && solve2_util(value / head, tail));
}

export function solve1(data: [number, number[]][]): number {
return data.length;
return data.filter((ecase) => {
// reverse will reverse the array in place too
return solve1_util(ecase[0], ecase[1].slice().reverse());
}).reduce((acc, [value, _]) => acc + value, 0);
}

export function solve2(data: [number, number[]][]): number {
return data.length;
return data.filter((ecase) => {
// reverse will reverse the array in place too
return solve2_util(ecase[0], ecase[1].slice().reverse());
}).reduce((acc, [value, _]) => acc + value, 0);
}

if (import.meta.main) {
Expand Down
4 changes: 2 additions & 2 deletions day7/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { parse, solve1, solve2 } from "./mod.ts";
Deno.test(async function testExample() {
const example_data_path = new URL("example.txt", import.meta.url).pathname;
const example_data = parse(await Deno.readTextFile(example_data_path));
assertEquals(solve1(example_data), 9);
assertEquals(solve2(example_data), 9);
assertEquals(solve1(example_data), 3749);
assertEquals(solve2(example_data), 11387);
});

0 comments on commit 71bde8b

Please sign in to comment.