-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:kws/AdventOfCode
- Loading branch information
Showing
96 changed files
with
7,641 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.