2021: day07: part 1

This commit is contained in:
Antoine Martin 2021-12-07 10:31:07 +01:00
parent 56a5379a1b
commit fa9827852d
5 changed files with 53 additions and 0 deletions

1
aoc2021/input/day07.txt Normal file
View file

@ -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,848,1174,380,1195,277,353,425,292,225,1521,631,76,692,329,1530,445,1625,561,161,1760,88,129,89,866,133,353,372,1456,888,115,347,291,246,8,57,17,869,1230,1224,681,586,1553,183,508,760,1200,812,634,578,126,147,684,1037,822,217,521,136,438,456,26,258,9,395,1,1398,34,13,40,493,871,154,606,716,649,491,416,293,1,107,21,317,1156,194,179,693,1591,894,845,10,18,1314,11,730,408,365,0,516,192,96,1438,1521,787,503,454,84,478,19,26,109,287,657,216,148,155,816,384,288,1121,39,263,488,348,110,1219,321,1101,51,5,95,147,806,17,207,81,139,686,23,231,1154,36,410,1025,2,327,172,307,232,420,88,129,1507,1028,852,467,46,81,16,64,989,558,919,1301,612,278,306,1500,95,507,1034,272,526,68,41,870,37,78,434,459,268,375,1427,1039,1039,1077,1068,445,482,106,1823,218,76,33,131,934,112,72,404,118,848,99,10,816,527,125,279,200,10,248,243,416,242,639,401,66,110,364,243,550,1289,55,30,1246,72,46,251,1225,264,765,137,94,626,252,18,481,250,55,10,284,247,90,277,34,176,118,1178,554,761,3,938,140,890,109,811,760,179,1084,998,16,338,550,120,190,220,74,846,212,30,1098,455,1110,1662,71,1275,609,47,1239,1050,93,169,383,564,323,134,310,208,685,255,219,369,556,272,1604,562,600,493,178,337,33,949,775,588,288,498,40,1056,483,796,8,1588,463,122,120,26,215,786,525,129,259,346,21,35,247,1564,1094,1013,85,54,193,889,590,908,122,158,363,646,1301,533,73,767,405,89,358,936,737,78,982,163,867,373,1460,492,151,560,371,647,110,1779,802,374,147,1279,252,299,244,568,1014,685,912,942,767,3,76,207,296,40,181,312,192,684,988,490,31,410,928,47,1345,943,1013,705,675,543,446,84,869,1173,393,348,99,25,21,20,132,295,4,1273,800,182,140,593,964,55,167,15,219,269,35,848,324,474,189,4,305,62,7,1145,13,975,55,880,43,368,82,1083,6,969,844,1388,185,818,114,641,151,1006,220,599,143,170,36,631,891,531,871,13,77,1261,340,911,159,258,213,1245,222,200,946,875,301,9,381,69,1152,901,490,207,38,104,103,423,311,1532,536,314,73,314,29,844,141,1191,169,136,42,143,163,277,22,1655,1063,45,774,103,83,1253,121,1473,834,245,143,211,1252,1368,97,4,1188,1102,359,134,298,225,292,101,832,426,1204,652,468,110,529,34,384,663,534,80,1,68,117,419,1321,1729,38,1609,416,554,1047,130,145,267,1382,0,237,32,17,237,803,21,888,185,108,79,1,192,286,1167,86,71,464,122,375,0,186,927,525,1127,442,675,373,53,404,624,65,1157,107,687,98,165,891,1186,513,727,46,148,275,516,1412,17,684,113,246,137,1246,1357,143,307,307,656,1030,198,33,955,1765,314,79,5,385,248,402,75,12,76,325,175,1198,258,38,912,795,296,1931,111,72,1619,1563,8,327,867,293,515,1647,1266,128,44,9,431,300,72,501,1181,147,1330,1446,490,297,436,274,512,107,185,57,112,2,121,472,505,860,1650,411,23,328,119,64,0,1684,249,106,167,55,225,501,36,81,1225,270,186,10,477,783,39,319,1151,264,788,42,521,282,257,721,442,250,642,255,63,1015,241,939,86,997,569,330,189,129,547,1424,446,285,8,288,599,372,56,274,245,3,41,943,353,67,294,32,656,879,918,94,314,767,957,1554,854,610,590,403,139,377,129,930,341,678,577,375,809,490,39,153,3,48,133,54,225,1261,677,267,30,28,115,642,1733,12,297,428,155,812,635,16,634,223,823,261,662,652,71,138,103,220,544,211,313,1298,512,559,1243,1319,427,78,765,343,212,557,275,1,551,234,697,1815,1005,275,179,595,1,1658,204,345,794,36,102,912,657,194,8,2,526,350,399,390,1166,469,324,1497,371,759,621,800,895,235,314,579,863,278,54,239,1275,79,1728,934,1145,1008,748,196,1056,339,569,629,47,1437,416,758,1000,287,592,1057,921,170,273,144,133,0,320,216,216,118,229,4,681,410,1801,34,485,769,839,47,950,1694,1222,1199,411,598,1210,7,1644,245,44,428,49,653,7,1037,310,754,32,476,253,124,809,66,539,434,1142,136,4,344,306,628,1332,15,1523,149,191,498,1480,498,385,562,457,199,201,487,608,172,20,584,7,17,342,167,93,60,589,445,926

View file

@ -0,0 +1 @@
16,1,2,0,4,2,7,1,2,14

48
aoc2021/src/day07.rs Normal file
View file

@ -0,0 +1,48 @@
use std::fmt::Write;
use anyhow::{Context, Result};
const INPUT: &str = include_str!("../input/day07.txt");
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<u64> {
let mut horizontal_positions = input
.trim()
.split(',')
.map(|n| n.parse::<u64>().context("couldn't parse position"))
.collect::<Result<Vec<_>>>()?;
// TODO: try linear selection algorithm
horizontal_positions.sort_unstable();
let median = horizontal_positions[horizontal_positions.len() / 2];
Ok(horizontal_positions
.iter()
// TODO: use abs_diff when stabilized
.map(|n| n.max(&median) - n.min(&median))
.sum())
}
#[cfg(test)]
mod tests {
use super::*;
const PROVIDED: &str = include_str!("../input/day07_provided.txt");
#[test]
fn part1_provided() {
assert_eq!(part1(PROVIDED).unwrap(), 37);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 340056);
}
}

View file

@ -6,3 +6,4 @@ pub mod day03;
pub mod day04; pub mod day04;
pub mod day05; pub mod day05;
pub mod day06; pub mod day06;
pub mod day07;

View file

@ -8,6 +8,7 @@ use aoc2021::day03;
use aoc2021::day04; use aoc2021::day04;
use aoc2021::day05; use aoc2021::day05;
use aoc2021::day06; use aoc2021::day06;
use aoc2021::day07;
fn main() -> Result<()> { fn main() -> Result<()> {
let days: &[DayFunc] = &[ let days: &[DayFunc] = &[
@ -17,6 +18,7 @@ fn main() -> Result<()> {
day04::run, day04::run,
day05::run, day05::run,
day06::run, day06::run,
day07::run,
]; ];
aoc::run(days) aoc::run(days)