Skip to content

Commit

Permalink
2024 day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 19, 2024
1 parent a006a5d commit 1b81ef6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
29 changes: 29 additions & 0 deletions 2024/19/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();
54 changes: 54 additions & 0 deletions 2024/19/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export async function taskOne(input: string[]): Promise<void> {
const allowed = input[0].split(', ')
const allowedMemo = new Set<string>()
const disallowedMemo = new Set<string>()
let s = 0
for (let i = 2; i < input.length; i++) {
if (check(input[i])) s++
}
console.log(s)


function check(towel: string) {
if (towel == '') return true
if (allowedMemo.has(towel)) return true
if (disallowedMemo.has(towel)) return false

for (const t of allowed) {
if (towel.startsWith(t)) {
if (check(towel.substring(t.length))) {
allowedMemo.add(towel)
return true
}
}
}
disallowedMemo.add(towel)
return false
}
}

export async function taskTwo(input: string[]): Promise<void> {
const allowed = input[0].split(', ')
const memo = new Map<string, number>()
let s = 0
for (let i = 2; i < input.length; i++) {
s += check(input[i])
}
console.log(s)


function check(towel: string): number {
if (towel == '') return 1
if (memo.has(towel)) return memo.get(towel)!

let s = 0
for (const t of allowed) {
if (towel.startsWith(t)) {
let c = check(towel.substring(t.length))
s += c
}
}
memo.set(towel, s)
return 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⭐-36-yellow)
![](https://img.shields.io/badge/stars%20⭐-38-yellow)

|Day|Language|Leaderboard (Part 1 / Part 2)|
|--|--|--|
Expand All @@ -20,4 +20,5 @@
|15|Typescript|882 / 1670|
|16|Typescript|347 / 1292|
|17|Typescript|486 / 8649|
|18|Typescript|420 / 1186|
|18|Typescript|420 / 1186|
|19|Typescript|585 / 491|
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⭐-481yellow)
![](https://img.shields.io/badge/Total%20stars%20⭐-485yellow)

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

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


<h3><a href="2023/README.md">2023</a></h3>
Expand Down
3 changes: 3 additions & 0 deletions setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ interface State {
y: number
}


const dir of [[1,0],[-1,0],[0,1],[0,-1]]
const dir of [[1,0],[-1,0],[0,1],[0,-1],[1,1],[1,-1],[-1,1],[-1,-1]]
*/

0 comments on commit 1b81ef6

Please sign in to comment.