Skip to content

Commit

Permalink
2023-18
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 18, 2023
1 parent 527e1d1 commit 6727680
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
29 changes: 29 additions & 0 deletions 2023/18/runner.ts
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();
86 changes: 86 additions & 0 deletions 2023/18/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
export async function taskOne(_input: string[]): Promise<void> {
const input = _input.map(i => i.split(" "))
const wall: Map<number, Set<number>> = new Map()
let x = 0
let y = 0
wall.set(0, new Set())
wall.get(0)?.add(0)
for (const line of input) {
const d = line[0] as 'U'|'D'|'L'|'R'
for(let i = 0; i < parseInt(line[1]); i++) {
x += dirX[d]
y+= dirY[d]
if (!wall.has(x)) wall.set(x, new Set())
wall.get(x)?.add(y)
}
}
let sum = sumMap(wall) + sumMap(fill(wall, [1,1]))
console.log(sum)

}

export async function taskTwo(_input: string[]): Promise<void> {
const input = _input.map(i => i.split(" "))
let x = 0
let y = 0
let sum = 0
let perim = 0
for (let i = 0; i < input.length; i += 2) {
const xLine = input[i]
const yLine = input[i+1]
const xDir = dirNum[parseInt(xLine[2].charAt(7)) as 0|1|2|3] as 'U'|'D'|'L'|'R'
const yDir = dirNum[parseInt(yLine[2].charAt(7)) as 0|1|2|3] as 'U'|'D'|'L'|'R'
const xLen = parseInt(xLine[2].slice(2, 7), 16)
const yLen = parseInt(yLine[2].slice(2, 7), 16)

// Add trench

x = x + xLen*dirX[xDir]
y = y + yLen*dirY[yDir]
const square = (x)*(yLen*dirY[yDir])
sum += square
perim += xLen + yLen

}
console.log(sum + perim/2+1)
}

interface Wall {
rootX: number,
rootY: number,
length: number
}

function sumMap(m: Map<number, Set<number>>) {
return Array.from(m.values()).map(i => Array.from(i).length).reduce((a,b)=>a+b,0)
}

const dirNum = {
0: 'R', 1: 'D', 2: 'L', 3: 'U'
}

const dirX= {
'R': 1, 'L': -1, 'U': 0, 'D': 0
}
const dirY= {
'R': 0, 'L': 0, 'U': -1, 'D': 1
}

function fill(coords: Map<number, Set<number>>, start: [number, number]) {
const visited: Map<number, Set<number>> = new Map()
const Qu: [number, number][] = [start]

while(Qu.length > 0) {
const q = Qu.shift() as [number,number]
if (visited.has(q[0]) && visited.get(q[0])?.has(q[1])) {continue;}
if (coords.has(q[0]) && coords.get(q[0])?.has(q[1])) continue
if(!visited.has(q[0])) visited.set(q[0], new Set())
visited.get(q[0])?.add(q[1])

Qu.push([q[0],q[1]+1])
Qu.push([q[0],q[1]-1])
Qu.push([q[0]+1,q[1]])
Qu.push([q[0]-1,q[1]])
}
return visited
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3>2023</h3>

![](https://img.shields.io/badge/stars%20⭐-34-yellow)
![](https://img.shields.io/badge/stars%20⭐-36-yellow)

|Day|Language|
|--|--|
Expand All @@ -30,6 +30,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,
|15|TypeScript|
|16|TypeScript|
|17|TypeScript|
|18|TypeScript|

<h3><a href="2022/README.md">2022</a></h3>

Expand Down

0 comments on commit 6727680

Please sign in to comment.