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.
* add code coverage * rm redundant task * day 4 * refactor * write permission for pr comment * corner case
- Loading branch information
Showing
11 changed files
with
154 additions
and
17 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
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { process_data, solve1, solve2 } from "./mod.ts"; | ||
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 = await Deno.readTextFile(example_data_path); | ||
assertEquals(solve1(process_data(example_data)), 11); | ||
assertEquals(solve2(process_data(example_data)), 31); | ||
assertEquals(solve1(parse(example_data)), 11); | ||
assertEquals(solve2(parse(example_data)), 31); | ||
}); |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
1 3 2 4 5 | ||
8 6 4 4 1 | ||
1 3 6 7 9 | ||
1 2 2 9 4 |
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
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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { process_data, solve1, solve2 } from "./mod.ts"; | ||
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 = await Deno.readTextFile(example_data_path); | ||
assertEquals(solve1(process_data(example_data)), 2); | ||
assertEquals(solve2(process_data(example_data)), 4); | ||
assertEquals(solve1(parse(example_data)), 2); | ||
assertEquals(solve2(parse(example_data)), 4); | ||
}); |
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/day4", | ||
"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,10 @@ | ||
MMMSXXMASM | ||
MSAMXMSMSA | ||
AMXSXMAAMM | ||
MSAMASMSMX | ||
XMASAMXAMM | ||
XXAMMXXAMA | ||
SMSMSASXSS | ||
SAXAMASAAA | ||
MAMMMXMMMM | ||
MXMXAXMASX |
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,101 @@ | ||
const XMAS: string = "XMAS"; | ||
const MAS: string = "MAS"; | ||
|
||
export function parse(data: string): string[] { | ||
const parsed = data.split("\n"); | ||
return parsed | ||
// Remove the last element, which is an empty string | ||
.slice(0, parsed.length - 1); | ||
} | ||
|
||
class Grid { | ||
grid: string[]; | ||
constructor(grid: string[]) { | ||
this.grid = grid; | ||
} | ||
|
||
x_len(): number { | ||
return this.grid[0].length; | ||
} | ||
|
||
y_len(): number { | ||
return this.grid.length; | ||
} | ||
|
||
get(x: number, y: number): string { | ||
return this.grid[y]?.[x] ?? ""; | ||
} | ||
|
||
find_xmas_at(x: number, y: number): number { | ||
let count = 0; | ||
|
||
for (const dx of [-1, 0, 1]) { | ||
for (const dy of [-1, 0, 1]) { | ||
if (dx === 0 && dy === 0) continue; | ||
|
||
if ( | ||
Array.from({ length: XMAS.length }).every((_, i) => { | ||
const xx = x + dx * i; | ||
const yy = y + dy * i; | ||
|
||
return this.get(xx, yy) == XMAS[i]; | ||
}) | ||
) { | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
is_x_mas_at(x: number, y: number): boolean { | ||
return [-1, 1].some((d) => { | ||
return this.get(x - d, y - d) + this.get(x, y) + | ||
this.get(x + d, y + d) == MAS; | ||
}) && | ||
[-1, 1].some((d) => { | ||
return this.get(x - d, y + d) + this.get(x, y) + | ||
this.get(x + d, y - d) == MAS; | ||
}); | ||
} | ||
|
||
find_all_xmas(): number { | ||
let count = 0; | ||
for (let x = 0; x < this.x_len(); x++) { | ||
for (let y = 0; y < this.y_len(); y++) { | ||
count += this.find_xmas_at(x, y); | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
find_all_x_mas(): number { | ||
let count = 0; | ||
for (let y = 0; y < this.y_len(); y++) { | ||
for (let x = 0; x < this.x_len(); x++) { | ||
if (this.is_x_mas_at(x, y)) { | ||
count++; | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
|
||
export function solve1(data: string[]): number { | ||
const grid = new Grid(data); | ||
return grid.find_all_xmas(); | ||
} | ||
|
||
export function solve2(data: string[]): number { | ||
const grid = new Grid(data); | ||
return grid.find_all_x_mas(); | ||
} | ||
|
||
if (import.meta.main) { | ||
const data_path = new URL("input.txt", import.meta.url).pathname; | ||
const data = await Deno.readTextFile(data_path); | ||
console.log(solve1(parse(data))); | ||
console.log(solve2(parse(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 = await Deno.readTextFile(example_data_path); | ||
assertEquals(solve1(parse(example_data)), 18); | ||
assertEquals(solve2(parse(example_data)), 9); | ||
}); |
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