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

Commit

Permalink
feat: day 7 (#9)
Browse files Browse the repository at this point in the history
* day 7 init

* day 7 done
  • Loading branch information
rnbguy authored Dec 7, 2024
1 parent 714f54c commit 9284994
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions day7/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@scope/day7",
"version": "0.1.0",
"exports": {
".": "./mod.ts"
}
}
9 changes: 9 additions & 0 deletions day7/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
65 changes: 65 additions & 0 deletions day7/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export function parse(data_r: string): [number, number[]][] {
const data = data_r.trim();
return data.split("\n").map((line) => {
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.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.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) {
const data_path = new URL("input.txt", import.meta.url).pathname;
const data = parse(await Deno.readTextFile(data_path));
console.log(solve1(data));
console.log(solve2(data));
}
9 changes: 9 additions & 0 deletions day7/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { assertEquals } from "@std/assert";
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), 3749);
assertEquals(solve2(example_data), 11387);
});

0 comments on commit 9284994

Please sign in to comment.