Skip to content

Commit 12cd4fd

Browse files
committedNov 21, 2018
feature: Squares With Three Sides
1 parent 60dda58 commit 12cd4fd

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
88

99
- [Day 1: No Time for a Taxicab](day-01-no-time-for-a-taxicab/)
1010
- [Day 2: Bathroom Security](day-02-bathroom-security/)
11+
- [Day 3: Squares With Three Sides](day-03-squares-with-three-sides/)
1112

1213
## Running Tests
1314

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Day 3: Squares With Three Sides
2+
3+
Now that you can think clearly, you move deeper into the labyrinth of hallways and office furniture that makes up this part of Easter Bunny HQ. This must be a graphic design department; the walls are covered in specifications for triangles.
4+
5+
Or are they?
6+
7+
The design document gives the side lengths of each triangle it describes, but... `5 10 25`? Some of these aren't triangles. You can't help but mark the impossible ones.
8+
9+
In a valid triangle, the sum of any two sides must be larger than the remaining side. For example, the "triangle" given above is impossible, because `5 + 10` is not larger than `25`.
10+
11+
In your puzzle input, **how many** of the listed triangles are **possible**?
12+
13+
## References
14+
- https://adventofcode.com/2016/day/3
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const assert = require('assert');
2+
3+
const { isPossibleTriangle, countValidTriangles } = require('./triangle');
4+
5+
describe('Day 3: Squares With Three Sides', () => {
6+
it('should determine if triangle is possible', () => {
7+
assert.strictEqual(isPossibleTriangle(5, 10, 25), false);
8+
assert.strictEqual(isPossibleTriangle(3, 4, 6), true);
9+
});
10+
11+
it('should count valid triangles', () => {
12+
const list =
13+
`5 10 25
14+
3 4 6`;
15+
16+
assert.strictEqual(countValidTriangles(list), 1);
17+
});
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const isPossibleTriangle = (a, b, c) => a + b > c;
2+
3+
const countValidTriangles = (input) => {
4+
return input
5+
.split('\n')
6+
.map((row) => {
7+
const sides = row
8+
.trim()
9+
.split(' ')
10+
.filter((x) => x.length)
11+
.map((x) => parseInt(x))
12+
.sort((a, b) => a - b);
13+
14+
return {
15+
a: sides[0],
16+
b: sides[1],
17+
c: sides[2],
18+
};
19+
})
20+
.map(({ a, b, c }) => isPossibleTriangle(a, b, c))
21+
.filter((t) => t)
22+
.length;
23+
};
24+
25+
module.exports = {
26+
isPossibleTriangle,
27+
countValidTriangles,
28+
};

0 commit comments

Comments
 (0)
Please sign in to comment.