From 4b29a4b04f643d8406859ab8624414d809100c60 Mon Sep 17 00:00:00 2001 From: Joseph Chamochumbi Date: Tue, 7 Dec 2021 07:07:14 +0100 Subject: [PATCH] feat: Day 7 2021, :star: :star: :fish: --- 2021/day-7/Cargo.toml | 9 ++++ 2021/day-7/src/main.rs | 107 +++++++++++++++++++++++++++++++++++++++++ 2021/deno/day-7.ts | 47 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 2021/day-7/Cargo.toml create mode 100644 2021/day-7/src/main.rs create mode 100644 2021/deno/day-7.ts diff --git a/2021/day-7/Cargo.toml b/2021/day-7/Cargo.toml new file mode 100644 index 0000000..37b2a4d --- /dev/null +++ b/2021/day-7/Cargo.toml @@ -0,0 +1,9 @@ +[package] +authors = ["Joseph Chamochumbi "] +edition = "2018" +name = "day-7" +version = "0.1.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +aoc = {git = "https://github.com/icyJoseph/advent-of-code.git"} diff --git a/2021/day-7/src/main.rs b/2021/day-7/src/main.rs new file mode 100644 index 0000000..bf98bc6 --- /dev/null +++ b/2021/day-7/src/main.rs @@ -0,0 +1,107 @@ +use aoc; + +fn solve(raw: String) -> () { + let positions = raw + .trim() + .split(",") + .map(|x| parse_num::(x)) + .collect::>(); + + let max_position = *positions.iter().max().unwrap(); + + let simple_fuel_calc = |target: u32| { + positions + .iter() + .map(|curr| { + if curr > &target { + curr - target + } else { + target - curr + } + }) + .sum() + }; + + let comp_fuel_calc = |target: u32| { + positions + .iter() + .map(|curr| { + let diff = if curr > &target { + curr - target + } else { + target - curr + }; + + diff * (diff + 1) / 2 + }) + .sum() + }; + + let calc_lowest_fuel = |aggregator: Box u32>| { + let mut lowest: Option = None; + + for pos in 0..=max_position { + match lowest { + Some(n) => { + let fuel = aggregator(pos); + if fuel < n { + lowest = Some(fuel); + } + } + _ => lowest = Some(aggregator(pos)), + } + } + + lowest + }; + + let part_one = calc_lowest_fuel(Box::new(simple_fuel_calc)); + + let part_two = calc_lowest_fuel(Box::new(comp_fuel_calc)); + + println!("Part one: {}", part_one.unwrap_or(0)); + + println!("Part two: {}", part_two.unwrap_or(0)); +} + +fn main() { + let input = aoc::get_input(2021, 7); + + // let input = std::fs::read_to_string("./input/day-7.in").expect("Error reading input"); + + solve(input); +} + +// Utilities +#[allow(dead_code)] +fn normal(x: usize, y: usize, width: usize) -> usize { + x + y * width +} + +#[allow(dead_code)] +fn rev_normal(norm: usize, width: usize) -> (usize, usize) { + (norm % width, norm / width) +} + +#[allow(dead_code)] +fn parse_num(str: &str) -> T { + match str.trim().parse::() { + Ok(n) => n, + _ => panic!("Error parsing"), + } +} +#[allow(dead_code)] +fn to_int(bin: &str) -> u32 { + match u32::from_str_radix(bin, 2) { + Ok(n) => n, + _ => panic!("Error parsing binary to integer"), + } +} + +#[allow(dead_code)] +fn string_vec(vec: &Vec, separator: &str) -> String { + vec.iter() + .map(|x| x.to_string()) + .collect::>() + .join(separator) +} diff --git a/2021/deno/day-7.ts b/2021/deno/day-7.ts new file mode 100644 index 0000000..003ee5b --- /dev/null +++ b/2021/deno/day-7.ts @@ -0,0 +1,47 @@ +const input = await Deno.readTextFile("./input/day-7.in"); + +const positions = input.split(",").map(Number); + +const sorted = positions.sort((a, b) => a - b); + +const length = sorted.length; + +const median = + length % 2 === 0 + ? Math.floor((sorted[length / 2] + sorted[length / 2 - 1]) / 2) + : sorted[Math.floor(length / 2)]; + +const total = sorted.reduce((prev, curr) => { + return prev + Math.abs(curr - median); +}, 0); + +/** + * Part One + */ +console.log("Part One:", total); + +/** + * Part Two + */ + +const maxPos = Math.max(...positions); + +let superTotal = Infinity; +let current = sorted[0]; + +while (current <= maxPos) { + const next = sorted.reduce((prev, x) => { + const diff = Math.abs(x - current); + const accumulated = (diff * (diff + 1)) / 2; + + return prev + accumulated; + }, 0); + + if (next < superTotal) { + superTotal = next; + } + + current += 1; +} + +console.log("Part Two:", superTotal);