2019 day 1 part 1

This commit is contained in:
Wesley Moore 2019-12-02 11:53:52 +11:00
parent dc00f93a03
commit 07453369bb
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE
6 changed files with 150 additions and 0 deletions

6
2019/Cargo.lock generated Normal file
View file

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "advent-of-code"
version = "0.1.0"

9
2019/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "advent-of-code"
version = "0.1.0"
authors = ["Wesley Moore <wes@wezm.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

100
2019/input/day1.txt Normal file
View file

@ -0,0 +1,100 @@
90903
135889
120859
137397
56532
92489
87979
149620
137436
62999
71672
61022
139765
69690
109513
67615
77803
146519
96432
129440
67912
143886
126992
129921
122339
61684
149706
52779
106421
145896
145977
76585
136021
63976
97063
114899
118570
102196
129126
98521
136186
68054
72452
92433
145851
98487
149980
114477
125479
62351
131559
115553
129371
147164
83125
146200
68621
55982
79131
64907
95570
132347
76889
96461
123627
128180
113508
70342
139386
131234
140377
119825
80791
52090
62615
101366
138512
113752
77912
64447
146047
94578
82228
116731
123488
103839
120854
54663
129242
51587
149536
71096
110104
145155
139278
78799
62410
64645
112849
60402

15
2019/src/bin/day1.rs Normal file
View file

@ -0,0 +1,15 @@
use std::io;
use advent_of_code::input;
fn main() -> io::Result<()> {
// Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
let result: i32 = input::read_number_list("input/day1.txt")?
.into_iter()
.map(|module_mass| (module_mass as f64 / 3.).floor() as i32 - 2)
.sum();
println!("Result: {}", result);
Ok(())
}

19
2019/src/input.rs Normal file
View file

@ -0,0 +1,19 @@
use std::fs::File;
use std::io;
use std::io::{BufRead, BufReader};
use std::path::Path;
pub fn read_number_list<P: AsRef<Path>>(path: P) -> io::Result<Vec<i32>> {
let input = BufReader::new(File::open(path)?);
let mut output = Vec::new();
for line in input.lines() {
let line = line?;
output.push(
line.parse()
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?,
)
}
Ok(output)
}

1
2019/src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod input;