-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
201 additions
and
59 deletions.
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
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,33 @@ | ||
import { expect } from "@std/expect"; | ||
import { describe, it } from "@std/testing/bdd"; | ||
import { part1, part2 } from "./main.ts"; | ||
import { loadInput } from "utils"; | ||
|
||
describe("day 1 example", () => { | ||
const input = [ | ||
"3 4", | ||
"4 3", | ||
"2 5", | ||
"1 3", | ||
"3 9", | ||
"3 3", | ||
]; | ||
|
||
it("part 1", () => { | ||
expect(part1(input)).toBe(11); | ||
}); | ||
|
||
it("part 2", () => { | ||
expect(part2(input)).toBe(31); | ||
}); | ||
}); | ||
|
||
describe("day 1 solution", () => { | ||
it("part 1", async () => { | ||
expect(part1(await loadInput(1))).toBe(2378066); | ||
}); | ||
|
||
it("part 2", async () => { | ||
expect(part2(await loadInput(1))).toBe(18934359); | ||
}); | ||
}); |
File renamed without changes.
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,33 @@ | ||
import { expect } from "@std/expect"; | ||
import { describe, it } from "@std/testing/bdd"; | ||
import { part1, part2 } from "./main.ts"; | ||
import { loadInput } from "utils"; | ||
|
||
describe("day 2 example", () => { | ||
const input = [ | ||
"7 6 4 2 1", | ||
"1 2 7 8 9", | ||
"9 7 6 2 1", | ||
"1 3 2 4 5", | ||
"8 6 4 4 1", | ||
"1 3 6 7 9", | ||
]; | ||
|
||
it("part 1", () => { | ||
expect(part1(input)).toBe(2); | ||
}); | ||
|
||
it("part 2", () => { | ||
expect(part2(input)).toBe(4); | ||
}); | ||
}); | ||
|
||
describe("day 2 solution", () => { | ||
it("part 1", async () => { | ||
expect(part1(await loadInput(2))).toBe(421); | ||
}); | ||
|
||
it("part 2", async () => { | ||
expect(part2(await loadInput(2))).toBe(476); | ||
}); | ||
}); |
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,59 @@ | ||
import { loadInput } from "utils"; | ||
|
||
type Mul = readonly [number, number]; | ||
|
||
export function part1(input: Array<string>) { | ||
return [...getAllMuls(input.join("|"))].map(([a, b]) => a * b).reduce( | ||
(acc, n) => acc + n, | ||
0, | ||
); | ||
} | ||
|
||
export function part2(input: Array<string>) { | ||
return [...getAllConditionalMuls(input.join("|"))].map(([a, b]) => a * b) | ||
.reduce( | ||
(acc, n) => acc + n, | ||
0, | ||
); | ||
} | ||
|
||
function matchMul(input: string): [Mul, string, number] | null { | ||
const match = input.match(/mul\((\d{1,3}),(\d{1,3})\)/); | ||
if (match == null) return null; | ||
const remaining = input.slice(match.index! + match[0].length); | ||
return [ | ||
[parseInt(match[1], 10), parseInt(match[2], 10)], | ||
remaining, | ||
match.index!, | ||
]; | ||
} | ||
|
||
function* getAllMuls(input: string) { | ||
let remaining = input; | ||
while (true) { | ||
const match = matchMul(remaining); | ||
if (match == null) { | ||
break; | ||
} | ||
yield match[0]; | ||
remaining = match[1]; | ||
} | ||
} | ||
|
||
function* getAllConditionalMuls(input: string) { | ||
const doables = input.split("do()"); | ||
for (let doable of doables) { | ||
const indexOfDont = doable.indexOf("don't()"); | ||
if (indexOfDont !== -1) { | ||
doable = doable.slice(0, indexOfDont); | ||
} | ||
for (const mul of getAllMuls(doable)) { | ||
yield mul; | ||
} | ||
} | ||
} | ||
|
||
if (import.meta.main) { | ||
console.log(part1(await loadInput(3))); | ||
console.log(part2(await loadInput(3))); | ||
} |
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,32 @@ | ||
import { expect } from "@std/expect"; | ||
import { describe, it } from "@std/testing/bdd"; | ||
import { part1, part2 } from "./main.ts"; | ||
import { loadInput } from "utils"; | ||
|
||
describe("day 3 example", () => { | ||
it("part 1", () => { | ||
expect( | ||
part1([ | ||
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))", | ||
]), | ||
).toBe(161); | ||
}); | ||
|
||
it("part 2", () => { | ||
expect( | ||
part2([ | ||
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))", | ||
]), | ||
).toBe(48); | ||
}); | ||
}); | ||
|
||
describe("day 3 solution", () => { | ||
it("part 1", async () => { | ||
expect(part1(await loadInput(3))).toBe(162813399); | ||
}); | ||
|
||
it("part 2", async () => { | ||
expect(part2(await loadInput(3))).toBe(53783319); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,18 +1,10 @@ | ||
type PartFunction = (input: Array<string>) => number; | ||
type Solution = { part1: PartFunction; part2: PartFunction }; | ||
|
||
const days: Record<string, Solution> = {}; | ||
const days = new Map<number, Solution>(); | ||
|
||
for (const day of [1, 2]) { | ||
days[pad(day)] = await import(`./${pad(day)}/main.ts`); | ||
} | ||
|
||
export function getSolution(day: string | number): Solution | null { | ||
return days[pad(day)]; | ||
for (const day of [1, 2, 3]) { | ||
days.set(day, await import(`./${day}/main.ts`)); | ||
} | ||
|
||
export { days }; | ||
|
||
function pad(day: string | number) { | ||
return day.toString().padStart(2, "0"); | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.