Skip to content

Commit 1fea7e1

Browse files
committed
style: format
1 parent e6e3850 commit 1fea7e1

File tree

6 files changed

+129
-118
lines changed

6 files changed

+129
-118
lines changed

day01.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const findFirstNumber = (line: string, reverse = false): string | undefined =>
4141
]
4242
.filter(([, index]) => index > -1)
4343
.sort(([, index1], [, index2]) => index1 - index2)[0]?.[0] as
44-
| keyof typeof numberMap
45-
| undefined;
44+
| keyof typeof numberMap
45+
| undefined;
4646

4747
/**
4848
* Find a number at the beginning and at the end of the line.

day03_test.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,34 @@ const schematics = [
2020
Deno.test("Day 3: Gear Ratios", async (t) => {
2121
await t.step("Part 1", async (t) => {
2222
await t.step("Example", async (t) => {
23-
await t.step("findPartNumbers()", () =>
24-
assertEquals(findPartNumbers(schematics), [
25-
{ n: 467, s: [{ s: "*", col: 3, row: 1 }] },
26-
{ n: 35, s: [{ s: "*", col: 3, row: 1 }] },
27-
{ n: 633, s: [{ s: "#", col: 6, row: 3 }] },
28-
{ n: 617, s: [{ s: "*", col: 3, row: 4 }] },
29-
{ n: 592, s: [{ s: "+", col: 5, row: 5 }] },
30-
{ n: 755, s: [{ s: "*", col: 5, row: 8 }] },
31-
{ n: 664, s: [{ s: "$", col: 3, row: 8 }] },
32-
{ n: 598, s: [{ s: "*", col: 5, row: 8 }] },
33-
])
23+
await t.step(
24+
"findPartNumbers()",
25+
() =>
26+
assertEquals(findPartNumbers(schematics), [
27+
{ n: 467, s: [{ s: "*", col: 3, row: 1 }] },
28+
{ n: 35, s: [{ s: "*", col: 3, row: 1 }] },
29+
{ n: 633, s: [{ s: "#", col: 6, row: 3 }] },
30+
{ n: 617, s: [{ s: "*", col: 3, row: 4 }] },
31+
{ n: 592, s: [{ s: "+", col: 5, row: 5 }] },
32+
{ n: 755, s: [{ s: "*", col: 5, row: 8 }] },
33+
{ n: 664, s: [{ s: "$", col: 3, row: 8 }] },
34+
{ n: 598, s: [{ s: "*", col: 5, row: 8 }] },
35+
]),
3436
);
3537

36-
await t.step(`Calculate the example solution`, () =>
37-
assertEquals(sum(findPartNumbers(schematics).map(({ n }) => n)), 4361)
38+
await t.step(
39+
`Calculate the example solution`,
40+
() =>
41+
assertEquals(
42+
sum(findPartNumbers(schematics).map(({ n }) => n)),
43+
4361,
44+
),
3845
);
3946
});
4047

4148
await t.step("Solution", async () => {
4249
const numbers = findPartNumbers(
43-
(await Deno.readTextFile("./input/day03.txt")).split("\n")
50+
(await Deno.readTextFile("./input/day03.txt")).split("\n"),
4451
);
4552
assertEquals(sum(numbers.map(({ n }) => n)), 529618);
4653
});
@@ -60,20 +67,18 @@ Deno.test("Day 3: Gear Ratios", async (t) => {
6067
await t.step(`Solve example`, () =>
6168
assertEquals(
6269
sum(findGears(schematics).map(([g1, g2]) => g1 * g2)),
63-
467835
64-
)
65-
);
70+
467835,
71+
));
6672

6773
await t.step("Solution", async () =>
6874
assertEquals(
6975
sum(
7076
findGears(
71-
(await Deno.readTextFile("./input/day03.txt")).split("\n")
72-
).map(([g1, g2]) => g1 * g2)
77+
(await Deno.readTextFile("./input/day03.txt")).split("\n"),
78+
).map(([g1, g2]) => g1 * g2),
7379
),
74-
77509019
75-
)
76-
);
80+
77509019,
81+
));
7782
});
7883
});
7984

@@ -83,7 +88,7 @@ Deno.test("Day 3: Gear Ratios", async (t) => {
8388
* TODO: refactor to search for symbols first and then numbers. This will make the code work better for part 2.
8489
*/
8590
const findPartNumbers = (
86-
schematics: string[]
91+
schematics: string[],
8792
): { n: number; s: Symbol[] }[] => {
8893
const numbers: { n: number; s: Symbol[] }[] = [];
8994
let currentNumber: string[] = [];
@@ -128,7 +133,7 @@ type Symbol = { s: string; col: number; row: number };
128133
const findAdjacentSymbols = (
129134
schematics: string[],
130135
row: number,
131-
col: number
136+
col: number,
132137
): Symbol[] =>
133138
[
134139
findSymbolAt(schematics, row, col - 1), // left
@@ -147,7 +152,7 @@ const findAdjacentSymbols = (
147152
const findSymbolAt = (
148153
schematics: string[],
149154
row: number,
150-
col: number
155+
col: number,
151156
): Symbol | undefined => {
152157
if (col < 0) return undefined; // Outside of schematics
153158
if (row < 0) return undefined; // Outside of schematics
@@ -188,7 +193,7 @@ const findGears = (schematics: string[]) => {
188193
const uniqueSymbols = (symbols: Symbol[], symbol: Symbol) => {
189194
if (
190195
symbols.find(({ col, row }) => col === symbol.col && row === symbol.row) ===
191-
undefined
196+
undefined
192197
) {
193198
return [...symbols, symbol];
194199
}

day04_test.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,63 @@ const card6 = `Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11`;
1212
Deno.test("Day 4: Scratchcards", async (t) => {
1313
await t.step("Part 1", async (t) => {
1414
await t.step("Example", async (t) => {
15-
for (const [card, expectedWinningNumbers, expectedPoints] of [
16-
[card1, [48, 83, 17, 86], 8],
17-
[card2, [32, 61], 2],
18-
[card3, [1, 21], 2],
19-
[card4, [84], 1],
20-
[card5, [], 0],
21-
[card6, [], 0],
22-
] as [string, Array<number>, number][]) {
15+
for (
16+
const [card, expectedWinningNumbers, expectedPoints] of [
17+
[card1, [48, 83, 17, 86], 8],
18+
[card2, [32, 61], 2],
19+
[card3, [1, 21], 2],
20+
[card4, [84], 1],
21+
[card5, [], 0],
22+
[card6, [], 0],
23+
] as [string, Array<number>, number][]
24+
) {
2325
await t.step(
2426
`card ${card} should have the winning numbers ${expectedWinningNumbers}`,
2527
() =>
26-
assertEquals(winningNumbers(card), new Set(expectedWinningNumbers))
28+
assertEquals(winningNumbers(card), new Set(expectedWinningNumbers)),
2729
);
2830

29-
await t.step(`card ${card} should score ${expectedPoints} points`, () =>
30-
assertEquals(cardScore(card), expectedPoints)
31+
await t.step(
32+
`card ${card} should score ${expectedPoints} points`,
33+
() => assertEquals(cardScore(card), expectedPoints),
3134
);
3235
}
3336

3437
await t.step("Sum of pile", () =>
3538
assertEquals(
3639
sum([card1, card2, card3, card4, card5, card6].map(cardScore)),
37-
13
38-
)
39-
);
40+
13,
41+
));
4042
});
4143

4244
await t.step("Solution", async () =>
4345
assertEquals(
4446
sum(
4547
(await Deno.readTextFile("./input/day04.txt"))
4648
.split("\n")
47-
.map(cardScore)
49+
.map(cardScore),
4850
),
49-
28538
50-
)
51-
);
51+
28538,
52+
));
5253
});
5354

5455
await t.step("Part 2", async (t) => {
55-
await t.step("Example", () =>
56-
assertEquals(play([card1, card2, card3, card4, card5, card6]), 30)
56+
await t.step(
57+
"Example",
58+
() => assertEquals(play([card1, card2, card3, card4, card5, card6]), 30),
5759
);
5860
await t.step("Solution", async () =>
5961
assertEquals(
6062
play((await Deno.readTextFile("./input/day04.txt")).split("\n")),
61-
9425061
62-
)
63-
);
63+
9425061,
64+
));
6465
});
6566

6667
await t.step("parseCard()", () =>
6768
assertEquals(parseCard(card3), [
6869
new Set([1, 21, 53, 59, 44]),
6970
new Set([69, 82, 63, 72, 16, 21, 14, 1]),
70-
])
71-
);
71+
]));
7272
});
7373

