Skip to content

Commit 94cebd1

Browse files
committed
feat: solve advent of code 2015 day 1 part a
1 parent d764ef7 commit 94cebd1

File tree

8 files changed

+152
-0
lines changed

8 files changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(((())))()((((((((())()(()))(()((((()(()(((()((()((()(()()()()()))(((()(()((((((((((())(()()((())()(((())))()(()(()((()(()))(()()()()((()((()(((()()(((((((()()())()((((()()(((((()(())()(())((())()()))()(((((((())(()())(()(((())(()))((())))(()((()())))()())((((())))(()(((((()(())(((()()((()((()((((((((((())(()())))))()))())()()((((()()()()()()((((((())())(((()())()((()()(((()()()))(((((()))(((()(()()()(()(()(((())()))(()(((()((())()(()())())))((()()()(()()(((()))(((()((((()(((((()()(()())((()())())(()((((((()(()()))((((()))))())((())()()((()(()))))((((((((()))(()()(((())())(())()((()()()()((()((()((()()(((())))(()((())()((((((((()((()(()()(((())())())))(())())))()((((()))))))())))()()))()())((()())()((()()()))(()()(((()(())((((())())((((((((()()()()())))()()()((((()()))))))()((((()(((()))(()()())))((()()(((()))()()())())(((())((()()(())()()()(((())))))()())((()))()))((())()()())()())()()(()))())))())()))(())((()(())))(()(())(()))))(()(())())(()(())(()(()))))((()())()))()((((()()))))())))()()())((())()((()()()))()(((()(()))))(())()()))(((()())))))))))(((())))()))())()))))()()(((())))))))()(()()(()))((()))))((())))((()((())))())))()()(()))())()(()((()())(()(()()())())(()()))()))))(()())()()))()()()()))(()(()(()))))))()(()))()))()()(()((())(()(())))()(((())(())())))))()(()(()))))()))(()()()(())()(()(())))()))))()()(((((())))))())()())())())()())()))))()))))))))())()()()()()()())))()))((())()))())))()((())()))))()))())))))))())()()()))()()(()((((()(((((((()(())((()())((()()))()))))(())))()()()(())((())()())))(())))(())))(((()()))()(())(((()(()))((())))())()))((((()))())()))))))))()(())())))(()))()(()()))())()()(())())))())()()(()())))()((()())(()(())(())))))))))))))(()))))()))))))()()())(()(((((()(()())))())()))(()))()))(()()))()())(()))())()(())((()()))))))())))())()(((())))(()(()))()()))()(()))))))((()())(()))))))()())))()()))))))))((((((((()()()(()))))))()())))())))()()((())()))((())(())))())())))()()()((()((()(())))())()(())))))))))()())))()()()()()()))()))((())())(()(()))))))(()()))()))(())))()))))))))))))(()))))))))()))))()))()())()))()()))))))()))))((()))))(()))())()(())))(()())((((()())))()))))(()))()(()()(())))))())))))()))))))())))())))))())))())())))())(()))))(())()(())))())()))((()()))))))())))((())))))))())))(())))))()()())))))())))))()))))))()))()()()(()(((()())())())(()))())))))((()(())(()))))))))(())))()()()())())(()))))()()()))()))())())())()(())))()(((()((((())))))))()))))))))))))))))))))((())()())(()))))()()))))))(()()(())())))())))((())))((())))))))))))))()))))()(()))))))())))))()))(()()())(()())))))))))()))))))(())))))()()))()())(((())))()))(()))))))))(())())))())))())())())()()))((())()(())()())()))()())(())(()))))()())))(()(((()))))))()(()())()()()))()))))))))()()()(())()())()(((((()))()())())(()))))()()()(())))())))()((()())))(()))())()(()())())(()))()()))((()()))((()()()()())))(())()))(()(())))((()()))))))))())))))))())()()))))))))))))))))(())()(())(())()())())()))()(()))))())())))))()())()(()))()()(())))(())())))))(()))))))))))))))())())(())(())))(((()))()))))())((())(()))())))))))())))))())))()))()))))))))))))())()))))()))))((()))(())))()(())))(())()))()))())))())))))))()(()())())))()()())))(())))))(()))))))))))))(()))()))()))())))(((()()()(())((()())))()())(((()))(())()))((()()()())))())(())(()))))()(((((())))(()))())())))))))((((()()()))())())()(()(()())))))))))()())())))(())))()())(((()(())())()()))())())))))))((()())((()()(()))(()(())))()))()))(()))(()))()()(()(((())((((()))()(()))((())()(()(()())()(()))()())))))(()))()))())()())))())))(())))((())(()())))))()))(())(()))()())()(()()((()(()))))))()(())(()())(())()))(((())()))(()()(()()()))))(()(())))()))))())))))())(()()()()()()(((())))(()()))()((())(((((()()())))(()))(()))()()))(((())())()(((()()()()))))(()))(())())))()())(()()())())))))))()))))((())))()())(()))(()(()))())))))())(())))))()()())())()))()()(())))(()))(())((((((())(()))(()))())()))(()()(())))()))(()()))()))()(())))(())))((()(()))(())()()())())))(((()()())(())()))))))()(((()(((((()()(((())(())))())()((()))))((()())()(())(((())))(((()((()(()(()))(()()))())(()))(())(())))()))))))((((()))()((((()(()))()))()()))))()(()(()))()(()((()(((()(()()(((()))))()(((()(()(()(((()(()())())()()(()(()())())(()((((())(()))()))(((((()()())(())()((()()())))()()(((()()))()((((((((()(())))())((()))))(())))(()))))((()((((()()(())(((((()))(((((((((((((()())))((((()(((()((())())()))((()))()(()()((()()()()(()()(()(()(((())()(()((((((()((()()((())()((((()((()()(()()())((()()()((()((())()(()(((()((())((((())(()))((()(()))(()())()((((((((()(((((((((((()))(()(((()(()()()((((())((())()())()))(())((())(()))(((()((()(())))(()))))((()()))))((((()(()(()())(()(())((((((((()((((()((()(((((()))())()(()))(()()((()(())(((((()(())()(((((()()))))))()(((())()(()()((((())()((())((()(((())(((()))((()()((((()(())))))((()((((()((()((()(((())((()))(((((((()(((()((((((((())()))((((())(((((()((((((((()(((()((()(((()()(((()((((((()()(()((((((((()()(()(()(())((((()())()))))(((()))((((())((((()())((()(())()((()((((((()((((((()(())))()())(((())())())()(())()(()())((()()((((())((((((())(()(((((()((((())()((((()(()(())(()())(((())()((())((((()))()((((((())(()(((()(((()((((((()(((()))(()()())())((()((()())()((((())(((()(()(((((((((())(())))()((()()()()(())((()))(((((((()(((((((((()(()))))(()((((((((()((((()((()()((((((()()(((((((()(()(())()(())((()()()((()(((((()())()(((((()())()()((()(()())(()()()(((()()(((((()((((((()()((()(()()()((((((((((((()((((((((()()(((()())))()(((()()(())())((((()((((()((((()()()(())(())((()(()(((((((((((((((()(())(())))))()()))((()(((()(())((()(((()(()()((((()()(((()(((()(((((()()((()(()(((()))((((((()((((((((()((()((())(((((()(((())(())())((()()))((((())()()((()(((()(((((()()(((()))(((()(()(((((((((((((()))((((((((()(((()))))())((((((((((((())((())((()())(((())((())(()((((((((((()(((())((()()(()((())(((((((((((()))((((((((((((()(()())((()((()((()(()(((()((((((((()()(()((()(()(((()))((()))(((((((((((((()(())((((((())(((()(())(()(()(()((()()))((((()((((()((((())))())((((()((((()))((((((()((((((()((()(((())))((())(()))(()((()((((()((()(((()()))((((()()()(((((((())(((())(()))())((((()())(((()(((((((((((()(()(()((()(((((((((((((((()()((((()((((((((()(((()()((()((((()))(((()(())((((((()((((())()((((()((()))(())()(()(((()((())())((((((()(()(())())(((())(()(()())(((((()((()((())()())(())))(((()(())))))))(((()(((()))()((()(((()()((()())()()))())))(((()))(()(((()(((((((((()(()(((((()()(((()())()()))))()(((()))(((()(()(()(()(()))()(())()))(()(((())))(()))))))))))(())((()((())((()(())()(())((()()((((()()((()()))((())(((()((()(())(())))()(()(((((()((()))())()(((((()()(((()(()((((((())(()))(())()))((()(()()))(())())()))(((())))(()((()(((())(())())))((()()((((((((((((((()((()(()()(()(((()))())()()((()()()(())(()))(()())(((())((())()(())()()(()()(())))((()(((()))))(((()()(()()))())((()((())()))((((()()()())((())))(((()(())(((((()(((((()((()(()((((()()(((()()()(((()())(((()()((((())(()))(((()))(())())((()))(((()((()))(((()()((())((()(((((()((((()()())((()))()((((()((()(()()()(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Finds the floor where santa needs to deliver presents. Uses a for loop.
3+
*
4+
* Time complexity: O(n)
5+
* Space complecity: O(1)
6+
*
7+
* @param {string} dir Floor directions.
8+
* @returns {number} Floor number.
9+
*/
10+
const findFloor = (dir) => {
11+
let floor = 0
12+
13+
for (const char of dir) {
14+
if (char === '(') floor++
15+
else floor--
16+
}
17+
18+
return floor
19+
}
20+
21+
module.exports = {
22+
fun: findFloor,
23+
id: 'for-loop'
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Finds the floor where santa needs to deliver presents. Uses tail recursion.
3+
*
4+
* Time complexity: O(n)
5+
* Space complexity: O(n)
6+
*
7+
* @param {string} dir Floor directions.
8+
* @returns {number} Floor number.
9+
*/
10+
const findFloor = (dir) => {
11+
/**
12+
* Recursive helper function.
13+
* @param {number} idx Current index.
14+
* @param {number} sum Current sum.
15+
*/
16+
const recur = (idx, sum) => {
17+
if (idx >= dir.length) return sum
18+
if (dir[idx] === '(') return recur(idx + 1, sum + 1)
19+
else return recur(idx + 1, sum - 1)
20+
}
21+
22+
return recur(0, 0)
23+
}
24+
25+
module.exports = {
26+
fun: findFloor,
27+
id: 'tail-recursion'
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Finds the floor where santa needs to deliver presents. Uses recursion.
3+
*
4+
* Time complexity: O(n)
5+
* Space complexity: O(n)
6+
*
7+
* @param {string} dir Floor directions.
8+
* @returns {number} Floor number.
9+
*/
10+
const findFloor = (dir) => {
11+
if (dir.length === 0) return 0
12+
if (dir[0] === '(') return 1 + findFloor(dir.slice(1))
13+
else return -1 + findFloor(dir.slice(1))
14+
}
15+
16+
module.exports = {
17+
fun: findFloor,
18+
id: 'recursion'
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Finds the floor where santa needs to deliver presents. Uses the spread
3+
* operator, and the map, reduce methods.
4+
*
5+
* Time complexity: O(n)
6+
* Space complexity: O(n)
7+
*
8+
* @param {string} dir Floor directions.
9+
* @returns {number} Floor number.
10+
*/
11+
const findFloor = (dir) =>
12+
[...dir]
13+
.map((char) => char === '(' ? 1 : -1)
14+
.reduce((sum, num) => num + sum, 0)
15+
16+
module.exports = {
17+
fun: findFloor,
18+
id: 'spread-map-reduce'
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const nqlaForLoop = require('./nqla-for-loop')
2+
const nqlaReduce = require('./nqla-reduce')
3+
const nqlaRecursive = require('./nqla-recursive')
4+
const nqlaTail = require('./nqla-recursive-tail')
5+
6+
const solutions = [
7+
nqlaForLoop,
8+
nqlaReduce,
9+
nqlaRecursive,
10+
nqlaTail
11+
]
12+
13+
module.exports = solutions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { describe, it } = require('node:test')
2+
const assert = require('node:assert/strict')
3+
4+
const { readFileSync } = require('fs')
5+
const { join } = require('path')
6+
7+
const solutions = require('./nqla.repo')
8+
9+
const bigInputPath = join(__dirname, '../input-big.txt')
10+
const bigInput = readFileSync(bigInputPath, 'utf-8')
11+
12+
for (const { fun, id } of solutions) {
13+
describe(`Advent of Code 'Not Quite Lisp (part a)' solution '${id}'`, () => {
14+
it('Solves empty string', () => {
15+
assert.equal(fun(''), 0)
16+
})
17+
18+
it('Solves string of length 1', () => {
19+
assert.equal(fun('('), 1)
20+
assert.equal(fun(')'), -1)
21+
})
22+
23+
it('Solves string of length 4', () => {
24+
assert.equal(fun('(())'), 0)
25+
assert.equal(fun('()()'), 0)
26+
assert.equal(fun('((()'), 2)
27+
assert.equal(fun('))))'), -4)
28+
})
29+
30+
it('Solves big input', () => {
31+
assert.equal(fun(bigInput), 232)
32+
})
33+
})
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { readFileSync } = require('fs')
2+
const { join } = require('path')
3+
4+
const { timeAndReport } = require('../../../../../lib/time')
5+
const solutions = require('./nqla.repo')
6+
7+
const bigInputPath = join(__dirname, '../input-big.txt')
8+
const bigInput = readFileSync(bigInputPath, 'utf-8')
9+
10+
const args = [bigInput]
11+
const runs = 1_000
12+
const id = 'Advent of Code "Not Quite Lisp (part a)"'
13+
14+
timeAndReport(solutions, args, runs, id)

0 commit comments

Comments
 (0)