Skip to content

Commit

Permalink
Merge branch 'HEAD' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
TimHi committed Dec 13, 2023
2 parents ae42939 + 1cc04e0 commit e23af4c
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Typescript/2023/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Typescript/2023/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"fast-memoize": "^2.5.2",
"functools": "^3.4.0",
"gauss-shoelace": "^0.1.3",
"hamming": "^0.0.2",
"hamming-code": "^0.0.2",
"hamming-distance-ts": "^1.0.1",
"lodash": "^4.17.21",
"lodash-decorators": "^6.0.1",
"nunjucks": "^3.2.4",
Expand Down
15 changes: 15 additions & 0 deletions Typescript/2023/src/days/__test__/day13.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import { SolvePartOne, SolvePartTwo } from "../day13/day13";
import { describe, expect, test } from "vitest";

describe("Day 13 Part 01", () => {
test("Expected result", () => {
expect(SolvePartOne()).toBe(-1);
});
});

describe("Day 13 Part 02", () => {
test("Expected result", () => {
expect(SolvePartTwo()).toBe(-1);
});
});
1 change: 1 addition & 0 deletions Typescript/2023/src/days/day12/day12.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ function getSpringGroupLengths(input: string): number[] {

return groupLengths;
}
//Memorize das mit filter?
const getCombinationsWithoutFilter = memoize((input: string, currentIndex: number = 0, currentCombination: string = ""): string[] => {
if (currentIndex === input.length) {
return [currentCombination];
Expand Down
205 changes: 205 additions & 0 deletions Typescript/2023/src/days/day13/day13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import * as fs from "fs";
const isSample = false;

function parseFields(): string[][] {
const fileName = isSample ? "/src/days/day13/sample.txt" : "/src/days/day13/full.txt";
const lines = fs.readFileSync(process.cwd() + fileName, "utf8").split("\n");
const fields: string[][] = [];
let field: string[] = [];
lines.forEach((l) => {
if (l === "") {
fields.push(field);
field = [];
} else {
field.push(l);
}
});
return fields;
}

function transposeArray(matrix: string[][]): string[][] {
const rows = matrix.length;
const cols = matrix[0].length;

// Create a new array with swapped rows and columns
const transposedMatrix: string[][] = Array.from({ length: cols }, () => Array(rows).fill(""));

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
transposedMatrix[j][i] = matrix[i][j];
}
}

return transposedMatrix;
}

export function SolvePartOne(): number {
const fields = parseFields();
let horSum = 0;
let vertSum = 0;
fields.forEach((field) => {
const horizontal = getHorizontalReflection(field);
if (horizontal !== undefined) {
horSum += horizontal + 1;
} else {
const originalToTranspose = field.map((l) => l.split(""));
const transposedField = transposeArray(originalToTranspose);
const reconstructedField: string[] = [];
transposedField.forEach((l) => reconstructedField.push(l.join("")));
const horizontal = getHorizontalReflection(reconstructedField);
if (horizontal !== undefined) {
vertSum += horizontal + 1;
} else {
throw new Error("Its fucked");
}
}
console.log("\n");
});

return horSum * 100 + vertSum;
}

export function SolvePartTwo(): number {
const transposeMap = new Map<number, boolean>();
const fields = parseFields();
const correctedFields: string[][] = [];
for (let index = 0; index < fields.length; index++) {
const copy = [...fields[index]];
const c = correctSmudgeLine(copy);
if (c !== undefined) correctedFields.push(c);
else {
const originalToTranspose = copy.map((l) => l.split(""));
const transposedField = transposeArray(originalToTranspose);
const reconstructedField: string[] = [];
transposedField.forEach((l) => reconstructedField.push(l.join("")));
const c = correctSmudgeLine(reconstructedField);
if (c !== undefined) {
correctedFields.push(c);
transposeMap.set(index, true);
} else {
throw new Error("Aal");
}
}
}
let horSum = 0;
let vertSum = 0;
correctedFields.forEach((field, i) => {
console.log(i);
const horizontal = getHorizontalReflection(field);
if (horizontal === undefined) {
const originalToTranspose = field.map((l) => l.split(""));
const transposedField = transposeArray(originalToTranspose);
const reconstructedField: string[] = [];
transposedField.forEach((l) => reconstructedField.push(l.join("")));
const horizontal = getHorizontalReflection(reconstructedField);
if (horizontal !== undefined) {
vertSum += horizontal + 1;
} else {
throw new Error("Its fucked");
}
} else {
const wasTransposed = transposeMap.get(i);
if (wasTransposed !== undefined && wasTransposed) {
vertSum += horizontal + 1;
} else {
horSum += horizontal + 1;
}
}
});
return horSum * 100 + vertSum;
}

function exchangeCharAtIndex(input: string, index: number): string {
if (input[index] === ".") return input.substring(0, index) + "#" + input.substring(index + 1);
else return input.substring(0, index) + "." + input.substring(index + 1);
}

function correctSmudgeLine(field: string[]): string[] | undefined {
let smudgeLineIndex = 0;
let isFound = false;
for (let yIndex = 0; yIndex < field.length - 1; yIndex++) {
let stepCount = 1;
if (isFound) break;
for (let upperSide = yIndex; upperSide >= 0; upperSide--) {
const upperRow = field[upperSide];
let lowerRow = undefined;
if (yIndex + stepCount < field.length) {
lowerRow = field[yIndex + stepCount];
const hammingDist = getHammingDistance(lowerRow, upperRow);
if (hammingDist === 1) {
smudgeLineIndex = upperSide;
isFound = true;
break;
}
}
stepCount++;
}
}
if (isFound) {
const correctedField: string[] = [];
let correctedRow: string = "";
for (let i = 0; i < field.length; i++) {
const currentRow = field[i];
const nextRow = field[i + 1];
if (i === smudgeLineIndex) {
const maxLength = Math.max(currentRow.length, nextRow.length);
for (let i = 0; i < maxLength; i++) {
const char1 = currentRow.charCodeAt(i) || 0;
const char2 = nextRow.charCodeAt(i) || 0;

if ((char1 ^ char2) !== 0) {
correctedRow = exchangeCharAtIndex(currentRow, i);
correctedField.push(correctedRow);
break;
}
}
} else {
correctedField.push(currentRow);
}
}

return correctedField;
} else {
return undefined;
}
}

function getHammingDistance(cur: string, next: string): number {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const hammingDistance = require("hamming");
if (cur.length !== next.length) throw new Error("Lines need to have the same length");
return hammingDistance(cur, next);
}

function prettyPrintField(field: string[]) {
field.forEach((l) => console.log(l));
}

/// Test each Y Index by comparing each row below and above the index
function getHorizontalReflection(field: string[]): number | undefined {
for (let yIndex = 0; yIndex < field.length - 1; yIndex++) {
let stepCount = 1;
let isReflection = true;
for (let upperSide = yIndex; upperSide >= 0; upperSide--) {
const upperRow = field[upperSide];
let lowerRow = undefined;
if (yIndex + stepCount < field.length) {
lowerRow = field[yIndex + stepCount];
} else {
//Reached edge, ignore rest
isReflection = true;
break;
}
if (upperRow !== lowerRow) {
isReflection = false;
break;
}
stepCount++;
}
if (isReflection) {
return yIndex;
}
}

return undefined;
}
15 changes: 15 additions & 0 deletions Typescript/2023/src/days/day13/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.

#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
6 changes: 5 additions & 1 deletion Typescript/2023/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { SolvePartOne as SolvePartOneD9, SolvePartTwo as SolvePartTwoD9 } from "
import { SolvePartOne as SolvePartOneD10, SolvePartTwo as SolvePartTwoD10 } from "./days/day10/day10";
import { SolvePartOne as SolvePartOneD11, SolvePartTwo as SolvePartTwoD11 } from "./days/day11/day11";
import { SolvePartOne as SolvePartOneD12, SolvePartTwo as SolvePartTwoD12 } from "./days/day12/day12";
const day: number = 12;
import { SolvePartOne as SolvePartOneD13, SolvePartTwo as SolvePartTwoD13 } from "./days/day13/day13";
const day: number = 13;

if (day === 1) {
console.log("Day 01 Part 01: " + SolvePartOne());
Expand Down Expand Up @@ -48,4 +49,7 @@ if (day === 1) {
} else if (day === 12) {
console.log("Day 12 Part 01: " + SolvePartOneD12());
console.log("Day 12 Part 02: " + SolvePartTwoD12());
} else if (day === 13) {
console.log("Day 13 Part 01: " + SolvePartOneD13());
console.log("Day 13 Part 02: " + SolvePartTwoD13());
}

0 comments on commit e23af4c

Please sign in to comment.