Skip to content

Commit

Permalink
2024 day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 17, 2024
1 parent da56cdf commit 843af51
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 4 deletions.
29 changes: 29 additions & 0 deletions 2024/17/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();
95 changes: 95 additions & 0 deletions 2024/17/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {lcmArr, lcm, gcd, ComplexNumber, Stack, Queue, JsonSet, FunctionSet, MinHeap } from '../../base'

export async function taskOne(input: string[]): Promise<void> {


let A = Number(input[0].split(': ')[1])
let B = Number(input[1].split(': ')[1])
let C = Number(input[2].split(': ')[1])
console.log(input[4].split(': ')[1])
const com = input[4].split(': ')[1].split(',').map(Number)
let i = 0;
const out: number[] = []
while(i < com.length) {
switch(com[i]) {
case 0: {
A = Math.floor(A / Math.pow(2, get(com[i+1])))
i+=2
break
}
case 1: {
B = B ^ com[i+1]
i+=2
break
}
case 2: {
B = get(com[i+1]) % 8
i+=2
break
}
case 3: {
if (A == 0)
i+=2
else
i = com[i+1]
break
}
case 4: {
B = B ^ C
i+=2
break
}
case 5: {
out.push(get(com[i+1]) % 8)
i+=2
break
}
case 6: {
B = Math.floor(A / Math.pow(2, get(com[i+1])))
i+=2
break
}
case 7: {
C = Math.floor(A / Math.pow(2, get(com[i+1])))
i+=2
break
}
}
}
console.log(out.join(','))
function get(v: number): number {
if (v < 4) return v
return {4:A,5:B,6:C}[v] ?? 0
}
}

export async function taskTwo(input: string[]): Promise<void> {
//let A = Number(input[0].split(': ')[1])
const com = input[4].split(': ')[1].split(',').map(Number)
let G = com.join('')

const r = com.map(BigInt)
console.log(recursive(0n, r.length-1).toString())

function recursive(A: bigint, i: number): bigint {
if (i < 0) return A

A *= 8n
for (let d of [0n,1n,2n,3n,4n,5n,6n,7n]) {
let cA = A
cA += d
let B = cA % 8n
B = B ^ 5n
let C = cA / (2n**B)
B = B ^ C
B = B ^ 6n
let out = B % 8n
if (out == r[i]) {
let next = recursive(cA, i-1)
if (next >= 0n) return next
}
}
return -1n
}
}

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

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

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

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


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

0 comments on commit 843af51

Please sign in to comment.