days 1 and 2 from last year

This commit is contained in:
Joshua Seigler 2024-12-04 11:14:53 -05:00
commit a71ae9c4d7
No known key found for this signature in database
15 changed files with 2167 additions and 0 deletions

9
src/day01/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2023 - day 1 🎄
## Info
Task description: [link](https://adventofcode.com/2023/day/1)
## Notes
...

75
src/day01/index.ts Normal file
View file

@ -0,0 +1,75 @@
import run from "aocrunner"
const parseInput = (rawInput: string) => rawInput.split("\n")
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
return input.reduce((acc, line) => {
const [left] = line.match(/\d/)!
const [, right] = line.match(/.*(\d)/)!
return acc + parseInt(left) * 10 + parseInt(right)
}, 0)
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
return input.reduce((acc, line) => {
const [left] = line.match(
/\d|one|two|three|four|five|six|seven|eight|nine/,
)!
const [, right] = line.match(
/.*(\d|one|two|three|four|five|six|seven|eight|nine)/,
)!
const lookup = new Map<string, number>()
for (let i = 1; i < 10; i++) {
lookup.set(String(i), i)
lookup.set(
[
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
][i - 1],
i,
)
}
return acc + lookup.get(left)! * 10 + lookup.get(right)!
}, 0)
}
run({
part1: {
tests: [
{
input: `1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet`,
expected: 142,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen`,
expected: 281,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})

9
src/day02/README.md Normal file
View file

@ -0,0 +1,9 @@
# 🎄 Advent of Code 2023 - day 2 🎄
## Info
Task description: [link](https://adventofcode.com/2023/day/2)
## Notes
...

94
src/day02/index.ts Normal file
View file

@ -0,0 +1,94 @@
import run from "aocrunner"
const parseInput = (rawInput: string) => {
const games = rawInput.split("\n").map((line) => {
const [, number, contents] = line.match(/Game (\d+): (.*)/)!
const gameNumber = parseInt(number)
const handfuls = contents.split("; ").map((types) =>
types.split(", ").map((set) => {
const [qty, color] = set.split(" ")
return {
qty: parseInt(qty),
color: color as "red" | "green" | "blue",
}
}),
)
return {
gameNumber,
handfuls,
}
})
return games
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
return input.reduce((total, game) => {
const { gameNumber, handfuls } = game
for (const handful of handfuls) {
if (
handful.some(
(set) =>
set.qty >
{
red: 12,
green: 13,
blue: 14,
}[set.color],
)
) {
return total
}
}
return total + gameNumber
}, 0)
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
return input.reduce((total, game) => {
const { handfuls } = game
const minimums = {
red: 0,
green: 0,
blue: 0,
}
for (const handful of handfuls) {
for (const set of handful) {
minimums[set.color] = Math.max(minimums[set.color] ?? 0, set.qty)
}
}
return total + minimums.red * minimums.blue * minimums.green
}, 0)
}
run({
part1: {
tests: [
{
input: `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`,
expected: 8,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`,
expected: 2286,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})

38
src/template/index.ts Normal file
View file

@ -0,0 +1,38 @@
import run from "aocrunner"
const parseInput = (rawInput: string) => rawInput
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
return
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
return
}
run({
part1: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
})

31
src/utils/index.ts Normal file
View file

@ -0,0 +1,31 @@
/**
* Root for your util libraries.
*
* You can import them in the src/template/index.ts,
* or in the specific file.
*
* Note that this repo uses ES Modules, so you have to explicitly specify
* .js extension (yes, .js not .ts - even for TypeScript files)
* for imports that are not imported from node_modules.
*
* For example:
*
* correct:
*
* import _ from 'lodash'
* import myLib from '../utils/myLib.js'
* import { myUtil } from '../utils/index.js'
*
* incorrect:
*
* import _ from 'lodash'
* import myLib from '../utils/myLib.ts'
* import { myUtil } from '../utils/index.ts'
*
* also incorrect:
*
* import _ from 'lodash'
* import myLib from '../utils/myLib'
* import { myUtil } from '../utils'
*
*/