From 3e750037c361428db25fbfa4e6d1f665ef288a03 Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Mon, 25 Dec 2023 14:09:00 +0100 Subject: [PATCH] some 2021 --- 2021/19/runner.ts | 29 +++++++ 2021/19/task.ts | 188 ++++++++++++++++++++++++++++++++++++++++++++++ 2021/20/runner.ts | 29 +++++++ 2021/20/task.ts | 22 ++++++ 2021/21/runner.ts | 29 +++++++ 2021/21/task.ts | 64 ++++++++++++++++ 2021/README.md | 2 +- 2023/25/task.ts | 2 - README.md | 2 +- 9 files changed, 363 insertions(+), 4 deletions(-) create mode 100644 2021/19/runner.ts create mode 100644 2021/19/task.ts create mode 100644 2021/20/runner.ts create mode 100644 2021/20/task.ts create mode 100644 2021/21/runner.ts create mode 100644 2021/21/task.ts diff --git a/2021/19/runner.ts b/2021/19/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2021/19/runner.ts @@ -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(); \ No newline at end of file diff --git a/2021/19/task.ts b/2021/19/task.ts new file mode 100644 index 0000000..d50c60c --- /dev/null +++ b/2021/19/task.ts @@ -0,0 +1,188 @@ +import {parseNumberList} from '../../base' + +export async function taskOne(input: string[]): Promise { + let i = 1 + const s: Scanner[] = [] + while(i < input.length) { + const scanner: Scanner = {b:[],mid:[],max:[],min:[]} + while(i < input.length && input[i] != "") { + const coord = (parseNumberList(input[i]) as Coord) + scanner.b.push(coord) + i++ + } + s.push(scanner) + i+=2 + } + s.forEach(i => { + for (let j = 0; j < i.b.length; j++) { + for (let k = 0; k < i.b.length;k++) { + const rel = i.b[j].map((a,ind) => Math.abs(a-i.b[k][ind])) + const ma = Math.max(...rel) + const mi = Math.min(...rel) + i.max.push(ma) + i.min.push(mi) + i.mid.push(rel.filter(i=>i!=ma && i!=mi)[0]) + } + } + }) + let pot = findMatches(s) + const scanner: Record = {} + scanner[0]= { + sc: [0,0,0], + b: s[0].b, + ...buildRels(s[0].b) + } + i = -1 + while(pot.length > 0) { + i++ + if (i >= pot.length) i = 0 + const p = pot[i] + if (scanner[p[0]] != undefined && scanner[p[1]] != undefined) { + pot = pot.slice(0,i).concat(pot.slice(i+1)) + i-- + continue + } + if (scanner[p[0]] == undefined && scanner[p[1]] == undefined) continue + + let known = -1 + let unknown = -1 + if (scanner[p[0]] != undefined) { + known = p[0] + unknown = p[1] + } else { + known = p[1] + unknown = p[0] + } + + for (let or = 0; or < 48; or++) { + const perm = getPermutation(s[unknown].b, or) + const rels = buildRels(perm) + if (isMatch(scanner[known], rels)) { + console.log("match") + let knownPoints: Coord[] = [] + for (let j = 0; j < scanner[known].b.length; j++) { + const knownRels = scanner[known].b.map(f => singleRel(scanner[known].b[j], f)) + for (let k = 0; k < perm.length; k++) { + const unknownRels = perm.map(f => singleRel(perm[k],f)) + let count = 0 + for (let l = 0; l < knownRels.length; l++) { + for (let m = 0; m < unknownRels.length; m++) { + if (knownRels[l].max == unknownRels[m].max && knownRels[l].min == unknownRels[m].min && knownRels[l].mid == unknownRels[m].mid) count++ + //if (knownRels[l].max == unknownRels[m].max && knownRels[l].min == unknownRels[m].min && knownRels[l].mid == unknownRels[m].mid) count++ + } + } + //console.log(count) + if (count >= 12) { + knownPoints = [scanner[known].b[j], perm[k]] + break; + } + } + if (knownPoints.length == 2) { + break; + } + } + if (knownPoints.length < 2) continue + scanner[unknown] = { + sc: scanner[known].sc.map((a, ind) => a+knownPoints[0][ind]-knownPoints[1][ind]) as Coord, + b: perm, + ...rels + } + break; + } + } + } + + const pointSet: Set = new Set() + for (let i = 0; i < s.length; i++) { + scanner[i].b.forEach(j => { + pointSet.add(j.map((a, ind)=>scanner[i].sc[ind]+a).join(",")) + }) + } + + console.log(scanner) + for (let i = 0; i < s.length; i++) { + console.log(scanner[i].sc) + } + console.log("done") + console.log(pointSet.size) +} + +export async function taskTwo(input: string[]): Promise { + console.log("Unimplemented"); +} + +type Coord = [number, number, number] +interface Scanner { + b: Coord[] + mid: number[] + max: number[] + min: number[] +} + +function findMatches(s: Scanner[]): [number,number][] { + const pot: [number,number][] = [] + + for (let i = 0; i < s.length; i++) { + for (let j = i+1; j < s.length; j++) { + if (isMatch(s[i], s[j])) pot.push([i, j]) + } + } + + return pot +} + +function isMatch(a: {mid: number[], min: number[], max: number[]}, b: {mid: number[], min: number[], max: number[]}) { + let count = 0 + for (let k = 0; k < a.mid.length; k++) { + for (let l = 0; l < b.mid.length; l++) { + if (a.mid[k] == b.mid[l] && a.min[k] == b.min[l] && a.max[k] == b.max[l]) count++ + //if (a.mid[k] == -b.mid[l] && a.min[k] == -b.min[l] && a.max[k] == -b.max[l]) count++ + } + } + return count >= 12*12 +} + +const coordSwitch = [ + [0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0] +] + +const sign = [ + [1,1,1],[-1,1,1],[1,-1,1],[1,1,-1],[-1,-1,1],[-1,1,-1],[1,-1,-1],[-1,-1,-1] +] + +function getPermutation(c: Coord[], num: number): Coord[] { + const s = sign[num % sign.length] + const m = coordSwitch[Math.floor(num/sign.length)] + return c.map(i => { + const temp = i.map((j,ind) => j*s[ind]) + return [temp[m[0]], temp[m[1]], temp[m[2]]] + }) +} + +function buildRels(b: Coord[]) { + const temp = { + max: [] as number[], + min: [] as number[], + mid: [] as number[] + } + for (let j = 0; j < b.length; j++) { + for (let k = 0; k < b.length;k++) { + const t = singleRel(b[j],b[k]) + temp.max.push(t.max) + temp.min.push(t.min) + temp.mid.push(t.mid) + } + } + return temp +} + +function singleRel(a: Coord, b: Coord) { + const rel = b.map((v,ind) => v-a[ind]) + const ma = Math.max(...rel) + const mi = Math.min(...rel) + return { + max: ma, + min: mi, + mid: rel.reduce((a,b)=>a+b,0)-ma-mi + } +} \ No newline at end of file diff --git a/2021/20/runner.ts b/2021/20/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2021/20/runner.ts @@ -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(); \ No newline at end of file diff --git a/2021/20/task.ts b/2021/20/task.ts new file mode 100644 index 0000000..d16ba0e --- /dev/null +++ b/2021/20/task.ts @@ -0,0 +1,22 @@ +export async function taskOne(input: string[]): Promise { + const map = input[0] + let image: Image = {} + + function setPixel(x:number,y:number,v:boolean, i: Image) { + if (i[x] == undefined) i[x] = {} + i[x][y] = v + } + function getPixel(x:number,y:number,i: Image) { + if (i[x] == undefined) return false + if (i[x][y] == undefined) return false + return i[x][y] + } + + for (let i = 2; i> + +export async function taskTwo(input: string[]): Promise { + console.log("Unimplemented"); +} \ No newline at end of file diff --git a/2021/21/runner.ts b/2021/21/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2021/21/runner.ts @@ -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(); \ No newline at end of file diff --git a/2021/21/task.ts b/2021/21/task.ts new file mode 100644 index 0000000..97bd82e --- /dev/null +++ b/2021/21/task.ts @@ -0,0 +1,64 @@ +export async function taskOne(input: string[]): Promise { + const pos = input.map(i=>parseInt(i.replace(" ", "").split(":")[1].trim()) - 1) + const score = input.map(i=>0) + let die = 0 + let rollCount = 0 + function roll() { + die++ + rollCount++ + const temp = die + die %= 100 + return temp + } + let player = 0 + while(!score.some(i=>i>=1000)) { + const r = roll()+ roll()+ roll() + pos[player] = (pos[player] + r) % 10 + score[player] += pos[player] + 1 + player = (player + 1) % pos.length + } + console.log(rollCount*Math.min(...score)) +} + +export async function taskTwo(input: string[]): Promise { + const pos = input.map(i=>parseInt(i.replace(" ", "").split(":")[1].trim()) - 1) + const dieOccurences = [0, 0, 0, 1, 3, 6, 7, 6, 3, 1] + interface PlayerState { + p: number + s: number + } + type Game = { p:[PlayerState, PlayerState], c:0|1, o: number} + const queue: Game[] = [ + {p: [{p:pos[0], s:0}, {p:pos[1], s:0}], c:0, o:1} + ] + while(queue.length > 0) { + const q = queue.pop() as Game + if (q.o == 0) continue + if (q.p[0].s >= 21) { + continue + } + if (q.p[1].s >= 21) { + continue + } + for(let [roll,occ] of dieOccurences.entries()) { + const newPos = (q.p[q.c].p + roll) % 10 + const newScore = q.p[q.c].s+newPos+1 + const newP: PlayerState[] = [] + if (q.o == 0) { + newP.push({p:newPos, s:newScore}) + newP.push(q.p[1]) + } else { + newP.push(q.p[0]) + newP.push({p:newPos, s:newScore}) + } + queue.push({ + p: newP as [PlayerState, PlayerState], + c: (q.c+1)%2 as 0|1, + o: q.o*occ + }) + } + + } + + console.log("done") +} diff --git a/2021/README.md b/2021/README.md index 982e774..aff98fc 100644 --- a/2021/README.md +++ b/2021/README.md @@ -1,4 +1,4 @@ # 2021 -![](https://img.shields.io/badge/stars%20⭐-36-yellow) +![](https://img.shields.io/badge/stars%20⭐-37-yellow) Puzzles not solved during event. \ No newline at end of file diff --git a/2023/25/task.ts b/2023/25/task.ts index e94d0f7..ae6df18 100644 --- a/2023/25/task.ts +++ b/2023/25/task.ts @@ -19,8 +19,6 @@ export async function taskOne(input: string[]): Promise { let potential: [string, string][][] = []; const keys = Object.keys(graph); const keys2 = keys.reverse(); - let i = 0 - let maxPos = 200 for (const k1 of keys) { for (const k2 of keys2) { if (k1 == k2) continue; diff --git a/README.md b/README.md index 845445f..a70e487 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

2021

-![](https://img.shields.io/badge/stars%20⭐-36-yellow) +![](https://img.shields.io/badge/stars%20⭐-37-yellow) ## Try it yourself https://adventofcode.com