Skip to content

Commit de544b8

Browse files
committed
working day 10, tweaks
1 parent 16687ec commit de544b8

File tree

6 files changed

+112
-51
lines changed

6 files changed

+112
-51
lines changed

2023/02/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const CHECKS = {
2121

2222
function isValidGame(round: Round): boolean {
2323
return ["red", "blue", "green"].every((color) =>
24-
round.games.every((game) => game[color] <= CHECKS[color])
24+
round.games.every(
25+
(game) => game[color as keyof Game] <= CHECKS[color as keyof Game]
26+
)
2527
);
2628
}
2729

@@ -36,7 +38,7 @@ function parse(line: string): Round {
3638
(acc, draw) => {
3739
const [quantity, color] = draw.trim().split(" ");
3840

39-
acc[color] = parseInt(quantity);
41+
acc[color as keyof Game] = parseInt(quantity);
4042

4143
return acc;
4244
},

2023/03/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function getAdjacentGears(
8484
): string[] {
8585
const out: string[] = [];
8686

87-
const getKey = (x, y) => `${x}-${y}`;
87+
const getKey = (x: number, y: number) => `${x}-${y}`;
8888

8989
const minX = Math.max(x - 1, 0);
9090
const maxX = Math.min(grid[0].length - 1, x + digitCount + 1);

2023/06/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* switchs from true->false and false->true then manually calculated the difference.
66
*/
77

8-
98
import fs from "fs/promises";
109
import _ from "lodash";
1110
const sampleSol = 288;
@@ -15,8 +14,8 @@ function parse(line: string): number[] {
1514
return line.split("").map((x) => parseInt(x, 10));
1615
}
1716

18-
function didWin(time, holdCount, record) {
19-
return record < holdCount * (time - holdCount);
20-
}
17+
// function didWin(time, holdCount, record) {
18+
// return record < holdCount * (time - holdCount);
19+
// }
2120

22-
60947882 / 2 + 21294301 - (60947882 / 10 + 3084851);
21+
// 60947882 / 2 + 21294301 - (60947882 / 10 + 3084851);

2023/10/index.ts

+87-41
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs/promises";
22
import _ from "lodash";
33
const sampleSol = 8;
4-
const sample2Sol = 0;
4+
const sample2Sol = 8;
55

66
// 0 - up
77
// 1 - right
@@ -19,10 +19,14 @@ interface Position {
1919
x: number;
2020
y: number;
2121
o: number;
22-
po: number;
22+
po: number; // previous orientation
2323
}
2424

25-
function nextPosition(grid: string[][], { x, y, o }: Position): Position {
25+
function nextPosition(
26+
grid: string[][],
27+
{ x, y, o }: Position,
28+
startingPos: Position[] = []
29+
): Position {
2630
const connecting = FACING_TO_CONNECTING_PIPES[o];
2731

2832
const nextCoord = [
@@ -45,7 +49,9 @@ function nextPosition(grid: string[][], { x, y, o }: Position): Position {
4549
const nextValue = grid[nextCoord[1]][nextCoord[0]];
4650
const orientationChange = connecting.indexOf(nextValue) - 1;
4751

48-
if (orientationChange === -2 && nextValue !== "S") {
52+
if (nextValue === "S") {
53+
return startingPos.find(({ po }) => po === o) as Position;
54+
} else if (orientationChange === -2) {
4955
throw new Error(
5056
"invalid connection" + JSON.stringify({ x, y, o, nextCoord, nextValue })
5157
);
@@ -63,26 +69,21 @@ function getStartingPositions(grid: string[][]): Position[] | null {
6369
for (let y = 0; y < grid.length; y++) {
6470
for (let x = 0; x < grid[0].length; x++) {
6571
if (grid[y][x] === "S") {
66-
const out: Position[] = [];
67-
72+
const validOrientations: number[] = [];
6873
for (let o = 0; o < 4; o++) {
6974
let pos: Position | null = null;
7075
try {
7176
pos = nextPosition(grid, { x, y, o, po: -1 });
7277
} catch (e) {
73-
console.log(e);
7478
// ignore
7579
}
76-
if (pos === null) continue;
77-
78-
if (FACING_TO_CONNECTING_PIPES[o].includes(grid[pos.y][pos.x])) {
79-
const orientationChange =
80-
FACING_TO_CONNECTING_PIPES[o].indexOf(grid[pos.y][pos.x]) - 1;
81-
out.push({ x, y, o, po: (o - orientationChange + 4) % 4 });
82-
}
80+
if (pos !== null) validOrientations.push(o);
8381
}
8482

85-
return out;
83+
return [
84+
{ x, y, o: validOrientations[0], po: (validOrientations[1] + 2) % 4 },
85+
{ x, y, o: validOrientations[1], po: (validOrientations[0] + 2) % 4 },
86+
];
8687
}
8788
}
8889
}
@@ -99,20 +100,29 @@ function partOne(rawLines: string[]) {
99100
throw new Error("Failed to find starting pos");
100101
}
101102

102-
let pos = nextPosition(grid, startingPos[0]),
103-
i = 1;
103+
let pos = startingPos[0],
104+
i = 0;
104105

105-
while (
106+
do {
107+
pos = nextPosition(grid, pos, startingPos);
108+
i++;
109+
} while (
106110
!(startingPos[0].x === pos.x && startingPos[0].y === pos.y) &&
107111
i < 10000000
108-
) {
109-
pos = nextPosition(grid, pos);
110-
i++;
111-
}
112+
);
112113

113114
return i / 2;
114115
}
115116

117+
function getVerticalO(pos: Position): number {
118+
if (pos.o % 2 == 0 && pos.po % 2 === 0) {
119+
throw new Error("passed bad position");
120+
}
121+
122+
if (pos.o % 2 === 0) return pos.o;
123+
return pos.po;
124+
}
125+
116126
function partTwo(rawLines: string[]) {
117127
const grid = rawLines.map((x) => x.split(""));
118128

@@ -122,46 +132,82 @@ function partTwo(rawLines: string[]) {
122132
throw new Error("Failed to find starting pos");
123133
}
124134

125-
let pos = nextPosition(grid, startingPos[0]),
126-
i = 1;
135+
let pos = startingPos[0],
136+
i = 0;
127137

128-
const positions = [startingPos[0], pos];
129-
while (
130-
!(startingPos[0].x === pos.x && startingPos[0].y === pos.y) &&
131-
i < 10000000
132-
) {
133-
pos = nextPosition(grid, pos);
138+
const positions: Position[] = [pos];
139+
do {
140+
pos = nextPosition(grid, pos, startingPos);
134141
positions.push(pos);
135142
i++;
136-
}
143+
} while (
144+
!(startingPos[0].x === pos.x && startingPos[0].y === pos.y) &&
145+
i < 10000000
146+
);
137147

138148
const lookUp = positions.reduce((acc, pos) => {
139149
acc[`${pos.x}:${pos.y}`] = pos;
140150
return acc;
141151
}, {} as Record<string, Position>);
142-
const printMap: string[] = [];
143-
const isOdd = false;
144-
const interior = 0;
152+
let interior = 0;
153+
let out = "";
154+
let print = "";
145155
for (let y = 0; y < grid.length; y++) {
156+
let isOdd = false;
157+
let lastPO = null;
158+
let inSegment = false;
146159
for (let x = 0; x < grid[0].length; x++) {
147160
const pos = lookUp[`${x}:${y}`];
148161
if (pos) {
149-
row += grid[y][x];
162+
const val = grid[pos.y][pos.x];
163+
164+
print += val;
165+
if (pos.o % 2 === 0 && pos.po % 2 === 0) {
166+
isOdd = !isOdd;
167+
out += isOdd ? "^" : "V";
168+
} else if (!inSegment) {
169+
lastPO = getVerticalO(pos);
170+
inSegment = true;
171+
out += "?";
172+
} else {
173+
if (pos.o % 2 === 1 && pos.po % 2 === 1) {
174+
out += "-";
175+
} else {
176+
const newPO = getVerticalO(pos);
177+
178+
if (newPO === lastPO) {
179+
isOdd = !isOdd;
180+
out += isOdd ? "^" : "V";
181+
} else {
182+
out += "U";
183+
}
184+
inSegment = false;
185+
lastPO = null;
186+
}
187+
}
188+
} else if (isOdd) {
189+
interior++;
190+
out += "O";
150191
} else {
151-
row += "X";
192+
print += " ";
193+
out += ".";
152194
}
153195
}
154-
printMap.push(row);
196+
out += "\n";
197+
print += "\n";
155198
}
156-
157-
console.log(printMap.join("\n"));
158-
return 0;
199+
// console.log(print);
200+
// console.log(out);
201+
return interior;
159202
}
160203

161204
(async function main() {
162205
const sample = await fs
163206
.readFile(__dirname + "/sample.txt", "utf8")
164207
.then((txt) => txt.split("\n"));
208+
const sample2 = await fs
209+
.readFile(__dirname + "/sample2.txt", "utf8")
210+
.then((txt) => txt.split("\n"));
165211
const input = await fs
166212
.readFile(__dirname + "/input.txt", "utf8")
167213
.then((txt) => txt.split("\n"));
@@ -176,7 +222,7 @@ function partTwo(rawLines: string[]) {
176222
const sol1 = await partOne(input);
177223
console.log("part 1 sol:", sol1);
178224

179-
const test2 = await partTwo(sample);
225+
const test2 = await partTwo(sample2);
180226
console.log("part 2 sample", test2);
181227
if (test2 !== sample2Sol) {
182228
console.log("Failed the part 2 test");

2023/10/sample2.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.F----7F7F7F7F-7....
2+
.|F--7||||||||FJ....
3+
.||.FJ||||||||L7....
4+
FJL7L7LJLJ||LJ.L-7..
5+
L--J.L7...LJS7F-7L7.
6+
....F-J..F7FJ|L7L7L7
7+
....L7.F7||L7|.L7L7|
8+
.....|FJLJ|FJ|F7|.LJ
9+
....FJL-7.||.||||...
10+
....L---J.LJ.LJLJ...

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"name": "adventofcode",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "five.js",
65
"scripts": {
6+
"clean": "rimraf build",
7+
"copy": "copyfiles 2023/**/*.txt build",
8+
"build": "yarn clean && yarn copy && tsc -b",
79
"test": "tsc --noEmit && eslint ."
810
},
911
"keywords": [],
@@ -16,12 +18,14 @@
1618
"abstract-astar": "^0.2.0",
1719
"chalk": "^5.2.0",
1820
"console.table": "^0.10.0",
21+
"copyfiles": "^2.4.1",
1922
"inquirer": "^9.1.4",
2023
"kruskal": "^0.0.5",
2124
"lodash": "^4.17.21",
2225
"log-update": "^5.0.1",
2326
"mathjs": "^11.5.0",
24-
"random-seed": "^0.3.0"
27+
"random-seed": "^0.3.0",
28+
"rimraf": "^5.0.5"
2529
},
2630
"devDependencies": {
2731
"@types/lodash": "^4.14.191",

0 commit comments

Comments
 (0)