7474
const winningNumbers = (cardInfo: string): Set<number> => {
@@ -112,7 +112,7 @@ const play = (pile: Array<string>): number => {
112112
winners: winningNumbers(card),
113113
},
114114
}),
115-
{}
115+
{},
116116
);
117117
// We only need to go over this once, because winners will never be added to
118118
// the current or previous card
@@ -134,6 +134,6 @@ const play = (pile: Array<string>): number => {
134134
// Sum up all the card instances
135135
return Object.values(processedPile).reduce(
136136
(total, { instances }) => total + instances,
137-
0
137+
0,
138138
);
139139
};

day05_test.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,36 @@ Deno.test("Day 5: If You Give A Seed A Fertilizer", async (t) => {
1212
*/
1313
`50 98 2`,
1414
/*
15-
The second line means that the source range starts at 50 and contains 48 values: 50, 51, ..., 96, 97. This corresponds to a destination range starting at 52 and also containing 48 values: 52, 53, ..., 98, 99.
15+
The second line means that the source range starts at 50 and contains 48 values: 50, 51, ..., 96, 97. This corresponds to a destination range starting at 52 and also containing 48 values: 52, 53, ..., 98, 99.
1616
*/
1717
`52 50 48`,
1818
]);
19-
for (const [seed, soil] of [
20-
// With this information, you know that seed number 98 corresponds to soil number 50 and that seed number 99 corresponds to soil number 51.
21-
[98, 50],
22-
[99, 51],
23-
// So, seed number 53 corresponds to soil number 55.
24-
[53, 55],
25-
// Any source numbers that aren't mapped correspond to the same destination number. So, seed number 10 corresponds to soil number 10.
26-
[10, 10],
27-
// With this map, you can look up the soil number required for each initial seed number:
28-
[79, 81],
29-
[14, 14],
30-
[55, 57],
31-
[13, 13],
32-
]) {
19+
for (
20+
const [seed, soil] of [
21+
// With this information, you know that seed number 98 corresponds to soil number 50 and that seed number 99 corresponds to soil number 51.
22+
[98, 50],
23+
[99, 51],
24+
// So, seed number 53 corresponds to soil number 55.
25+
[53, 55],
26+
// Any source numbers that aren't mapped correspond to the same destination number. So, seed number 10 corresponds to soil number 10.
27+
[10, 10],
28+
// With this map, you can look up the soil number required for each initial seed number:
29+
[79, 81],
30+
[14, 14],
31+
[55, 57],
32+
[13, 13],
33+
]
34+
) {
3335
await t.step(
3436
`Seed number ${seed} corresponds to soil number ${soil}`,
35-
() => assertEquals(seedToSoil(seed), soil)
37+
() => assertEquals(seedToSoil(seed), soil),
3638
);
3739
}
3840
});
3941

