Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions solutions/2023/dotloadmovie/src/day10/data/data.txt

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions solutions/2023/dotloadmovie/src/day10/html/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const cells = document.querySelectorAll('td.cell-none');

cells.forEach((cell) => {
cell.addEventListener('click', (el) => {
cell.classList.toggle('cell-selected');
});
});
41 changes: 41 additions & 0 deletions solutions/2023/dotloadmovie/src/day10/html/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
table {
border-collapse: collapse;
}

td {
width: 10px;
height: 10px;

border: 1px solid red;
}

td span {
display: none;
}

td.cell-vert {
background: url('./img/vert.png') center center no-repeat;
}

td.cell-horiz {
background: url('./img/horiz.png') center center no-repeat;
}

td.cell-F {
background: url('./img/tl.png') center center no-repeat;
}
td.cell-7 {
background: url('./img/tr.png') center center no-repeat;
}

td.cell-L {
background: url('./img/bl.png') center center no-repeat;
}

td.cell-J {
background: url('./img/br.png') center center no-repeat;
}

td.cell-selected {
background: green;
}
1 change: 1 addition & 0 deletions solutions/2023/dotloadmovie/src/day10/html/table.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions solutions/2023/dotloadmovie/src/day10/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { solution1, solution2 } from './index';

describe('tests', () => {
/*test('test solution1', () => {
const output = solution1();
expect(output).toEqual(6778);
});*/

test('test solution2', () => {
const output = solution2();
expect(output).toEqual(10);
});
});
4 changes: 4 additions & 0 deletions solutions/2023/dotloadmovie/src/day10/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { solution1 } from "./solution1";
import { solution2 } from "./solution2";

export { solution1, solution2 };
126 changes: 126 additions & 0 deletions solutions/2023/dotloadmovie/src/day10/solution1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import fs from 'fs';

type Position = {
x: number;
y: number;
};

type GridElement = {
exits: Position[] | null;
self: Position;
char: string;
} | null;

interface List {
[key: string]: GridElement;
}

export const solution1 = () => {
const getPoints = (symbol: string, position: Position): Position[] | null => {
let output = null;

if (symbol === 'F') {
output = [
{ x: position.x + 1, y: position.y },
{ x: position.x, y: position.y + 1 },
];
}

if (symbol === '|') {
output = [
{ x: position.x, y: position.y + 1 },
{ x: position.x, y: position.y - 1 },
];
}

if (symbol === '-') {
output = [
{ x: position.x + 1, y: position.y },
{ x: position.x - 1, y: position.y },
];
}

if (symbol === 'L') {
output = [
{ x: position.x, y: position.y - 1 },
{ x: position.x + 1, y: position.y },
];
}

if (symbol === '7') {
output = [
{ x: position.x - 1, y: position.y },
{ x: position.x, y: position.y + 1 },
];
}

if (symbol === 'J') {
output = [
{ x: position.x, y: position.y - 1 },
{ x: position.x - 1, y: position.y },
];
}

return output;
};

const input = fs
.readFileSync(`${__dirname}/data/data.txt`, 'utf-8')
.split('\n');

const start: Position = { x: 0, y: 0 };
const firstStep: Position = { x: 31, y: 25 };

const list: List = {};

const grid = input.map((row: string, rowi: number) => {
const output = row.split('');

output.forEach((char: string, coli: number) => {
if (char === 'S') {
start.x = coli;
start.y = rowi;
}
});

return output;
});

const buildList = (): any => {
let currPositions: any = { lastPos: start, thisPos: firstStep };
let currChar = '';

const step = (lastPos: Position, thisPos: Position): any => {
const char = grid[thisPos.y][thisPos.x];
const exits = getPoints(char, thisPos);

currChar = char;

list[`${thisPos.x}-${thisPos.y}`] = {
exits,
self: thisPos,
char,
};

currPositions.thisPos = exits?.filter((pos: Position) => {
if (pos.x === lastPos.x && pos.y === lastPos.y) {
return false;
}

return true;
})[0] || { x: -1, y: -1 };

currPositions.lastPos = { ...thisPos };
};

while (currChar !== 'S') {
step(currPositions.lastPos, currPositions.thisPos);
}
};

buildList();

const output = Math.floor((Object.keys(list).length + 1) / 2);

return output;
};
176 changes: 176 additions & 0 deletions solutions/2023/dotloadmovie/src/day10/solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import fs from 'fs';

