Solve 2021/07
parent
27234dbb63
commit
64337c6479
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day07"
|
||||
version = "0.1.0"
|
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day07"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,89 @@
|
||||
https://adventofcode.com/2021/day/7
|
||||
|
||||
## \--- Day 7: The Treachery of Whales ---
|
||||
|
||||
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!
|
||||
|
||||
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!
|
||||
|
||||
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?
|
||||
|
||||
There's one major catch - crab submarines can only move horizontally.
|
||||
|
||||
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.
|
||||
|
||||
For example, consider the following horizontal positions:
|
||||
|
||||
[code]
|
||||
|
||||
16,1,2,0,4,2,7,1,2,14
|
||||
[/code]
|
||||
|
||||
This means there's a crab with horizontal position `16`, a crab with
|
||||
horizontal position `1`, and so on.
|
||||
|
||||
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`:
|
||||
|
||||
* Move from `16` to `2`: `14` fuel
|
||||
* Move from `1` to `2`: `1` fuel
|
||||
* Move from `2` to `2`: `0` fuel
|
||||
* Move from `0` to `2`: `2` fuel
|
||||
* Move from `4` to `2`: `2` fuel
|
||||
* Move from `2` to `2`: `0` fuel
|
||||
* Move from `7` to `2`: `5` fuel
|
||||
* Move from `1` to `2`: `1` fuel
|
||||
* Move from `2` to `2`: `0` fuel
|
||||
* Move from `14` to `2`: `12` fuel
|
||||
|
||||
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).
|
||||
|
||||
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?_
|
||||
|
||||
## \--- Part Two ---
|
||||
|
||||
The crabs don't seem interested in your proposed solution. Perhaps you
|
||||
misunderstand crab engineering?
|
||||
|
||||
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.
|
||||
|
||||
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`:
|
||||
|
||||
* Move from `16` to `5`: `66` fuel
|
||||
* Move from `1` to `5`: `10` fuel
|
||||
* Move from `2` to `5`: `6` fuel
|
||||
* Move from `0` to `5`: `15` fuel
|
||||
* Move from `4` to `5`: `1` fuel
|
||||
* Move from `2` to `5`: `6` fuel
|
||||
* Move from `7` to `5`: `3` fuel
|
||||
* Move from `1` to `5`: `10` fuel
|
||||
* Move from `2` to `5`: `6` fuel
|
||||
* Move from `14` to `5`: `45` fuel
|
||||
|
||||
This costs a total of `_168_` fuel. This is the new cheapest possible outcome;
|
||||
the old alignment position (`2`) now costs `206` fuel instead.
|
||||
|
||||
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?_
|
||||
|
@ -0,0 +1 @@
|
||||
16,1,2,0,4,2,7,1,2,14
|
@ -0,0 +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,478,1187,253,1892,900,155,20,787,17,248,1397,407,167,686,638,1020,960,124,840,220,1824,700,373,4,551,229,294,567,254,350,1144,679,124,361,145,483,335,202,1334,367,60,870,11,557,482,645,672,1296,1538,427,78,542,1135,13,65,0,140,705,13,642,187,1085,36,1118,349,601,382,584,941,26,949,200,763,198,430,204,1352,1135,210,342,11,1089,830,1523,9,523,167,762,254,805,8,132,29,102,1299,936,756,59,134,183,235,316,139,48,182,44,88,213,113,93,169,565,601,1899,1191,189,796,770,32,1183,365,374,867,918,1084,86,75,20,47,99,1140,2,99,1024,366,455,752,556,1220,66,326,450,213,1,342,756,49,675,160,280,68,221,193,379,88,179,94,16,109,570,1145,1207,824,355,1389,1601,168,86,236,923,120,759,14,478,460,84,167,1723,1005,269,6,171,861,311,832,952,701,3,1598,1466,96,780,57,161,631,572,276,105,594,276,17,405,688,1444,173,23,199,177,689,19,565,472,151,986,76,379,1430,212,928,106,25,143,84,833,942,860,1555,271,239,720,596,1209,235,535,361,1794,79,283,275,17,342,1687,1434,173,967,740,217,1370,18,1579,1259,546,94,623,475,834,1000,456,101,520,120,1023,360,167,213,617,42,1149,629,760,17,33,27,1347,414,646,1116,1340,134,259,143,407,249,328,968,677,241,438,98,313,27,791,1,634,3,918,1482,213,123,444,45,24,26,26,1203,64,67,1562,1,4,298,12,384,32,443,37,268,674,356,202,286,694,272,163,950,1022,54,59,21,73,519,462,106,76,1112,10,72,388,194,6,120,9,645,209,1121,75,599,362,661,439,69,62,339,390,23,1247,365,1266,4,246,511,47,467,134,276,497,130,458,427,669,1191,701,917,168,1191,294,641,236,801,375,106,872,800,87,356,583,1096,253,459,951,1331,719,66,1091,525,15,370,290,141,1201,30,43,37,76,1131,616,297,172,402,1016,654,301,63,872,303,69,1195,502,351,52,1659,86,104,294,807,166,120,190,333,60,283,819,198,184,144,278,343,1395,496,103,705,485,172,642,225,181,583,188,38,436,801,91,5,634,180,28,20,146,488,676,121,420,965,220,1564,1011,241,423,3,1631,709,106,725,164,1032,65,205,503,188,397,1072,49,121,761,721,249,418,87,126,258,712,500,435,157,127,681,108,270,647,504,505,83,407,212,165,1177,160,715,1292,491,195,141,25,829,1316,242,754,364,1707,33,594,434,488,368,298,183,1156,29,1674,537,378,8,9,860,240,571,749,471,331,501,156,62,427,1103,52,12,832,1198,284,388,827,556,194,288,218,397,84,1485,95,401,739,986,994,305,668,1324,1437,312,993,15,822,923,707,135,42,423,37,1183,1344,997,19,699,395,119,7,168,1711,50,151,38,20,163,686,1364,21,24,411,32,335,188,55,628,274,1766,439,180,286,1024,87,15,1498,290,561,971,32,294,67,113,219,42,18,715,3,664,242,583,221,1045,236,74,46,1612,639,325,164,100,69,518,38,502,26,329,112,1174,127,124,90,144,527,468,152,1098,800,125,349,191,290,191,27,651,446,267,9,1304,269,586,64,983,152,236,512,8,248,177,109,311,957,47,126,69,13,709,204,381,1151,580,340,994,865,258,190,9,1149,930,1128,321,100,471,0,507,1308,326,585,813,1088,76,174,333,387,631,186,430,988,24,820,11,45,173,167,1494,98,1467,456,167,21,1363,1173,394,318,1601,1111,1249,757,282,672,1227,1214,277,336,815,136,1192,681,689,431,130,1488,154,465,14,709,339,1123,68,151,1280,143,1797,23,250,1231,1007,302,1103,2,585,552,1732,994,225,771,1495,82,229,700,910,15,38,159,1122,316,1044,711,1436,920,1722,523,1398,188,443,1032,93,33,397,272,187,24,489,53,79,1277,671,1094,68,1705,984,1096,512,145,389,167,161,1174,94,4,534,1295,648,75,24,366,995,175,220,714,843,412,267,634,1209,66,1094,125,822,1114,1513,694,1520,30,676,817,245,26,77,1146,552,143,165,39,343,971,87,0,90,1434,588,616,99,297,1034,114,5,702,917,582,733,31,54,820,0,212,192,282,33,639,1661,460,75,680,115,178,194,271,274,582,1008,89,139,611,707,0,376,65,9,161,135,40,134,566,66,601,95,817,745,202,352,447,322,842,6,1247,175,468,330,608,368,139,21,29,486,121,9,1293,298,73,328,302,145,889,1794,677,56,952,520,80
|
@ -0,0 +1,74 @@
|
||||
use std::io::stdin;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() > 1 && args[1] == "part1" {
|
||||
part1();
|
||||
} else {
|
||||
part2();
|
||||
}
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
println!("Part1");
|
||||
let mut crabs = String::new();
|
||||
stdin().read_line(&mut crabs).ok();
|
||||
|
||||
let mut crabs: Vec<i32> = crabs
|
||||
.split(',')
|
||||
.map(|s| s.trim())
|
||||
.map(|s| s.parse().unwrap())
|
||||
.collect();
|
||||
|
||||
dbg!(&crabs);
|
||||
crabs.sort();
|
||||
dbg!(&crabs);
|
||||
|
||||
let median_idx = crabs.len() / 2;
|
||||
dbg!(median_idx);
|
||||
let median = crabs[median_idx];
|
||||
dbg!(median);
|
||||
// answer 311 too low
|
||||
//
|
||||
let fuel_consumption: i32 = crabs.iter().map(|pos| (median - pos).abs()).sum();
|
||||
dbg!(fuel_consumption);
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
println!("Part2");
|
||||
|
||||
let mut crabs = String::new();
|
||||
stdin().read_line(&mut crabs).ok();
|
||||
let crabs: Vec<i32> = crabs
|
||||
.split(',')
|
||||
.map(|s| s.trim())
|
||||
.map(|s| s.parse().unwrap())
|
||||
.collect();
|
||||
|
||||
let mut prev_consumption = std::i32::MAX;
|
||||
let mut x = 0;
|
||||
|
||||
loop {
|
||||
let consumption = crabs.iter().map(|&pos| p2_consumption(pos, x)).sum();
|
||||
if consumption < prev_consumption {
|
||||
prev_consumption = consumption
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
println!("Calculated for x={}: {}", x, consumption);
|
||||
x += 1;
|
||||
}
|
||||
dbg!(x);
|
||||
dbg!(prev_consumption);
|
||||
}
|
||||
|
||||
fn p2_consumption(crab: i32, to_position: i32) -> i32 {
|
||||
let steps_to_go = (to_position - crab).abs();
|
||||
let mut consumption = (steps_to_go / 2) * (steps_to_go + 1);
|
||||
if steps_to_go % 2 == 1 {
|
||||
consumption += (steps_to_go / 2) + 1
|
||||
}
|
||||
return consumption;
|
||||
}
|
Loading…
Reference in New Issue