mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-17 04:59:27 +00:00
Initial commit
This commit is contained in:
28
day-1/day1-part1.ts
Normal file
28
day-1/day1-part1.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import * as path from 'path'
|
||||
|
||||
export async function day1Part1() {
|
||||
const input = await Bun.file(path.join(__dirname,'input.txt')).text()
|
||||
const lines = input.split('\n')
|
||||
const column1: number[] = []
|
||||
const column2: number[] = []
|
||||
|
||||
for (const line of lines) {
|
||||
const [left, right] = line.split(' ')
|
||||
column1.push(Number.parseInt(left, 10))
|
||||
column2.push(Number.parseInt(right, 10))
|
||||
}
|
||||
|
||||
column1.sort().reverse()
|
||||
column2.sort().reverse()
|
||||
|
||||
let result = 0
|
||||
|
||||
for(let i=0; i< column1.length; i++){
|
||||
const left = column1[i]
|
||||
const right = column2[i]
|
||||
const diff = Math.abs(left - right)
|
||||
result += diff
|
||||
}
|
||||
|
||||
console.log({result})
|
||||
}
|
||||
38
day-1/day1-part2.ts
Normal file
38
day-1/day1-part2.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import * as path from 'path'
|
||||
|
||||
export async function day1Part2() {
|
||||
const start = performance.now()
|
||||
const input = await Bun.file(path.join(__dirname, 'input.txt')).text()
|
||||
const lines = input.split('\n')
|
||||
const column1: number[] = []
|
||||
const column2Map = new Map()
|
||||
|
||||
for (const line of lines) {
|
||||
const [left, right] = line.split(' ')
|
||||
const n1 = Number.parseInt(left, 10)
|
||||
const n2 = Number.parseInt(right, 10)
|
||||
column1.push(n1)
|
||||
|
||||
if (column2Map.has(n2)) {
|
||||
const v = column2Map.get(n2)
|
||||
column2Map.set(n2, v + 1)
|
||||
} else {
|
||||
column2Map.set(n2, 1)
|
||||
}
|
||||
}
|
||||
|
||||
let result = 0
|
||||
for (let i = 0; i < column1.length; i++) {
|
||||
const left = column1[i]
|
||||
if(!column2Map.has(left)){
|
||||
continue
|
||||
}
|
||||
const multiplier = column2Map.get(left)
|
||||
|
||||
result += multiplier * left
|
||||
}
|
||||
|
||||
const end = performance.now()
|
||||
|
||||
console.log({result, time: end - start})
|
||||
}
|
||||
1000
day-1/input.txt
Normal file
1000
day-1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user