type Position = {
x: number;
y: number;
};

type GridElement = {
exits: Position[] | null;
self: Position;
char: string;
} | null;

interface List {
[key: string]: GridElement;
}

export const solution2 = () => {
const getPoints = (symbol: string, position: Position): Position[] | null => {
let output = null;

if (symbol === 'F') {
output = [
{ x: position.x + 1, y: position.y },
{ x: position.x, y: position.y + 1 },
];
}

if (symbol === '|') {
output = [
{ x: position.x, y: position.y + 1 },
{ x: position.x, y: position.y - 1 },
];
}

if (symbol === '-') {
output = [
{ x: position.x + 1, y: position.y },
{ x: position.x - 1, y: position.y },
];
}

if (symbol === 'L') {
output = [
{ x: position.x, y: position.y - 1 },
{ x: position.x + 1, y: position.y },
];
}

if (symbol === '7') {
output = [
{ x: position.x - 1, y: position.y },
{ x: position.x, y: position.y + 1 },
];
}

if (symbol === 'J') {
output = [
{ x: position.x, y: position.y - 1 },
{ x: position.x - 1, y: position.y },
];
}

return output;
};

const input = fs
.readFileSync(`${__dirname}/data/data.txt`, 'utf-8')
.split('\n');

const start: Position = { x: 0, y: 0 };
const firstStep: Position = { x: 31, y: 25 };

const list: List = {};

const grid = input.map((row: string, rowi: number) => {
const output = row.split('');

output.forEach((char: string, coli: number) => {
if (char === 'S') {
start.x = coli;
start.y = rowi;
}
});

return output;
});

const buildList = (): any => {
let currPositions: any = { lastPos: start, thisPos: firstStep };
let currChar = '';

const step = (lastPos: Position, thisPos: Position): any => {
const char = grid[thisPos.y][thisPos.x];
const exits = getPoints(char, thisPos);

currChar = char;

list[`${thisPos.x}-${thisPos.y}`] = {
exits,
self: thisPos,
char,
};

currPositions.thisPos = exits?.filter((pos: Position) => {
if (pos.x === lastPos.x && pos.y === lastPos.y) {
return false;
}

return true;
})[0] || { x: -1, y: -1 };

currPositions.lastPos = { ...thisPos };
};

while (currChar !== 'S') {
step(currPositions.lastPos, currPositions.thisPos);
}
};

const plotList = () => {
grid.forEach((row: any, i: number) => {
row.forEach((col: any, j: number) => {
grid[i][j] = '';
});
});

Object.values(list).forEach((el: GridElement) => {
const mapped = grid[el?.self.y as number][el?.self.x as number];

grid[el?.self.y as number][el?.self.x as number] = el?.char || '';
});
};

const getClassName = (char: string) => {
if (char === '-') {
return 'cell-horiz';
}

if (char === '|') {
return 'cell-vert';
}

if (char === '') {
return 'cell-none';
}

return 'cell-' + char;
};

const exportGrid = () => {
const output: any = grid.map((row: any) => {
const rowOutput = row.map((col: any) => {
return `<td class="${getClassName(col)}"><span>${col}</span></td>`;
});

return `<tr>${rowOutput.join('')}</tr>`;
});

const toRender = `<html><head><link rel="stylesheet" href="./styles.css" /></head><body><table><tbody>${output.join(
''
)}</tbody></table><script src="./script.js"></script></body></html>`;

fs.writeFileSync(`${__dirname}/html/table.html`, toRender);
};

buildList();

plotList();

exportGrid();

const output = 10;

return output;
};
Loading