Day 1.
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
Cargo.lock
|
||||||
|
target/
|
||||||
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["awick"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = "^2.33.0"
|
||||||
100
inputs/day1
Normal file
100
inputs/day1
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
129880
|
||||||
|
115705
|
||||||
|
118585
|
||||||
|
124631
|
||||||
|
81050
|
||||||
|
138183
|
||||||
|
61173
|
||||||
|
95354
|
||||||
|
130788
|
||||||
|
89082
|
||||||
|
75554
|
||||||
|
110104
|
||||||
|
140528
|
||||||
|
71783
|
||||||
|
125889
|
||||||
|
126602
|
||||||
|
73089
|
||||||
|
76822
|
||||||
|
51774
|
||||||
|
85940
|
||||||
|
81004
|
||||||
|
149584
|
||||||
|
145921
|
||||||
|
105570
|
||||||
|
142370
|
||||||
|
80823
|
||||||
|
147779
|
||||||
|
115651
|
||||||
|
70250
|
||||||
|
67763
|
||||||
|
128192
|
||||||
|
51298
|
||||||
|
134963
|
||||||
|
73510
|
||||||
|
90976
|
||||||
|
141216
|
||||||
|
65134
|
||||||
|
140468
|
||||||
|
143998
|
||||||
|
101711
|
||||||
|
88477
|
||||||
|
53335
|
||||||
|
138328
|
||||||
|
141186
|
||||||
|
149804
|
||||||
|
64950
|
||||||
|
53107
|
||||||
|
54648
|
||||||
|
97557
|
||||||
|
85927
|
||||||
|
125038
|
||||||
|
80514
|
||||||
|
64912
|
||||||
|
140591
|
||||||
|
114229
|
||||||
|
57089
|
||||||
|
123464
|
||||||
|
127572
|
||||||
|
137169
|
||||||
|
146550
|
||||||
|
51138
|
||||||
|
115504
|
||||||
|
128034
|
||||||
|
147244
|
||||||
|
108107
|
||||||
|
101205
|
||||||
|
51498
|
||||||
|
136829
|
||||||
|
140171
|
||||||
|
59441
|
||||||
|
144489
|
||||||
|
139384
|
||||||
|
145841
|
||||||
|
96771
|
||||||
|
116821
|
||||||
|
88599
|
||||||
|
126780
|
||||||
|
65012
|
||||||
|
67621
|
||||||
|
129699
|
||||||
|
149639
|
||||||
|
97590
|
||||||
|
147527
|
||||||
|
117462
|
||||||
|
146709
|
||||||
|
60527
|
||||||
|
107643
|
||||||
|
92956
|
||||||
|
72177
|
||||||
|
92285
|
||||||
|
62475
|
||||||
|
63099
|
||||||
|
66904
|
||||||
|
77268
|
||||||
|
62945
|
||||||
|
134364
|
||||||
|
106924
|
||||||
|
117842
|
||||||
|
130016
|
||||||
|
123712
|
||||||
69
src/main.rs
Normal file
69
src/main.rs
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
use clap::{App,Arg,SubCommand};
|
||||||
|
|
||||||
|
fn is_number(s: String) -> Result<(), String> {
|
||||||
|
match u64::from_str_radix(&s, 10) {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => Err(e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_base_fuel(mass: u64) -> u64 {
|
||||||
|
let div3 = mass / 3;
|
||||||
|
|
||||||
|
if div3 >= 2 {
|
||||||
|
div3 - 2
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_fuel(mass: u64) -> u64 {
|
||||||
|
let mut res = calculate_base_fuel(mass);
|
||||||
|
let mut last_round = res;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let new_round = calculate_base_fuel(last_round);
|
||||||
|
if new_round == 0 {
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
res += new_round;
|
||||||
|
last_round = new_round;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let matches = App::new("My Advent of Code Thing")
|
||||||
|
.version("1.0")
|
||||||
|
.author("Adam Wick <awick@uhsure.com>")
|
||||||
|
.about("Runs advent of code programs")
|
||||||
|
.subcommand(SubCommand::with_name("fuel")
|
||||||
|
.about("runs the fuel computation from day1")
|
||||||
|
.arg(Arg::with_name("NUM")
|
||||||
|
.help("The mass of the ship")
|
||||||
|
.multiple(true)
|
||||||
|
.validator(is_number)))
|
||||||
|
.get_matches();
|
||||||
|
|
||||||
|
if let Some(problem1) = matches.subcommand_matches("fuel") {
|
||||||
|
let mut total = 0;
|
||||||
|
|
||||||
|
match problem1.values_of("NUM") {
|
||||||
|
None =>
|
||||||
|
println!("ERROR: No values to compute fuel for!"),
|
||||||
|
Some(masses) => {
|
||||||
|
for mass_str in masses {
|
||||||
|
let mass = u64::from_str_radix(&mass_str, 10).unwrap();
|
||||||
|
let fuel = calculate_fuel(mass);
|
||||||
|
println!("Mass {}: {} fuel", mass, fuel);
|
||||||
|
total += fuel;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("TOTAL FUEL: {}", total);
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Failed to run a reasonable command.");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user