Skip to content

Commit

Permalink
2024 day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 20, 2024
1 parent 8039240 commit 1e8a8a0
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 4 deletions.
29 changes: 29 additions & 0 deletions 2024/20/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();
164 changes: 164 additions & 0 deletions 2024/20/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { Queue } from '../../base'

export async function taskOne(input: string[]): Promise<void> {
const grid = input.map(i => i.split(''))

let start = [0,0]
let end = [0,0]
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
if (grid[y][x] == 'S') start = [x,y]
if (grid[y][x] == 'E') end = [x,y]
}
}
const queue = new Queue<State>()
const distancesFromStart = new Map<string, number>()
const distancesFromEnd = new Map<string, number>()
queue.push({x:start[0], y: start[1], d: 0})

while(!queue.isEmpty()) {
const q = queue.pop()
if (grid[q.y][q.x] == '#') continue
const k = q.x + '|' + q.y
if (distancesFromStart.has(k)) continue
distancesFromStart.set(k, q.d)

for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) {
queue.push({
x: q.x + dir[0],
y: q.y + dir[1],
d: q.d+1
})
}
}
queue.push({x:end[0], y: end[1], d: 0})
while(!queue.isEmpty()) {
const q = queue.pop()
if (grid[q.y][q.x] == '#') continue
const k = q.x + '|' + q.y
if (distancesFromEnd.has(k)) continue
distancesFromEnd.set(k, q.d)

for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) {
queue.push({
x: q.x + dir[0],
y: q.y + dir[1],
d: q.d+1
})
}
}
let totalDistance = distancesFromStart.get(end[0]+'|'+end[1])!
let s = 0
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
if (grid[y][x] == '#') continue
const distanceToGetHere = distancesFromStart.get(x+'|'+y)!
for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) {
if (grid[y+dir[1]][x+dir[0]] == '#') {
try {
if (grid[y+2*dir[1]][x+2*dir[0]] != '#') {
const stillNeeded = distancesFromEnd.get((x+2*dir[0])+'|'+(y+2*dir[1]))!
let totalTime = distanceToGetHere + 2 + stillNeeded
let savedTime = totalDistance - totalTime
if (savedTime >= 100) {
s++
}
}
} catch(e){}

}
}

}
}
console.log(s)


interface State {
x: number
y: number
d: number
}

}

export async function taskTwo(input: string[]): Promise<void> {
const grid = input.map(i => i.split(''))

let start = [0,0]
let end = [0,0]
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
if (grid[y][x] == 'S') start = [x,y]
if (grid[y][x] == 'E') end = [x,y]
}
}
const queue = new Queue<State>()
const distancesFromStart = new Map<string, number>()
const distancesFromEnd = new Map<string, number>()
queue.push({x:start[0], y: start[1], d: 0})

while(!queue.isEmpty()) {
const q = queue.pop()
if (grid[q.y][q.x] == '#') continue
const k = q.x + '|' + q.y
if (distancesFromStart.has(k)) continue
distancesFromStart.set(k, q.d)

for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) {
queue.push({
x: q.x + dir[0],
y: q.y + dir[1],
d: q.d+1
})
}
}
queue.push({x:end[0], y: end[1], d: 0})
while(!queue.isEmpty()) {
const q = queue.pop()
if (grid[q.y][q.x] == '#') continue
const k = q.x + '|' + q.y
if (distancesFromEnd.has(k)) continue
distancesFromEnd.set(k, q.d)

for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) {
queue.push({
x: q.x + dir[0],
y: q.y + dir[1],
d: q.d+1
})
}
}
let totalDistance = distancesFromStart.get(end[0]+'|'+end[1])!
let s = 0
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
if (grid[y][x] == '#') continue
const distanceToGetHere = distancesFromStart.get(x+'|'+y)!
for (let nX =-20; nX <= 20; nX++) {
for (let nY = -20; nY <= 20; nY++) {
if (Math.abs(nX) + Math.abs(nY) > 20) continue
if (x + nX < 0 || x + nX >= grid[0].length) continue
if (y + nY < 0 || y + nY >= grid.length) continue
if (grid[y+nY][x+nX] == '#') continue

const stillNeeded = distancesFromEnd.get((x+nX)+'|'+(y+nY))!
let totalTime = distanceToGetHere + Math.abs(nX) + Math.abs(nY) + stillNeeded
let savedTime = totalDistance - totalTime
if (savedTime >= 100) {
s++
}
}
}
}
}
console.log(s)


interface State {
x: number
y: number
d: number
}
}

5 changes: 3 additions & 2 deletions 2024/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 2024
![](https://img.shields.io/badge/stars%20⭐-38-yellow)
![](https://img.shields.io/badge/stars%20⭐-40-yellow)

|Day|Language|Leaderboard (Part 1 / Part 2)|
|--|--|--|
Expand All @@ -21,4 +21,5 @@
|16|Typescript|347 / 1292|
|17|Typescript|486 / 8649|
|18|Typescript|420 / 1186|
|19|Typescript|585 / 491|
|19|Typescript|585 / 491|
|20|Typescript|933 / 503|
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# My Advent of Code Solutions

![](https://img.shields.io/badge/Total%20stars%20⭐-485yellow)
![](https://img.shields.io/badge/Total%20stars%20⭐-487-yellow)

## What language do I use?
Whatever I feel like today. But it will most likely either be Java, Python, C++, TypeScript or Haskell - Lets be honest with ourselfs, it will by TypeScript
Expand All @@ -9,7 +9,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3>2024</h3>

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

|Day|Language|
|--|--|
Expand Down

0 comments on commit 1e8a8a0

Please sign in to comment.