Skip to content

Commit 43d54ab

Browse files
committed
Working part one and two
1 parent c2d33e4 commit 43d54ab

File tree

5 files changed

+120
-7
lines changed

5 files changed

+120
-7
lines changed

2021/7/README.md

+63-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
# Answers
22

3-
| Part 1 | Part 2 |
4-
| ------ | ------- |
5-
| ` ` | ` ` |
3+
| Part 1 | Part 2 |
4+
| -------- | ---------- |
5+
| `337488` | `89647695` |
6+
7+
## --- Day 7: The Treachery of Whales ---
8+
9+
A giant [whale](https://en.wikipedia.org/wiki/Sperm_whale) has decided your submarine is its next meal, and it's much faster than you are. There's nowhere to run!
10+
11+
Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep for them otherwise) zooms in to rescue you! They seem to be preparing to blast a hole in the ocean floor; sensors indicate a _massive underground cave system_ just beyond where they're aiming!
12+
13+
The crab submarines all need to be aligned before they'll have enough power to blast a large enough hole for your submarine to get through. However, it doesn't look like they'll be aligned before the whale catches you! Maybe you can help?
14+
15+
There's one major catch - crab submarines can only move horizontally.
16+
17+
You quickly make a list of _the horizontal position of each crab_ (your puzzle input). Crab submarines have limited fuel, so you need to find a way to make all of their horizontal positions match while requiring them to spend as little fuel as possible.
18+
19+
For example, consider the following horizontal positions:
20+
21+
16,1,2,0,4,2,7,1,2,14
22+
23+
This means there's a crab with horizontal position `16`, a crab with horizontal position `1`, and so on.
24+
25+
Each change of 1 step in horizontal position of a single crab costs 1 fuel. You could choose any horizontal position to align them all on, but the one that costs the least fuel is horizontal position `2`:
26+
27+
* Move from `16` to `2`: `14` fuel
28+
* Move from `1` to `2`: `1` fuel
29+
* Move from `2` to `2`: `0` fuel
30+
* Move from `0` to `2`: `2` fuel
31+
* Move from `4` to `2`: `2` fuel
32+
* Move from `2` to `2`: `0` fuel
33+
* Move from `7` to `2`: `5` fuel
34+
* Move from `1` to `2`: `1` fuel
35+
* Move from `2` to `2`: `0` fuel
36+
* Move from `14` to `2`: `12` fuel
37+
38+
This costs a total of _`37`_ fuel. This is the cheapest possible outcome; more expensive outcomes include aligning at position `1` (`41` fuel), position `3` (`39` fuel), or position `10` (`71` fuel).
39+
40+
Determine the horizontal position that the crabs can align to using the least fuel possible. _How much fuel must they spend to align to that position?_
41+
42+
-----------------
43+
44+
## --- Part Two ---
45+
46+
The crabs don't seem interested in your proposed solution. Perhaps you misunderstand crab engineering?
47+
48+
As it turns out, crab submarine engines don't burn fuel at a constant rate. Instead, each change of 1 step in horizontal position costs 1 more unit of fuel than the last: the first step costs `1`, the second step costs `2`, the third step costs `3`, and so on.
49+
50+
As each crab moves, moving further becomes more expensive. This changes the best horizontal position to align them all on; in the example above, this becomes `5`:
51+
52+
* Move from `16` to `5`: `66` fuel
53+
* Move from `1` to `5`: `10` fuel
54+
* Move from `2` to `5`: `6` fuel
55+
* Move from `0` to `5`: `15` fuel
56+
* Move from `4` to `5`: `1` fuel
57+
* Move from `2` to `5`: `6` fuel
58+
* Move from `7` to `5`: `3` fuel
59+
* Move from `1` to `5`: `10` fuel
60+
* Move from `2` to `5`: `6` fuel
61+
* Move from `14` to `5`: `45` fuel
62+
63+
This costs a total of _`168`_ fuel. This is the new cheapest possible outcome; the old alignment position (`2`) now costs `206` fuel instead.
64+
65+
Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! _How much fuel must they spend to align to that position?_

2021/7/input.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ const input = fs
55
.readFileSync(path.join(__dirname, 'input.txt'), 'utf8')
66
.toString()
77
.trim()
8-
.split('\n')
9-
.map((line) => {
10-
return line;
11-
});
8+
.split(',')
9+
.map((num) => parseInt(num, 10));
1210

