Skip to content

Commit

Permalink
2024 day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 15, 2024
1 parent a6f9319 commit 4d5753c
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 4 deletions.
29 changes: 29 additions & 0 deletions 2024/15/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();
176 changes: 176 additions & 0 deletions 2024/15/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
export async function taskOne(input: string[]): Promise<void> {
let i = 0
const grid: string[][] = []
while (input[i] != '') {
grid.push(input[i].split(''))
i++
}
i++
let instructions = ''
while(i < input.length) {
instructions += input[i]
i++
}
let pos = [-1,-1]
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
if (grid[y][x] == '@') pos = [y,x]
}
}

for (const i of instructions) {
let dir = [0,0]
if (i == '>') dir = [0,1]
if (i == '<') dir = [0,-1]
if (i == 'v') dir = [1,0]
if (i == '^') dir = [-1,0]

let c = [pos[0], pos[1]]
let foundFree = false
while(grid[c[0]][c[1]] != '#') {
if (grid[c[0]][c[1]] == '.') {
foundFree = true
break
}
c[0] += dir[0]
c[1] += dir[1]
}
if (!foundFree) continue


while(c[0] != pos[0] || c[1] != pos[1]) {
grid[c[0]][c[1]] = grid[c[0]-dir[0]][c[1]-dir[1]]
grid[c[0]-dir[0]][c[1]-dir[1]] = '.'
c[0] -= dir[0]
c[1] -= dir[1]
}
pos[0] += dir[0]
pos[1] += dir[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] == 'O') {
s += y*100+x
}
}
}
console.log(s)
}

export async function taskTwo(input: string[]): Promise<void> {
let i = 0
let oggrid: string[][] = []
while (input[i] != '') {
const row: string[] = []
for (const c of input[i]) {
if (c == '#') {
row.push('#')
row.push('#')
} else
if (c == '.') {
row.push('.')
row.push('.')
} else
if (c == '@') {
row.push('@')
row.push('.')
} else
if (c == 'O') {
row.push('[')
row.push(']')
}
}
oggrid.push(row)
i++
}
i++
let instructions = ''
while(i < input.length) {
instructions += input[i]
i++
}
let pos = [-1,-1]
for (let y = 0; y < oggrid.length; y++) {
for (let x = 0; x < oggrid[y].length; x++) {
if (oggrid[y][x] == '@') pos = [y,x]
}
}

for (const i of instructions) {
const grid = JSON.parse(JSON.stringify(oggrid)) as string[][]
let dir = [0,0]
if (i == '>') dir = [0,1]
if (i == '<') dir = [0,-1]
if (i == 'v') dir = [1,0]
if (i == '^') dir = [-1,0]

let c = [pos[0], pos[1]]
let foundEmpty = false
while(grid[c[0]][c[1]] != '#') {
if (grid[c[0]][c[1]] == '.') {
foundEmpty = true
break
}

c[0] += dir[0]
c[1] += dir[1]
}

if (!foundEmpty) continue

if (dir[0] == 0) {
while(c[0] != pos[0] || c[1] != pos[1]) {
grid[c[0]][c[1]] = grid[c[0]-dir[0]][c[1]-dir[1]]
grid[c[0]-dir[0]][c[1]-dir[1]] = '.'
c[0] -= dir[0]
c[1] -= dir[1]
}
} else {
if (!push(pos[1],pos[0],dir[1],dir[0])) continue
}

pos[0] += dir[0]
pos[1] += dir[1]
oggrid = grid

function push(x: number, y: number, dX: number, dY: number): boolean {
let orth = new ComplexNumber(1,0)
if (grid[y+dY][x+dX] == '#') return false
if (grid[y+dY][x+dX] == '.') {
grid[y+dY][x+dX] = grid[y][x]
grid[y][x] = '.'
return true
}
if (grid[y+dY][x+dX] == '[') {
if (!push(x+orth.rel+dX,y+orth.img+dY,dX,dY)) return false
if (!push(x+dX,y+dY,dX,dY)) return false
grid[y+dY][x+dX] = grid[y][x]
grid[y][x] = '.'
return true
}
if (grid[y+dY][x+dX] == ']') {
if (!push(x-orth.rel+dX,y-orth.img+dY,dX,dY)) return false
if (!push(x+dX,y+dY,dX,dY)) return false
grid[y+dY][x+dX] = grid[y][x]
grid[y][x] = '.'
return true
}


throw "how?"
}
}

let s = 0
for (let y = 0; y < oggrid.length; y++) {
for (let x = 0; x < oggrid[y].length; x++) {
if (oggrid[y][x] == '[') {
s += y*100+x
}
}
}
console.log(s)

}
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⭐-28-yellow)
![](https://img.shields.io/badge/stars%20⭐-30-yellow)

|Day|Language|Leaderboard (Part 1 / Part 2)|
|--|--|--|
Expand All @@ -16,4 +16,5 @@
|11|Typescript|414 / 803|
|12|Typescript|1588 / 4633|
|13|Typescript|675 / 1094|
|14|Typescript|895 / 296|
|14|Typescript|895 / 296|
|15|Typescript|882 / 1670|
5 changes: 3 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⭐-475yellow)
![](https://img.shields.io/badge/Total%20stars%20⭐-477yellow)

## What language do I use?
Whatever I feel like today. But it will most likely either be Java, Python, C++, Typescript or Haskell
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⭐-28-yellow)
![](https://img.shields.io/badge/stars%20⭐-30-yellow)

|Day|Language|
|--|--|
Expand All @@ -27,6 +27,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,
|12|TypeScript|
|13|TypeScript|
|14|TypeScript|
|15|TypeScript|


<h3><a href="2023/README.md">2023</a></h3>
Expand Down

0 comments on commit 4d5753c

Please sign in to comment.