Skip to content

Commit 85bf20c

Browse files
committed
feat: solve advent of code 2016 day 1 part 1
1 parent 801e467 commit 85bf20c

File tree

8 files changed

+200
-0
lines changed

8 files changed

+200
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R3, L5, R1, R2, L5, R2, R3, L2, L5, R5, L4, L3, R5, L1, R3, R4, R1, L3, R3, L2, L5, L2, R4, R5, R5, L4, L3, L3, R4, R4, R5, L5, L3, R2, R2, L3, L4, L5, R1, R3, L3, R2, L3, R5, L194, L2, L5, R2, R1, R1, L1, L5, L4, R4, R2, R2, L4, L1, R2, R53, R3, L5, R72, R2, L5, R3, L4, R187, L4, L5, L2, R1, R3, R5, L4, L4, R2, R5, L5, L4, L3, R5, L2, R1, R1, R4, L1, R2, L3, R5, L4, R2, L3, R1, L4, R4, L1, L2, R3, L1, L1, R4, R3, L4, R2, R5, L2, L3, L3, L1, R3, R5, R2, R3, R1, R2, L1, L4, L5, L2, R4, R5, L2, R4, R4, L3, R2, R1, L4, R3, L3, L4, L3, L1, R3, L2, R2, L4, L4, L5, R3, R5, R3, L2, R5, L2, L1, L5, L1, R2, R4, L5, R2, L4, L5, L4, L5, L2, L5, L4, R5, R3, R2, R2, L3, R3, L2, L5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R3, L5, R1, R2, L5, R2, R3, L2, L5, R5, L4, L3, R5, L1, R3, R4, R1, L3, R3, L2, L5, L2, R4, R5, R5, L4, L3, L3, R4, R4, R5, L5, L3, R2, R2, L3, L4, L5, R1, R3, L3, R2, L3, R5, L194, L2, L5, R2, R1, R1, L1, L5, L4, R4, R2, R2, L4, L1, R2, R53, R3, L5, R72, R2, L5, R3, L4, R187, L4, L5, L2, R1, R3, R5, L4, L4, R2, R5, L5, L4, L3, R5, L2, R1, R1, R4, L1, R2, L3, R5, L4, R2, L3, R1, L4, R4, L1, L2, R3, L1, L1, R4, R3, L4, R2, R5, L2, L3, L3, L1, R3, R5, R2, R3, R1, R2, L1, L4, L5, L2, R4, R5, L2, R4, R4, L3, R2, R1, L4, R3, L3, L4, L3, L1, R3, L2, R2, L4, L4, L5, R3, R5, R3, L2, R5, L2, L1, L5, L1, R2, R4, L5, R2, L4, L5, L4, L5, L2, L5, L4, R5, R3, R2, R2, L3, R3, L2, L5, R3, L5, R1, R2, L5, R2, R3, L2, L5, R5, L4, L3, R5, L1, R3, R4, R1, L3, R3, L2, L5, L2, R4, R5, R5, L4, L3, L3, R4, R4, R5, L5, L3, R2, R2, L3, L4, L5, R1, R3, L3, R2, L3, R5, L194, L2, L5, R2, R1, R1, L1, L5, L4, R4, R2, R2, L4, L1, R2, R53, R3, L5, R72, R2, L5, R3, L4, R187, L4, L5, L2, R1, R3, R5, L4, L4, R2, R5, L5, L4, L3, R5, L2, R1, R1, R4, L1, R2, L3, R5, L4, R2, L3, R1, L4, R4, L1, L2, R3, L1, L1, R4, R3, L4, R2, R5, L2, L3, L3, L1, R3, R5, R2, R3, R1, R2, L1, L4, L5, L2, R4, R5, L2, R4, R4, L3, R2, R1, L4, R3, L3, L4, L3, L1, R3, L2, R2, L4, L4, L5, R3, R5, R3, L2, R5, L2, L1, L5, L1, R2, R4, L5, R2, L4, L5, L4, L5, L2, L5, L4, R5, R3, R2, R2, L3, R3, L2, L5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Computes the block distance of a destination based on input instructions.
3+
* Uses differences (deltas) to represent directions, and one 'if' chain to
4+
* change direction.
5+
*
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*
9+
* @param {string} instr Movement instructions.
10+
* @returns {number} Block distance from the destination.
11+
*/
12+
const blockDistance = (instr) => {
13+
const splitIns = instr ? instr.split(', ') : []
14+
let x = 0
15+
let y = 0
16+
let dx = 0
17+
let dy = 1
18+
19+
for (const ins of splitIns) {
20+
const turn = ins[0]
21+
const dist = parseInt(ins.slice(1))
22+
23+
// Update direction deltas based on turn.
24+
if (turn === 'L') { // 90° counterclockwise
25+
const temp = dx
26+
dx = -dy
27+
dy = temp
28+
} else { // 90° clockwise
29+
const temp = dx
30+
dx = dy
31+
dy = -temp
32+
}
33+
34+
// Update distance based on direction deltas.
35+
x += dist * dx
36+
y += dist * dy
37+
}
38+
39+
return Math.abs(x) + Math.abs(y)
40+
}
41+
42+
module.exports = {
43+
fun: blockDistance,
44+
id: 'distance-delta one-if'
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Computes the block distance of a destination based on input instructions.
3+
* Uses numbers to represent directions, modulo operator to change direction,
4+
* and two small 'if' chans to change direction and distance separately.
5+
*
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*
9+
* @param {string} instr Movement instructions.
10+
* @returns {number} Block distance from the destination.
11+
*/
12+
const blockDistance = (instr) => {
13+
const splitIns = instr ? instr.split(', ') : []
14+
let x = 0
15+
let y = 0
16+
let dir = 0 // 0: north, 1: east, 2: south, 3: west
17+
18+
for (const ins of splitIns) {
19+
const turn = ins[0]
20+
const dist = parseInt(ins.slice(1))
21+
22+
// Update direction based on turn.
23+
if (turn === 'L') dir = (dir + 3) % 4
24+
else dir = (dir + 1) % 4
25+
26+
// Update distance based on direction.
27+
if (dir === 0) y += dist
28+
else if (dir === 1) x += dist
29+
else if (dir === 2) y -= dist
30+
else x -= dist
31+
}
32+
33+
return Math.abs(x) + Math.abs(y)
34+
}
35+
36+
module.exports = {
37+
fun: blockDistance,
38+
id: 'distance-num two-if'
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Computes the block distance of a destination based on input instructions.
3+
* Uses strings to represent directions, and one big 'if' chain to change
4+
* distance and direction.
5+
*
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*
9+
* @param {string} instr Movement instructions.
10+
* @returns {number} Block distance from the destination.
11+
*/
12+
const blockDistance = (instr) => {
13+
const splitIns = instr ? instr.split(', ') : []
14+
let x = 0
15+
let y = 0
16+
let dir = 'N'
17+
18+
for (const ins of splitIns) {
19+
const turn = ins[0]
20+
const dist = parseInt(ins.slice(1))
21+
22+
if (dir === 'N') {
23+
if (turn === 'L') {
24+
x -= dist
25+
dir = 'W'
26+
} else {
27+
x += dist
28+
dir = 'E'
29+
}
30+
} else if (dir === 'S') {
31+
if (turn === 'L') {
32+
x += dist
33+
dir = 'E'
34+
} else {
35+
x -= dist
36+
dir = 'W'
37+
}
38+
} else if (dir === 'W') {
39+
if (turn === 'L') {
40+
y -= dist
41+
dir = 'S'
42+
} else {
43+
y += dist
44+
dir = 'N'
45+
}
46+
} else {
47+
if (turn === 'L') {
48+
y += dist
49+
dir = 'N'
50+
} else {
51+
y -= dist
52+
dir = 'S'
53+
}
54+
}
55+
}
56+
57+
return Math.abs(x) + Math.abs(y)
58+
}
59+
60+
module.exports = {
61+
fun: blockDistance,
62+
id: 'distance-str one-big-if'
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const ntftaStrOneBigIf = require('./ntfta-str-one-big-if')
2+
const ntftaNumTwoIf = require('./ntfta-num-two-if')
3+
const ntftaDeltaOneIf = require('./ntfta-delta-one-if')
4+
5+
const solutions = [
6+
ntftaStrOneBigIf,
7+
ntftaNumTwoIf,
8+
ntftaDeltaOneIf
9+
]
10+
11+
module.exports = solutions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { describe, it } = require('node:test')
2+
const assert = require('assert/strict')
3+
4+
const { readFileSync } = require('fs')
5+
const { join } = require('path')
6+
7+
const solutions = require('./ntfta.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 'No Time for a Taxicab' solution '${id}'`, () => {
14+
it('solves for empty directions', () => {
15+
assert.equal(fun(''), 0)
16+
})
17+
18+
it('solves for two directions', () => {
19+
assert.equal(fun('R2, L3'), 5)
20+
})
21+
22+
it('solves big-sized directions (solves the puzzle)', () => {
23+
assert.equal(fun(bigInput), 236)
24+
})
25+
})
26+
}
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('./ntfta.repo')
6+
7+
const hugeInputPath = join(__dirname, '../input-huge.txt')
8+
const hugeInput = readFileSync(hugeInputPath, 'utf-8')
9+
10+
const args = [hugeInput]
11+
const runs = 25_000
12+
const id = 'Advent of Code 2016 day 1 part 1 (huge input)'
13+
14+
timeAndReport(solutions, args, runs, id)

0 commit comments

Comments
 (0)