Skip to content

Commit

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

export async function taskOne(input: string[]): Promise<void> {
const pos = new Set<string>()
const guardDir = new ComplexNumber(-1,0)
let guard = new ComplexNumber(0,0)
for (let y = 0; y < input.length; y++) {
for (let x = 0; x < input[0].length; x++) {
if (input[y][x] == '^') guard = new ComplexNumber(y,x)
}
}
while(true) {
pos.add(guard.rel + '-' + guard.img)
const newPos = guard.add(guardDir)
if (!(newPos.rel < input.length && newPos.rel >= 0 && newPos.img < input[0].length && newPos.img >= 0)) break
if (input[newPos.rel][newPos.img] == '#') guardDir.mulAdd(ComplexNumber.fromImg(-1))
else guard = newPos
}
console.log(pos.size)
}

export async function taskTwo(input: string[]): Promise<void> {
let count = 0
let guardStart = new ComplexNumber(0,0)
for (let y = 0; y < input.length; y++) {
for (let x = 0; x < input[0].length; x++) {
if (input[y][x] == '^') guardStart = new ComplexNumber(y,x)
}
}
const pos = new Set<string>()
const guardDir = new ComplexNumber(-1,0)
let guard = guardStart.add(new ComplexNumber(0,0))
while(true) {
pos.add(guard.rel + '-' + guard.img)
const newPos = guard.add(guardDir)
if (!(newPos.rel < input.length && newPos.rel >= 0 && newPos.img < input[0].length && newPos.img >= 0)) break
if (input[newPos.rel][newPos.img] == '#') guardDir.mulAdd(ComplexNumber.fromImg(-1))
else guard = newPos
}
const tries = Array.from(pos).map(i => i.split('-').map(Number))

for (const t of tries) {
const x = t[1]
const y = t[0]
if (input[y][x] == '#') continue
const pos = new Set<string>()
const guardDir = new ComplexNumber(-1,0)
let guard = guardStart.add(new ComplexNumber(0,0))
while(true) {
const k = guard.rel + '-' + guard.img + '-' + guardDir.rel + '-' + guardDir.img
if (pos.has(k)) {
count++
break
}
pos.add(k)
const newPos = guard.add(guardDir)
if (!(newPos.rel < input.length && newPos.rel >= 0 && newPos.img < input[0].length && newPos.img >= 0)) {
break
}
if (input[newPos.rel][newPos.img] == '#' || (newPos.rel == y && newPos.img == x)) guardDir.mulAdd(ComplexNumber.fromImg(-1))
else guard = newPos
}
}
console.log(count)
}
5 changes: 3 additions & 2 deletions 2024/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# 2024
![](https://img.shields.io/badge/stars%20⭐-10-yellow)
![](https://img.shields.io/badge/stars%20⭐-12-yellow)

|Day|Language|Leaderboard (Part 1 / Part 2)|
|--|--|--|
|1|TypeScript|558 / 897|
|2|TypeScript|3058 / 2617|
|3|TypeScript|6107 / 2643|
|4|TypeScript|2103 / 1654|
|5|TypeScript|4252 / 4287|
|5|TypeScript|4252 / 4287|
|6|TypeScript|527 / 726|
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⭐-421-yellow)
![](https://img.shields.io/badge/Total%20stars%20⭐-423-yellow)

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

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


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

0 comments on commit da43fb8

Please sign in to comment.