4042
await t.step("Almanac example", async () => {
4143
const example = almanac(
42-
(await Deno.readTextFile("./input/day05.example.txt")).split("\n")
44+
(await Deno.readTextFile("./input/day05.example.txt")).split("\n"),
4345
);
4446

4547
/*
@@ -54,7 +56,7 @@ Deno.test("Day 5: If You Give A Seed A Fertilizer", async (t) => {
5456

5557
await t.step("Solution", async () => {
5658
const solution = almanac(
57-
(await Deno.readTextFile("./input/day05.txt")).split("\n")
59+
(await Deno.readTextFile("./input/day05.txt")).split("\n"),
5860
);
5961
assertEquals(lowest(solution), 403695602);
6062
});
@@ -63,7 +65,7 @@ Deno.test("Day 5: If You Give A Seed A Fertilizer", async (t) => {
6365
await t.step("Part 2", async (t) => {
6466
await t.step("Example", async () => {
6567
const example = lowestSeedRange(
66-
(await Deno.readTextFile("./input/day05.example.txt")).split("\n")
68+
(await Deno.readTextFile("./input/day05.example.txt")).split("\n"),
6769
);
6870
assertEquals(example, 46);
6971
});
@@ -94,7 +96,7 @@ const seedMap = (ranges: string[]) => {
9496
(range) =>
9597
Object.values(rangeRx.exec(range)?.groups ?? {}).map((s) =>
9698
parseInt(s, 10)
97-
) as [number, number, number]
99+
) as [number, number, number],
98100
)
99101
.map(([destRangeStart, sourceRangeStart, rangeLength]) => ({
100102
destRangeStart,
@@ -107,7 +109,7 @@ const seedMap = (ranges: string[]) => {
107109
// find a matching range
108110
const range = r.find(
109111
({ sourceRangeStart, sourceRangeEnd }) =>
110-
sourceRangeStart <= seed && sourceRangeEnd >= seed
112+
sourceRangeStart <= seed && sourceRangeEnd >= seed,
111113
);
112114
if (range === undefined) return seed;
113115
return seed + range.destRangeStart - range.sourceRangeStart;
@@ -140,7 +142,7 @@ const lowestSeedRange = (almanac: string[]): number => {
140142
for (let i = start; i < start + length; i++) {
141143
lowest = Math.min(
142144
maps.reduce((mapped, seedMap) => seedMap(mapped), i),
143-
lowest
145+
lowest,
144146
);
145147
}
146148
}
@@ -151,10 +153,12 @@ const lowestSeedRange = (almanac: string[]): number => {
151153
// Pass each seed through each map
152154
const mapSeeds = (
153155
seeds: number[],
154-
maps: Array<(seed: number) => number>
156+
maps: Array<(seed: number) => number>,
155157
): Set<number> =>
156158
new Set<number>(
157-
seeds.map((seed) => maps.reduce((mapped, seedMap) => seedMap(mapped), seed))
159+
seeds.map((seed) =>
160+
maps.reduce((mapped, seedMap) => seedMap(mapped), seed)
161+
),
158162
);
159163

160164
/**

day06_test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ Deno.test("Day 6: Wait For It", async (t) => {
99
[15, 40],
1010
[30, 200],
1111
]),
12-
288
13-
)
14-
);
12+
288,
13+
));
1514
await t.step(`Solve`, () =>
1615
assertEquals(
1716
waysToWin([
@@ -20,16 +19,17 @@ Deno.test("Day 6: Wait For It", async (t) => {
2019
[75, 1147],
2120
[66, 1062],
2221
]),
23-
281600
24-
)
25-
);
22+
281600,
23+
));
2624
});
2725
await t.step("Part 2", async (t) => {
28-
await t.step(`Solve the example`, () =>
29-
assertEquals(waysToWin([[71530, 940200]]), 71503)
26+
await t.step(
27+
`Solve the example`,
28+
() => assertEquals(waysToWin([[71530, 940200]]), 71503),
3029
);
31-
await t.step(`Solve`, () =>
32-
assertEquals(waysToWin([[47707566, 282107911471062]]), 33875953)
30+
await t.step(
31+
`Solve`,
32+
() => assertEquals(waysToWin([[47707566, 282107911471062]]), 33875953),
3333
);
3434
});
3535
});

0 commit comments

Comments
 (0)