mirror of
https://github.com/Aviortheking/advent-of-code.git
synced 2025-08-04 17:32:00 +00:00
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:
1
2015/day-3/input.txt
Normal file
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
34
2015/day-3/part-1.ts
Normal 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
49
2015/day-3/part-2.ts
Normal 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
|
||||
)
|
Reference in New Issue
Block a user