Browse Source

First day

master
commit
e7b9c0740c
  1. 2
      .gitignore
  2. 105
      Cargo.lock
  3. 10
      Cargo.toml
  4. 100
      input/day1
  5. 28
      src/day1.rs
  6. 43
      src/main.rs

2
.gitignore

@ -0,0 +1,2 @@
/target
**/*.rs.bk

105
Cargo.lock

@ -0,0 +1,105 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "advent_of_code_2019"
version = "0.1.0"
dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "atty"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "clap"
version = "2.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-width"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
"checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90"
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

10
Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "advent_of_code_2019"
version = "0.1.0"
authors = ["Gaël Berthaud-Müller <blacksponge@tuta.io>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.33"

100
input/day1

@ -0,0 +1,100 @@
129561
125433
97919
93037
73254
96511
115676
95032
69369
145385
111145
64368
83462
95765
133284
136563
67439
69311
147720
92632
142940
100610
106538
80025
121672
125386
126601
67943
120022
95914
132721
105831
138493
57649
72843
81754
103116
148993
139042
145929
61039
126034
74187
60750
99048
131776
123137
113098
107571
117050
108649
117455
147443
121863
104952
103465
128718
61795
121049
112010
74403
56153
136161
76872
94156
131477
91769
90744
118647
135791
98914
104988
62070
82308
71964
91477
63733
84412
127000
65449
67976
51400
56045
82951
101119
143015
99388
51796
93467
63220
124459
136330
130535
144270
88616
63626
139954
92191
117618
110422

28
src/day1.rs

@ -0,0 +1,28 @@
use std::io::BufRead;
pub fn part1<F: BufRead> (input: F) {
let res: i32 = input.lines()
.filter_map(|line| line.unwrap().parse::<i32>().ok())
.map(|mass| mass / 3 - 2)
.sum();
println!("{}", res);
}
fn recursive_fuel_mass(mass: i32) -> i32 {
let fuel_mass = mass / 3 - 2;
if fuel_mass <= 0 {
0
} else {
fuel_mass + recursive_fuel_mass(fuel_mass)
}
}
pub fn part2<F: BufRead> (input: F) {
let res: i32 = input.lines()
.filter_map(|line| line.unwrap().parse::<i32>().ok())
.map(recursive_fuel_mass)
.sum();
println!("{}", res);
}

43
src/main.rs

@ -0,0 +1,43 @@
#[macro_use]
extern crate clap;
use clap::{Arg, App};
use std::fs::File;
use std::io::BufReader;
mod day1;
fn main() {
let matches = App::new("Advent of Code 2019")
.version("0.1")
.arg(Arg::with_name("challenge")
.short("c")
.long("challenge")
.value_name("CHALLENGE")
.required(true)
.takes_value(true)
.min_values(1)
.max_values(50))
.arg(Arg::with_name("input")
.short("i")
.long("input")
.value_name("INPUT")
.required(true)
.takes_value(true))
.get_matches();
let input = matches.value_of("input").unwrap();
let challenge = value_t!(matches, "challenge", usize).unwrap() - 1;
let input_file = File::open(input).expect("Unable to open input file");
let reader = BufReader::new(input_file);
let challenges = [
day1::part1, day1::part2
];
if let Some(f) = challenges.get(challenge) {
f(reader);
} else {
println!("Challenge {}, part {} not unimplemented", challenge / 2 + 1, challenge % 2 +1)
}
}
Loading…
Cancel
Save