This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* day 7 init * day 7 done
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@scope/day7", | ||
"version": "0.1.0", | ||
"exports": { | ||
".": "./mod.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |