Skip to content

Commit 43985c8

Browse files
feat(2021-day-02): move the submarine using aiming
1 parent 57638de commit 43985c8

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

2021/day-02/navigate.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,39 @@ const move = (location, direction, distance) => {
1616
return result
1717
}
1818

19-
const navigate = (location, route) => {
19+
const navigate = (location, route, useAiming = false) => {
2020
let position = location
2121
route.forEach(instruction => {
22-
position = move(position, instruction[0], instruction[1])
22+
if (useAiming) {
23+
position = aimedMove(position, instruction[0], instruction[1])
24+
} else {
25+
position = move(position, instruction[0], instruction[1])
26+
}
2327
})
2428
return position
2529
}
2630

31+
const aimedMove = (location, direction, distance) => {
32+
const result = location
33+
switch (direction) {
34+
case 'forward':
35+
result.x += distance
36+
result.d += (distance * location.a)
37+
break
38+
case 'up':
39+
result.a -= distance
40+
break
41+
case 'down':
42+
result.a += distance
43+
break
44+
default:
45+
throw new Error(`Direction ${direction} is unsupported`)
46+
}
47+
return result
48+
}
49+
2750
module.exports = {
2851
move,
29-
navigate
52+
navigate,
53+
aimedMove
3054
}

2021/day-02/navigate.test.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { move, navigate } = require('./navigate')
3+
const { move, navigate, aimedMove } = require('./navigate')
44

55
const TestRoute = [
66
['forward', 5],
@@ -47,4 +47,48 @@ describe('--- Day 2: Dive! ---', () => {
4747
})
4848
})
4949
})
50+
describe('Part 2', () => {
51+
describe('aimedMove()', () => {
52+
it('moves forward when the aim is 0', () => {
53+
expect(aimedMove({ x: 0, d: 0, a: 0 }, 'forward', 5)).to.deep.equal({
54+
x: 5,
55+
d: 0,
56+
a: 0
57+
})
58+
})
59+
it('aims down without moving', () => {
60+
expect(aimedMove({ x: 0, d: 0, a: 0 }, 'down', 5)).to.deep.equal({
61+
x: 0,
62+
d: 0,
63+
a: 5
64+
})
65+
})
66+
it('moves forward and down when aimed', () => {
67+
expect(aimedMove({ x: 0, d: 0, a: 5 }, 'forward', 8)).to.deep.equal({
68+
x: 8,
69+
d: 40,
70+
a: 5
71+
})
72+
})
73+
it('aims up without moving', () => {
74+
expect(aimedMove({ x: 0, d: 0, a: 5 }, 'up', 3)).to.deep.equal({
75+
x: 0,
76+
d: 0,
77+
a: 2
78+
})
79+
})
80+
it('throws an error for an unexpected direction', () => {
81+
expect(() => {
82+
aimedMove({ x: 0, d: 0, a: 5 }, 'garbage', 5)
83+
}).to.throw('Direction garbage is unsupported')
84+
})
85+
})
86+
describe('navigate()', () => {
87+
it('can navigate using aiming', () => {
88+
const destination = navigate({ x: 0, d: 0, a: 0 }, TestRoute, true)
89+
expect(destination.x).to.equal(15)
90+
expect(destination.d).to.equal(60)
91+
})
92+
})
93+
})
5094
})

2021/day-02/solution.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
2828

2929
const part2 = () => {
3030
const data = resetInput()
31-
console.debug(data)
32-
return 'No answer yet'
31+
const destination = navigate({ x: 0, d: 0, a: 0 }, data, true)
32+
return destination.x * destination.d
3333
}
3434
const answers = []
3535
answers.push(part1())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy