-
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
Showing
8 changed files
with
598 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,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
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,102 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const points = input.map(i => { | ||
const r = /[^<]*< *([-0-9]+), *([-0-9]+)>[^<]*< *([-0-9]+), *([-0-9]+)>/.exec(i)! | ||
return { | ||
x: Number(r[1]), | ||
y: Number(r[2]), | ||
vx: Number(r[3]), | ||
vy: Number(r[4]) | ||
} as Point | ||
}) | ||
|
||
let t = 0 | ||
while(true) { | ||
const ps = getPointsAtTime(points, t) | ||
let minY = Infinity | ||
let maxY = -Infinity | ||
|
||
for (const p of ps) { | ||
if (p.y < minY) minY = p.y | ||
if (p.y > maxY) maxY = p.y | ||
} | ||
|
||
if (maxY - minY < 10) { | ||
console.log('T: ' + t) | ||
print(ps) | ||
break | ||
} | ||
|
||
t++ | ||
} | ||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
const points = input.map(i => { | ||
const r = /[^<]*< *([-0-9]+), *([-0-9]+)>[^<]*< *([-0-9]+), *([-0-9]+)>/.exec(i)! | ||
return { | ||
x: Number(r[1]), | ||
y: Number(r[2]), | ||
vx: Number(r[3]), | ||
vy: Number(r[4]) | ||
} as Point | ||
}) | ||
|
||
let t = 0 | ||
while(true) { | ||
const ps = getPointsAtTime(points, t) | ||
let minY = Infinity | ||
let maxY = -Infinity | ||
|
||
for (const p of ps) { | ||
if (p.y < minY) minY = p.y | ||
if (p.y > maxY) maxY = p.y | ||
} | ||
|
||
if (maxY - minY < 10) { | ||
console.log(t) | ||
break | ||
} | ||
|
||
t++ | ||
} | ||
} | ||
|
||
interface Position { | ||
x: number, y: number | ||
} | ||
|
||
interface Point extends Position { | ||
vx:number,vy: number | ||
} | ||
|
||
function getPointsAtTime(ps: Point[], t: number): Position[] { | ||
return ps.map(p => { | ||
return { | ||
x: p.x + t*p.vx, | ||
y: p.y + t*p.vy | ||
} | ||
}) | ||
} | ||
|
||
function print(ps: Position[]) { | ||
let minX = Infinity | ||
let maxX = -Infinity | ||
let minY = Infinity | ||
let maxY = -Infinity | ||
|
||
for (const p of ps) { | ||
if (p.x < minX) minX = p.x | ||
if (p.x > maxX) maxX = p.x | ||
if (p.y < minY) minY = p.y | ||
if (p.y > maxY) maxY = p.y | ||
} | ||
console.log(minX, maxX, minY, maxY) | ||
|
||
const g = Array.from({length:maxY-minY+1}, () => Array.from({length: maxX-minX+1}, () => false)) | ||
|
||
for (const p of ps) { | ||
g[p.y-minY][p.x-minX] = true | ||
} | ||
|
||
g.forEach(r => console.log(r.map(i => i ? '#' : ' ').join(''))) | ||
} |
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,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
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,94 @@ | ||
export async function taskOne(input: string[]): Promise<void> { | ||
const serialNumber = Number(input[0]) | ||
const L = 300 | ||
|
||
const grid = Array.from({length: L}, (_, x) => Array.from({length:L}, (_, y) => { | ||
const rackID = x + 10 + 1 | ||
let powerLevel = (rackID) * (y + 1) | ||
powerLevel += serialNumber | ||
powerLevel *= rackID | ||
return { | ||
rackID: rackID, | ||
powerLevel: (Math.floor(powerLevel / 100)% 10)-5 | ||
} as Cell | ||
})) | ||
|
||
let maxP = [0,0] | ||
let maxV = -Infinity | ||
for (let x = 0; x < L-2; x++) { | ||
for (let y = 0; y < L-2; y++) { | ||
let s = 0 | ||
for (let xd = 0; xd < 3; xd++) { | ||
for (let yd = 0; yd < 3; yd++) { | ||
s += grid[x+xd][y+yd].powerLevel | ||
} | ||
} | ||
if (s > maxV) { | ||
maxV = s | ||
maxP = [x, y] | ||
} | ||
} | ||
} | ||
console.log(`${maxP[0]+1},${maxP[1]+1}`) | ||
|
||
} | ||
|
||
export async function taskTwo(input: string[]): Promise<void> { | ||
const serialNumber = Number(input[0]) | ||
const L = 300 | ||
|
||
const grid = Array.from({length: L}, (_, x) => Array.from({length:L}, (_, y) => { | ||
const rackID = x + 10 + 1 | ||
let powerLevel = (rackID) * (y + 1) | ||
powerLevel += serialNumber | ||
powerLevel *= rackID | ||
return (Math.floor(powerLevel / 100)% 10)-5 | ||
})) | ||
|
||
const summedAreaTable = Array.from({length: L}, () => Array.from({length:L}, () => 0)) | ||
|
||
summedAreaTable[0][0] = grid[0][0] | ||
for (let x = 1; x < L; x++) { | ||
summedAreaTable[x][0] = summedAreaTable[x-1][0] + grid[x][0] | ||
summedAreaTable[0][x] = summedAreaTable[0][x-1] + grid[0][x] | ||
} | ||
|
||
for (let x = 1; x < L; x++) { | ||
for (let y = 1; y < L; y++) { | ||
summedAreaTable[x][y] = summedAreaTable[x-1][y] + summedAreaTable[x][y-1] - summedAreaTable[x-1][y-1] + grid[x][y] | ||
} | ||
} | ||
/* | ||
for (let y = 1; y < 10; y++) { | ||
let r = '' | ||
for (let x = 1; x < 10; x++) { | ||
r += summedAreaTable[x][y] + ' ' | ||
} | ||
console.log(r) | ||
}*/ | ||
|
||
function get(x1: number, x2: number, y1:number, y2: number) { | ||
return summedAreaTable[x2][y2] + summedAreaTable[x1][y1] - summedAreaTable[x2][y1] - summedAreaTable[x1][y2] | ||
} | ||
|
||
let maxP = [0,0, 0] | ||
let maxV = -Infinity | ||
for (let s = 1; s <= L; s++) { | ||
for (let x = 0; x < L-s; x++) { | ||
for (let y = 0; y < L-s; y++) { | ||
let su = get(x, x+s, y, y+s) | ||
if (su > maxV) { | ||
maxV = su | ||
maxP = [x, y, s] | ||
} | ||
} | ||
} | ||
} | ||
|
||
console.log(`${maxP[0]+2},${maxP[1]+2},${maxP[2]}`) | ||
} | ||
|
||
interface Cell { | ||
rackID: number, | ||
powerLevel: number | ||
} |
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,29 @@ | ||
import { readFile } from "fs/promises"; | ||
import { taskOne, taskTwo } from "./task"; | ||
|
||
async function main() { | ||
let lastArgument = process.argv.pop() as string; | ||
let taskNumber = 1; | ||
let isTest = false; | ||
|
||
if (lastArgument === "test") { | ||
isTest = true; | ||
taskNumber = parseInt(process.argv.pop() as string); | ||
} else { | ||
taskNumber = parseInt(lastArgument) | ||
} | ||
|
||
const fileToLoad = isTest ? "test.in" : "solve.in"; | ||
const fileContents = await readFile(fileToLoad, "utf-8") | ||
|
||
const lines = fileContents.split("\n"); | ||
|
||
if (taskNumber === 1) { | ||
await taskOne(lines); | ||
} | ||
if (taskNumber === 2) { | ||
await taskTwo(lines); | ||
} | ||
} | ||
|
||
void main(); |
Oops, something went wrong.