1311
module.exports = {
1412
input,

2021/7/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,76,50,481,307,723,1100,235,147,851,504,1068,780,490,126,771,831,358,711,68,281,645,644,862,705,372,217,679,1097,1005,728,1739,571,40,1293,782,378,802,245,1370,1512,9,372,448,64,36,1212,141,585,1468,278,286,476,239,285,664,277,43,665,1037,654,205,1238,36,41,276,945,624,47,91,1569,284,107,845,60,961,30,21,269,1091,304,364,339,169,430,176,156,1483,1510,111,146,447,590,1227,611,483,428,396,839,307,901,380,128,80,535,461,482,379,59,281,977,44,966,545,37,163,845,845,151,936,269,938,612,1284,863,516,927,511,825,125,599,101,394,1062,140,483,218,83,443,404,492,78,507,860,1741,43,128,60,566,181,5,554,937,163,280,655,184,480,408,127,935,613,766,46,312,770,697,275,13,84,600,168,83,281,599,607,1441,197,344,0,302,414,147,370,748,421,844,871,319,666,117,640,247,167,529,324,252,235,303,443,1305,796,109,20,364,532,1388,708,769,916,340,405,90,47,504,516,97,535,28,69,960,590,254,106,188,190,1388,698,246,264,98,229,1648,292,710,14,421,31,147,1493,552,1371,454,4,146,674,452,1267,1027,170,141,936,1341,884,558,276,631,68,39,2,464,1,839,318,881,413,2,452,352,34,89,323,884,1439,243,79,56,128,1273,1134,606,11,682,747,415,599,782,179,269,320,682,177,336,466,10,370,159,1636,367,888,573,171,682,60,9,59,332,10,1496,637,1029,413,186,1183,77,309,461,883,1079,699,233,69,259,108,1160,435,480,495,13,858,718,126,115,728,1008,133,442,7,598,1475,1156,226,162,415,3,151,72,527,792,494,763,144,64,490,273,1245,300,465,744,36,1465,251,8,494,1126,362,180,1263,175,141,1041,103,163,205,568,93,699,103,437,204,931,563,550,88,415,146,265,31,221,1123,835,375,1101,578,388,92,1417,845,308,343,499,158,293,242,4,509,574,254,1556,69,668,691,0,558,16,687,1210,166,748,400,863,66,600,771,1073,561,738,398,384,232,350,393,1113,1222,153,462,907,797,712,18,1463,1185,1055,994,57,130,265,131,52,463,902,453,38,132,783,1560,232,169,1162,173,311,5,1477,397,336,480,540,491,67,340,27,291,341,35,275,78,1525,387,218,63,79,533,4,569,1643,595,1508,851,39,1200,912,10,53,42,60,154,1174,155,275,137,677,367,1373,4,708,441,756,647,1054,872,1039,109,530,1179,939,429,567,866,1411,436,23,212,184,66,79,831,538,90,827,678,549,313,434,60,907,284,171,570,1091,603,447,122,1092,29,789,563,462,15,310,340,16,365,393,614,48,368,42,457,736,737,1008,513,61,764,366,400,525,1683,1177,909,908,112,734,16,79,917,541,127,107,79,1208,32,258,596,166,376,1313,735,1457,864,563,55,439,54,694,81,93,48,470,1028,689,1177,1331,155,412,847,250,405,387,8,456,18,619,533,729,1475,1182,935,210,55,355,958,15,32,598,85,175,471,1087,280,652,53,13,225,12,488,717,353,2,134,351,698,276,456,209,535,604,19,12,785,3,63,879,437,216,1,1275,811,786,417,33,51,733,1074,143,309,65,555,557,78,611,909,260,973,701,998,490,213,9,233,760,933,916,437,1369,1952,372,324,859,670,73,296,1391,127,407,230,52,16,547,803,883,258,308,710,343,1290,184,8,41,9,68,104,175,1034,1544,219,752,327,690,134,601,1574,214,385,1233,231,267,944,1533,349,431,97,632,278,1505,162,888,62,90,489,351,990,846,14,159,134,14,314,148,214,1153,513,114,6,49,10,14,957,219,16,204,954,863,50,482,90,696,99,253,252,433,57,385,54,343,106,154,78,1595,590,380,102,825,1933,191,1328,374,263,355,137,494,60,781,1113,391,274,325,326,14,965,269,15,32,742,81,393,730,892,982,103,890,499,58,816,292,29,480,173,831,132,1033,1511,1137,1511,22,1105,146,344,308,915,540,1371,1238,414,352,304,841,749,6,491,30,1322,415,293,1207,31,90,636,303,1551,354,23,275,18,32,623,1483,49,12,311,407,1551,296,252,647,778,1499,98,1220,264,1020,1440,377,1125,8,72,270,162,348,3,1023,965,719,62,1467,1176,663,439,557,654,85,1493,70,349,10,727,15,1173,387,529,608,1398,905,619,173,849,1493,49,88,4,708,1084,370,1007,285,4,530,770,561,26,669,1100,30,876,649,178,32,354,621,911,334,514,9,449,1019,107

2021/7/part-one.js

+30
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
11
const { input } = require('./input');
2+
3+
// Memoized
4+
const fuelCostFrom = (() => {
5+
const cache = new Map();
6+
7+
return (position) => {
8+
if (!cache.has(position)) {
9+
let distance = 0;
10+
for (let other_position of input) {
11+
distance += Math.abs(position - other_position);
12+
}
13+
cache.set(position, distance);
14+
}
15+
16+
return cache.get(position);
17+
};
18+
})();
19+
20+
let min_position = -1;
21+
let min_distance = Number.MAX_SAFE_INTEGER;
22+
for (let position of input) {
23+
let distance = fuelCostFrom(position);
24+
25+
if (distance < min_distance) {
26+
min_position = position;
27+
min_distance = distance;
28+
}
29+
}
30+
31+
console.log(min_distance, `(At position ${min_position})`);

2021/7/part-two.js

+24
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
const { input } = require('./input');
2+
3+
const fuelCostFrom = (from, to) => {
4+
const range = Math.abs(from - to);
5+
6+
// Thank you Gauss
7+
// @see https://en.wikipedia.org/wiki/Triangular_number
8+
return (range * (range + 1)) / 2;
9+
}
10+
11+
let min_position = -1;
12+
let min_distance = Number.MAX_SAFE_INTEGER;
13+
for (let position_a of input) {
14+
let distance = 0;
15+
for (let position_b of input) {
16+
distance += fuelCostFrom(position_a, position_b);
17+
}
18+
19+
if (distance < min_distance) {
20+
min_position = position_a;
21+
min_distance = distance;
22+
}
23+
}
24+
25+
console.log(min_distance, `(At position ${min_position})`);

0 commit comments

Comments
 (0)