From 34bffc49705fb31f947ad0f2f45494dc9472e81b Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sun, 1 Dec 2019 09:24:11 +0200 Subject: Day 1 --- Cargo.lock | 6 ++++ inputs/day_1.txt | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.org | 12 +++++++ src/bin/day_1.rs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 11 +++++- 5 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 Cargo.lock create mode 100644 inputs/day_1.txt create mode 100644 readme.org create mode 100644 src/bin/day_1.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3719353 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "advent-of-code-2019" +version = "0.1.0" + diff --git a/inputs/day_1.txt b/inputs/day_1.txt new file mode 100644 index 0000000..5c720ed --- /dev/null +++ b/inputs/day_1.txt @@ -0,0 +1,100 @@ +66690 +86239 +75191 +140364 +95979 +106923 +95229 +123571 +84764 +89444 +98107 +89062 +109369 +146067 +124760 +76900 +139198 +111441 +74046 +84920 +54397 +143807 +121654 +93863 +73909 +104121 +58485 +119084 +126227 +142078 +79820 +132617 +108430 +98032 +107434 +127307 +105619 +57741 +53468 +63301 +137970 +136780 +80897 +133205 +79159 +89124 +94477 +56714 +143704 +122097 +117335 +108246 +75507 +101459 +101162 +146197 +121884 +66217 +57074 +142903 +140951 +64883 +124556 +67382 +142407 +121778 +57933 +94599 +87426 +143758 +64043 +65678 +90137 +61090 +77315 +102383 +146607 +139290 +85394 +149787 +125611 +106405 +91561 +135739 +54845 +68782 +111175 +61011 +125658 +70751 +85607 +75458 +75419 +124311 +66022 +122784 +129018 +54901 +73788 +108240 diff --git a/readme.org b/readme.org new file mode 100644 index 0000000..c1923ac --- /dev/null +++ b/readme.org @@ -0,0 +1,12 @@ +* Advent of Code 2019 + +** Personal challenge + +Try to keep the solution pure. Only main can do IO things, like return +different results when it's called differently. The rest of the +program should only be pure expressions. + +** Optimizations + +- Limit the use of statements. Try to use expressions instead, or move + the statement out to a function. diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs new file mode 100644 index 0000000..7c8f104 --- /dev/null +++ b/src/bin/day_1.rs @@ -0,0 +1,97 @@ +use std::io; +use std::io::prelude::*; +use std::iter; +use std::iter::Sum; +use std::num::ParseIntError; +use std::ops::Add; +use std::str::FromStr; + +fn main() { + let stdin = io::stdin(); + + let input = stdin + .lock() + .lines() + .map(|l| l.unwrap().parse::().unwrap()); + + dbg!(fuel_required(input)); +} + +// TODO: If this were a nice CLI program, it would probably have a switch to choose between these two rather than doing both? +fn fuel_required(it: impl Iterator) -> (Fuel, Fuel) { + it.map(|m| { + ( + m.fuel_excluding_fuel_weight(), + m.fuel_including_fuel_weight(), + ) + }) + .fold((Fuel(0), Fuel(0)), |(sum_x, sum_y), (x, y)| { + (sum_x + x, sum_y + y) + }) +} + +struct Module { + weight: Weight, +} + +impl FromStr for Module { + type Err = ParseIntError; + + fn from_str(s: &str) -> Result { + Ok(Module { + weight: Weight(s.parse()?), + }) + } +} + +impl Module { + fn fuel_excluding_fuel_weight(&self) -> Fuel { + self.weight.required_fuel() + } + + fn fuel_including_fuel_weight(&self) -> Fuel { + iter::successors(Some(self.weight.required_fuel()), |fuel| { + if fuel.weight().is_zero() { + None + } else { + Some(fuel.weight().required_fuel()) + } + }) + .sum() + } +} + +struct Weight(u32); + +impl Weight { + fn is_zero(&self) -> bool { + self.0 == 0 + } + + fn required_fuel(&self) -> Fuel { + Fuel((self.0 / 3).saturating_sub(2)) + } +} + +#[derive(Debug)] +struct Fuel(u32); + +impl Fuel { + fn weight(&self) -> Weight { + Weight(self.0) + } +} + +impl Add for Fuel { + type Output = Self; + + fn add(self, other: Self) -> Self { + Fuel(self.0 + other.0) + } +} + +impl Sum for Fuel { + fn sum>(iter: I) -> Self { + iter.fold(Fuel(0), Add::add) + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a9..0c24a73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,12 @@ +use std::io; +use std::io::prelude::*; + fn main() { - println!("Hello, world!"); + let stdin = io::stdin(); + let answer = string_length_sum(stdin.lock().lines().map(|l| l.unwrap())); + dbg!(answer); +} + +fn string_length_sum(it: impl Iterator) -> usize { + it.map(|l| l.len()).sum() } -- cgit v1.2.3