Skip to content

Commit 8a0b041

Browse files
feat(2020-day-12): give the ferry directions
1 parent a136ce7 commit 8a0b041

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

2020/day-12/ferry.js

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,77 @@
1-
const move = () => {
2-
return
1+
const parseCommand = (command) => {
2+
return {
3+
cmd: command.slice(0, 1),
4+
unit: Number(command.slice(1))
5+
}
36
}
47

5-
const route = () => {
6-
return
8+
const move = ({
9+
position = {
10+
x: 0,
11+
y: 0,
12+
d: 90 // Default to facing east
13+
},
14+
command
15+
}) => {
16+
// Movement subfunctions
17+
const mv = {
18+
N: (u) => { // North
19+
position.y += u
20+
},
21+
S: (u) => { // South
22+
position.y -= u
23+
},
24+
E: (u) => { // East
25+
position.x += u
26+
},
27+
W: (u) => { // West
28+
position.x -= u
29+
},
30+
L: (u) => { // Turn Left
31+
position.d -= u
32+
position.d = position.d % 360
33+
},
34+
R: (u) => { // Turn Right
35+
position.d += u
36+
position.d = position.d % 360
37+
},
38+
F: (u) => { // Forward
39+
// TODO: replace with vector positioning of arbitrary angles
40+
switch (position.d) {
41+
case 0 :
42+
mv.N(u)
43+
break
44+
case 90 :
45+
mv.E(u)
46+
break
47+
case 180 :
48+
mv.S(u)
49+
break
50+
case 270 :
51+
mv.W(u)
52+
break
53+
default :
54+
console.debug('Position', position)
55+
console.debug('Forward', u)
56+
throw new Error('Non-cardinal compass direction')
57+
}
58+
}
59+
}
60+
61+
console.debug('Received', command, position)
62+
const operation = parseCommand(command)
63+
mv[operation.cmd](operation.unit)
64+
return position
65+
}
66+
67+
const route = ({ instructions }) => {
68+
return instructions.reduce(
69+
(position, command) => {
70+
console.debug('Routing position', position)
71+
console.debug('Routing command', command)
72+
return move({ position, command })
73+
}, { x: 0, y: 0, d: 90 }
74+
)
775
}
876

977
module.exports = {

2020/day-12/ferry.test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env mocha */
22
const { expect } = require('chai')
3-
const { distance } = require('../../2018/day-06')
3+
const { distance } = require('../../2018/day-06/coordinates')
44
const { move, route } = require('./ferry')
55

66
describe('--- Day 11: Seating System ---', () => {
@@ -51,11 +51,29 @@ describe('--- Day 11: Seating System ---', () => {
5151
move({ position: origin, command })
5252
).to.deep.equal({ x: 0, y: 0, d: 180 })
5353
})
54-
it('can move forward', () => {
54+
it('can move forward in the direction it is pointing', () => {
5555
const command = 'F10'
5656
expect(
5757
move({ position: origin, command })
5858
).to.deep.equal({ x: 10, y: 0, d: 90 })
59+
expect(
60+
move({ position: { x: 0, y: 0, d: 0 }, command })
61+
).to.deep.equal({ x: 0, y: 10, d: 0 })
62+
expect(
63+
move({ position: { x: 0, y: 0, d: 270 }, command })
64+
).to.deep.equal({ x: -10, y: 0, d: 270 })
65+
expect(
66+
move({ position: { x: 0, y: 0, d: 180 }, command })
67+
).to.deep.equal({ x: 0, y: -10, d: 180 })
68+
})
69+
it('cannot handle non-cardoma; directions', () => {
70+
expect(() => {
71+
move({ position: { x: 0, y: 0, d: 13 }, command: 'F10' })
72+
}).to.throw('Non-cardinal compass direction')
73+
})
74+
it('defaults to 0,0 facing East', () => {
75+
expect(move({ command: 'F10' }))
76+
.to.deep.equal({ x: 10, y: 0, d: 90 })
5977
})
6078
})
6179
describe('route()', () => {
@@ -70,7 +88,8 @@ describe('--- Day 11: Seating System ---', () => {
7088
const result = route({ instructions })
7189
expect(result)
7290
.to.deep.equal({ x: 17, y: -8, d: 180 })
73-
expect(distance(result.x, result.y)).to.equal(25)
91+
// manhattan distance from origin
92+
expect(distance({ x: 0, y: 0 }, result)).to.equal(25)
7493
})
7594
})
7695
})

0 commit comments

Comments
 (0)