-
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.
Merge pull request #14 from hildjj/day4
Day 4
- Loading branch information
Showing
5 changed files
with
137 additions
and
1 deletion.
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 @@ | ||
lines = (@[^\n]+ "\n")* |
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,50 @@ | ||
import { type MainArgs, parseFile } from './lib/utils.ts'; | ||
import { AllBoxDirs, BoxDir, Rect } from './lib/rect.ts'; | ||
|
||
type Parsed = string[][]; | ||
|
||
function part1(inp: Parsed): number { | ||
let tot = 0; | ||
const r = new Rect(inp); | ||
const Xs = r.filter((v) => v === 'X'); | ||
|
||
for (const x of Xs) { | ||
for (const dir of AllBoxDirs) { | ||
if (r.ray(x, dir, 4).join('') === 'XMAS') { | ||
tot++; | ||
} | ||
} | ||
} | ||
|
||
return tot; | ||
} | ||
|
||
function part2(inp: Parsed): number { | ||
let tot = 0; | ||
const r = new Rect(inp); | ||
const As = r.filter((v) => v === 'A'); | ||
|
||
const corners = [BoxDir.NW, BoxDir.SE, BoxDir.NE, BoxDir.SW]; | ||
for (const a of As) { | ||
const letters = corners.map((d) => { | ||
const p = a.inBoxDir(d, r); | ||
return p ? r.get(p) : undefined; | ||
}); | ||
if (letters.some((p) => (p !== 'M') && (p !== 'S'))) { | ||
continue; | ||
} | ||
if (letters[0] === letters[1]) { | ||
continue; | ||
} | ||
if (letters[2] === letters[3]) { | ||
continue; | ||
} | ||
tot++; | ||
} | ||
return tot; | ||
} | ||
|
||
export default async function main(args: MainArgs): Promise<[number, number]> { | ||
const inp = await parseFile<Parsed>(args); | ||
return [part1(inp), part2(inp)]; | ||
} |
Submodule inputs
updated
from b7be02 to f9def5
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export default [2569, 1998]; |