Added the four first days of 2015

Yes i'm gonna do them all !

Signed-off-by: Avior <github@avior.me>
This commit is contained in:
Florian Bouillon 2021-12-07 17:03:46 +01:00
parent c0369d340a
commit 1060cb8a09
Signed by: Florian Bouillon
GPG Key ID: BEEAF3722D0EBF64
11 changed files with 1179 additions and 0 deletions

1
2015/day-1/input.txt Normal file

File diff suppressed because one or more lines are too long

8
2015/day-1/part-1.ts Normal file
View File

@ -0,0 +1,8 @@
import fs from 'fs'
const input = fs.readFileSync(__dirname + '/input.txt').toString()
.split('')
console.log(
"Result:", input.reduce((p, c) => p + (c === '(' ? 1 : -1), 0)
)

19
2015/day-1/part-2.ts Normal file
View File

@ -0,0 +1,19 @@
import fs from 'fs'
const input = fs.readFileSync(__dirname + '/input.txt').toString()
.split('')
let v = 0
for (let i = 0; i < input.length; i++) {
const item = input[i];
if (item === '(') {
v++
continue
}
if (--v < 0) {
console.log(
"Result:", i + 1
)
break
}
}

1000
2015/day-2/input.txt Normal file

File diff suppressed because it is too large Load Diff

17
2015/day-2/part-1.ts Normal file
View File

@ -0,0 +1,17 @@
import fs from 'fs'
const input: Array<[number, number, number]> = fs.readFileSync(__dirname + '/input.txt').toString()
.split('\n')
.map((it) => it.split('x').map((v) => parseInt(v)) as [number, number, number])
function calc(item: [number, number, number]) {
const l = item[0]
const w = item[1]
const h = item[2]
const smallestSide = Math.min(l*w, w*h, h*l)
return 2*l*w + 2*w*h + 2*h*l + smallestSide
}
console.log(
"Result:", input.reduce((p, c) => p + calc(c), 0)
)

18
2015/day-2/part-2.ts Normal file
View File

@ -0,0 +1,18 @@
import fs from 'fs'
const input: Array<[number, number, number]> = fs.readFileSync(__dirname + '/input.txt').toString()
.split('\n')
.map((it) => it.split('x').map((v) => parseInt(v)) as [number, number, number])
function calc(item: [number, number, number]) {
const l = item[0]
const w = item[1]
const h = item[2]
const largest = Math.max(l, w, h)
const tmp = l === largest ? w+h : w === largest ? l+h : l+w
return tmp + tmp + l*w*h
}
console.log(
"Result:", input.reduce((p, c) => p + calc(c), 0)
)

1
2015/day-3/input.txt Normal file

File diff suppressed because one or more lines are too long

34
2015/day-3/part-1.ts Normal file
View File

@ -0,0 +1,34 @@
import fs from 'fs'
const input = fs.readFileSync(__dirname + '/input.txt').toString()
.split('') as Array<'<' | '>' | 'v' | '^'>
const poses: Array<`${number}:${number}`> = ['0:0']
let x = 0
let y = 0
for (const direction of input) {
switch (direction) {
case '>':
x++
break;
case '<':
x--
break;
case '^':
y--
break;
case 'v':
y++
break;
}
const v: `${number}:${number}` = `${x}:${y}`
if (!poses.includes(v)) {
poses.push(v)
}
}
console.log(
"Result:", poses.length
)

49
2015/day-3/part-2.ts Normal file
View File

@ -0,0 +1,49 @@
import fs from 'fs'
const input = fs.readFileSync(__dirname + '/input.txt').toString()
.split('') as Array<'<' | '>' | 'v' | '^'>
const poses: Array<`${number}:${number}`> = ['0:0']
let x = 0
let y = 0
let roboX = 0
let roboY = 0
for (let i = 0; i < input.length; i++) {
const direction = input[i]
let xChange = 0
let yChange = 0
switch (direction) {
case '>':
xChange++
break;
case '<':
xChange--
break;
case '^':
yChange--
break;
case 'v':
yChange++
break;
}
let v: `${number}:${number}`
if (i % 2 === 0) { // santa
x += xChange
y += yChange
v = `${x}:${y}`
} else { // roboSanta
roboX += xChange
roboY += yChange
v = `${roboX}:${roboY}`
}
if (!poses.includes(v)) {
poses.push(v)
}
}
console.log(
"Result:", poses.length
)

16
2015/day-4/part-1.ts Normal file
View File

@ -0,0 +1,16 @@
import crypto from 'crypto'
// const input = "pqrstuv"
const input = "yzbqklnj"
let hash = ""
let i = 0
do {
hash = crypto.createHash('md5').update(input + ++i).digest('hex')
} while (!hash.startsWith('00000'))
console.log(
"Result:", i, hash
)

16
2015/day-4/part-2.ts Normal file
View File

@ -0,0 +1,16 @@
import crypto from 'crypto'
// const input = "pqrstuv"
const input = "yzbqklnj"
let hash = ""
let i = 0
do {
hash = crypto.createHash('md5').update(input + ++i).digest('hex')
} while (!hash.startsWith('000000'))
console.log(
"Result:", i, hash
)