-
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.
- Loading branch information
1 parent
c12ab50
commit 807dab3
Showing
4 changed files
with
92 additions
and
2 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,89 @@ | ||
import AoCPuzzle from '../../puzzle'; | ||
import { Point } from '../../types'; | ||
|
||
export default class Puzzle extends AoCPuzzle { | ||
private xmasWords = 0; | ||
|
||
private highlightCell(p: Point): void { | ||
this.grid[p.y][p.x] = `\x1b[42m${this.grid[p.y][p.x]}\x1b[0m`; | ||
} | ||
|
||
private findXmas(p: Point): void { | ||
for (let y = -1; y <= 1; y += 1) { | ||
for (let x = -1; x <= 1; x += 1) { | ||
if (x === 0 && y === 0) { | ||
continue; | ||
} | ||
if ( | ||
(p.y + (3 * y)) >= 0 && (p.y + (3 * y)) < this.grid.length && | ||
(p.x + (3 * x)) >= 0 && (p.x + (3 * x)) < this.grid[0].length && | ||
this.grid[p.y + (1 * y)][p.x + (1 * x)].includes("M") && | ||
this.grid[p.y + (2 * y)][p.x + (2 * x)].includes("A") && | ||
this.grid[p.y + (3 * y)][p.x + (3 * x)].includes("S") | ||
) { | ||
this.xmasWords += 1; | ||
this.grid[p.y][p.x] = "\x1b[42mX\x1b[0m" | ||
this.grid[p.y + (1 * y)][p.x + (1 * x)] = "\x1b[42mM\x1b[0m" | ||
this.grid[p.y + (2 * y)][p.x + (2 * x)] = "\x1b[42mA\x1b[0m" | ||
this.grid[p.y + (3 * y)][p.x + (3 * x)] = "\x1b[42mS\x1b[0m" | ||
} | ||
} | ||
} | ||
} | ||
|
||
public async part1(): Promise<string | number> { | ||
for (let y = 0; y < this.grid.length; y += 1) { | ||
for (let x = 0; x < this.grid[y].length; x += 1) { | ||
if (this.grid[y][x].includes("X")) { | ||
this.findXmas({ x, y }); | ||
} | ||
} | ||
} | ||
|
||
if (this.isExample) { | ||
this.printGrid(); | ||
} | ||
return this.xmasWords; | ||
} | ||
|
||
private masXShapedWords = 0; | ||
|
||
private findMasXShapedWords(p: Point): void { | ||
if (p.y >= 1 && p.y < this.grid.length - 1 && | ||
p.x >= 1 && p.x < this.grid[0].length - 1) { | ||
const tl = this.grid[p.y - 1][p.x - 1] | ||
const tr = this.grid[p.y - 1][p.x + 1] | ||
const bl = this.grid[p.y + 1][p.x - 1] | ||
const br = this.grid[p.y + 1][p.x + 1] | ||
|
||
if ( | ||
((tl.includes('M') && br.includes('S')) || (tl.includes('S') && br.includes('M'))) && | ||
((tr.includes('M') && bl.includes('S')) || (tr.includes('S') && bl.includes('M'))) | ||
) { | ||
this.masXShapedWords += 1; | ||
this.highlightCell({ x: p.x, y: p.y }); | ||
this.highlightCell({ x: p.x - 1, y: p.y - 1 }); | ||
this.highlightCell({ x: p.x + 1, y: p.y - 1 }); | ||
this.highlightCell({ x: p.x - 1, y: p.y + 1 }); | ||
this.highlightCell({ x: p.x + 1, y: p.y + 1 }); | ||
} | ||
} | ||
} | ||
|
||
public async part2(): Promise<string | number> { | ||
this.setInput(this.input); | ||
|
||
for (let y = 0; y < this.grid.length; y += 1) { | ||
for (let x = 0; x < this.grid[y].length; x += 1) { | ||
if (this.grid[y][x].includes("A")) { | ||
this.findMasXShapedWords({ x, y }); | ||
} | ||
} | ||
} | ||
|
||
if (this.isExample) { | ||
this.printGrid(); | ||
} | ||
return this.masXShapedWords; | ||
} | ||
} |
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 +1 @@ | ||
{"1":{"part1ExampleResult":11,"part1ExampleTime":0.06974999999999909,"part1Result":2367773,"part1Time":0.7124579999999696,"part2ExampleResult":31,"part2ExampleTime":0.03674999999998363,"part2Result":21271939,"part2Time":0.12887499999999363},"2":{"part1ExampleResult":2,"part1ExampleTime":0.05358300000000327,"part1Result":585,"part1Time":0.6856249999999591,"part2ExampleResult":4,"part2ExampleTime":0.037833000000034644,"part2Result":626,"part2Time":0.7567500000000109},"3":{"part1ExampleResult":161,"part1ExampleTime":0.10170800000003055,"part1Result":162813399,"part1Time":0.30133299999999963,"part2ExampleResult":48,"part2ExampleTime":0.08725000000004002,"part2Result":53783319,"part2Time":0.15795799999995097}} | ||
{"1":{"part1ExampleResult":11,"part1ExampleTime":0.06974999999999909,"part1Result":2367773,"part1Time":0.7124579999999696,"part2ExampleResult":31,"part2ExampleTime":0.03674999999998363,"part2Result":21271939,"part2Time":0.12887499999999363},"2":{"part1ExampleResult":2,"part1ExampleTime":0.05358300000000327,"part1Result":585,"part1Time":0.6856249999999591,"part2ExampleResult":4,"part2ExampleTime":0.037833000000034644,"part2Result":626,"part2Time":0.7567500000000109},"3":{"part1ExampleResult":161,"part1ExampleTime":0.10170800000003055,"part1Result":162813399,"part1Time":0.30133299999999963,"part2ExampleResult":48,"part2ExampleTime":0.08725000000004002,"part2Result":53783319,"part2Time":0.15795799999995097},"4":{"part1ExampleResult":18,"part1ExampleTime":0.18233299999997143,"part1Result":2569,"part1Time":2.5545000000000186,"part2ExampleResult":9,"part2ExampleTime":0.16058399999997164,"part2Result":1998,"part2Time":3.2487499999999727}} |
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