mirror of
https://github.com/seigler/aoc2024
synced 2025-07-28 09:36:09 +00:00
day 1
This commit is contained in:
commit
d54c4c67a2
14 changed files with 2376 additions and 0 deletions
9
src/day01/README.md
Normal file
9
src/day01/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 1 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/1)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
64
src/day01/index.ts
Normal file
64
src/day01/index.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput: string) => {
|
||||
const rows = rawInput.split('\n').map(line => line.split(/\s+/).map(Number))
|
||||
let lists: number[][] = [[], []]
|
||||
rows.forEach(row => {
|
||||
row.forEach((value, index) => lists[index].push(value))
|
||||
})
|
||||
return lists
|
||||
}
|
||||
|
||||
const part1 = (rawInput: string) => {
|
||||
const input = parseInput(rawInput)
|
||||
input.forEach(list => list.sort())
|
||||
let total = 0
|
||||
for (let i = 0; i < input[0].length; i++) {
|
||||
total += Math.abs(input[0][i] - input[1][i])
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const [left, right] = parseInput(rawInput)
|
||||
right.sort()
|
||||
const counts = new Map<number, number>()
|
||||
right.forEach(entry => {
|
||||
counts.set(entry, (counts.get(entry) ?? 0) + 1)
|
||||
})
|
||||
const total = left.reduce((acc, cur) => { return acc + cur * (counts.get(cur) ?? 0)}, 0)
|
||||
return total
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [
|
||||
{
|
||||
input: `3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3`,
|
||||
expected: 11,
|
||||
},
|
||||
],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [
|
||||
{
|
||||
input: `3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3`,
|
||||
expected: 31,
|
||||
},
|
||||
],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
})
|
9
src/day02/README.md
Normal file
9
src/day02/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2024 - day 2 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2024/day/2)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
38
src/day02/index.ts
Normal file
38
src/day02/index.ts
Normal 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,
|
||||
})
|
38
src/template/index.ts
Normal file
38
src/template/index.ts
Normal 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
31
src/utils/index.ts
Normal 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'
|
||||
*
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue