From 35160c9e759bb7ab5d478074f417305012c779be Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 1 Dec 2018 12:05:29 +0100 Subject: Start 2018! --- 2018/.gitignore | 1 + 2018/Cargo.lock | 14 + 2018/Cargo.toml | 7 + 2018/input/1.txt | 986 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2018/src/day1.rs | 25 ++ 2018/src/main.rs | 78 +++++ 6 files changed, 1111 insertions(+) create mode 100644 2018/.gitignore create mode 100644 2018/Cargo.lock create mode 100644 2018/Cargo.toml create mode 100644 2018/input/1.txt create mode 100644 2018/src/day1.rs create mode 100644 2018/src/main.rs diff --git a/2018/.gitignore b/2018/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/2018/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/2018/Cargo.lock b/2018/Cargo.lock new file mode 100644 index 0000000..5bf0575 --- /dev/null +++ b/2018/Cargo.lock @@ -0,0 +1,14 @@ +[[package]] +name = "aoc-2018" +version = "0.1.0" +dependencies = [ + "argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "argparse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3f8ebf5827e4ac4fd5946560e6a99776ea73b596d80898f357007317a7141e47" diff --git a/2018/Cargo.toml b/2018/Cargo.toml new file mode 100644 index 0000000..8c8eda7 --- /dev/null +++ b/2018/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "aoc-2018" +version = "0.1.0" +authors = ["tomsmeding "] + +[dependencies] +argparse = "0.2.2" diff --git a/2018/input/1.txt b/2018/input/1.txt new file mode 100644 index 0000000..2c568af --- /dev/null +++ b/2018/input/1.txt @@ -0,0 +1,986 @@ ++13 +-12 +-14 ++19 +-13 ++16 ++12 ++19 ++8 +-10 +-9 +-2 ++1 +-13 ++10 +-7 +-11 +-10 ++1 +-14 +-15 ++16 ++6 ++17 +-19 +-13 ++5 +-19 +-12 +-10 +-12 ++13 ++14 ++3 +-13 ++3 ++17 ++10 +-16 ++19 ++1 ++11 +-16 ++3 +-16 +-12 +-7 +-11 +-6 +-19 ++6 ++12 ++15 +-7 +-18 ++13 +-4 +-14 +-7 +-18 ++17 ++9 +-12 +-3 +-13 +-16 ++14 +-4 +-12 +-10 +-3 ++8 +-10 +-4 +-10 +-11 +-14 ++11 +-7 ++17 ++19 ++2 +-13 ++17 +-10 +-6 ++15 ++7 ++15 +-3 +-7 +-6 +-1 +-13 +-5 +-6 +-6 +-12 +-18 +-7 +-19 +-5 +-15 +-7 ++4 +-18 ++5 ++17 +-2 +-12 +-1 ++3 +-11 ++10 +-13 ++10 ++17 ++15 ++16 ++16 +-7 +-17 +-11 ++10 +-15 +-13 ++2 +-6 ++3 ++9 ++2 ++20 +-14 ++22 ++6 ++14 +-3 +-15 +-7 +-16 +-2 +-20 ++3 +-21 +-13 +-14 ++3 ++17 +-18 +-14 ++13 +-11 ++14 ++18 +-16 +-17 +-5 +-3 ++10 ++2 +-14 ++18 +-3 +-8 +-14 +-10 +-5 ++11 +-15 +-3 ++1 +-17 ++7 ++17 +-15 +-17 ++19 +-15 ++12 +-10 +-16 +-13 +-5 +-6 ++4 +-3 ++11 +-15 +-16 ++3 ++14 ++9 ++6 ++14 +-5 ++1 ++16 +-3 ++9 +-14 ++6 +-17 +-12 +-13 +-2 +-4 +-19 +-9 ++17 +-18 ++9 +-5 +-14 +-14 ++18 ++19 +-16 +-13 +-3 +-1 ++6 ++6 +-10 +-3 +-4 ++21 ++10 ++6 ++4 ++23 ++17 +-13 ++15 +-3 ++13 ++4 ++11 ++17 ++8 +-1 ++10 +-15 +-8 ++1 ++14 ++13 +-7 +-11 ++9 ++17 +-11 ++7 ++18 ++17 ++3 ++5 +-13 ++4 +-13 +-4 +-22 ++14 +-24 ++3 ++26 ++19 ++20 ++4 +-12 ++11 ++2 +-12 +-6 +-20 ++9 ++2 ++10 +-4 +-10 +-20 +-5 +-5 ++8 +-7 +-27 +-16 ++6 +-12 +-16 ++6 +-20 +-14 +-8 +-12 +-18 +-19 ++3 +-16 ++3 ++1 +-16 ++11 +-1 +-4 +-16 ++4 +-6 +-7 +-12 +-9 ++15 +-3 +-4 +-10 ++15 ++10 +-7 ++9 ++6 +-4 ++2 ++20 +-14 ++18 ++11 +-6 ++1 ++1 ++1 +-13 +-7 ++3 +-12 ++11 +-17 +-9 +-12 +-1 +-8 +-16 +-1 +-14 +-6 ++18 ++16 +-4 +-15 ++9 +-10 +-18 ++11 ++2 +-10 +-1 ++10 ++20 ++7 ++13 +-1 ++14 ++18 ++16 +-7 ++22 ++11 ++13 ++6 +-9 +-8 ++14 +-20 ++18 +-15 +-16 +-1 ++12 ++12 ++12 +-20 ++38 +-10 ++17 +-4 ++21 ++12 +-19 +-12 ++4 ++20 ++16 ++14 ++12 ++16 +-9 +-17 +-13 +-7 +-16 +-24 +-9 ++2 ++1 ++7 +-13 +-48 +-15 +-3 ++6 +-21 +-10 ++8 +-20 +-16 +-4 ++13 ++15 +-11 ++19 +-43 ++10 +-34 +-15 +-17 ++16 ++10 ++1 +-3 +-9 +-19 ++1 +-18 ++9 +-14 +-13 +-17 ++19 ++1 +-12 +-18 +-11 ++2 +-18 ++8 ++1 ++6 ++7 +-17 ++5 ++23 +-8 +-18 ++17 ++5 ++1 ++10 ++10 ++9 ++12 +-18 +-12 +-15 +-3 +-11 +-18 ++12 +-16 +-21 ++6 ++6 ++4 +-7 +-21 ++14 +-5 ++4 +-7 +-17 +-17 ++14 +-2 ++15 +-7 +-14 +-7 +-7 +-2 +-6 ++10 +-9 +-20 ++15 ++17 ++8 ++16 +-2 +-4 ++10 ++8 ++3 +-27 ++14 +-20 ++4 +-24 +-14 +-2 ++22 ++21 +-17 +-21 ++6 +-8 +-25 +-12 ++19 +-14 +-4 +-9 +-7 +-40 +-30 ++37 ++51 +-84 +-56 +-14 ++35 ++12 +-123 +-18 ++14 +-71 +-35 +-18 ++1 +-11 ++15 +-13 +-12 +-19 +-16 ++14 ++6 +-17 +-11 +-18 ++92 +-24 ++10 +-22 ++16 +-22 ++3 +-21 ++2 ++30 ++31 ++8 +-2 ++1 ++19 +-23 ++7 +-112 +-15 +-16 ++10 +-15 +-49 ++43 ++7 ++250 +-39 +-80 ++1324 ++76538 ++17 +-3 ++4 ++76 ++35 ++12 +-33 ++52 ++37 +-33 ++26 ++60 +-5 ++12 ++25 +-18 ++17 ++9 ++19 +-7 ++3 +-4 +-8 ++11 +-9 ++20 ++18 ++6 +-15 ++2 ++17 ++4 ++5 +-7 +-14 +-10 +-2 +-7 ++6 ++22 +-18 ++15 +-10 ++5 +-3 +-18 ++24 ++1 ++18 ++14 ++21 +-11 +-20 ++6 +-16 ++9 +-11 +-29 +-21 +-17 ++13 ++14 +-9 ++45 ++61 ++9 ++5 +-11 ++13 ++17 ++3 ++17 +-6 +-8 +-19 ++2 +-16 +-1 +-1 +-17 ++9 ++15 +-22 +-4 ++5 ++17 +-9 ++19 ++21 +-6 ++5 ++17 +-4 +-7 ++8 +-23 +-18 +-18 ++10 ++19 ++9 +-17 +-18 +-25 ++4 ++8 ++6 +-30 ++67 +-17 ++38 ++23 ++1 +-10 ++14 ++6 ++9 ++10 +-18 ++4 ++12 ++14 ++2 ++3 ++20 +-15 ++16 ++4 +-19 ++6 ++11 ++15 ++17 ++9 ++16 +-12 +-6 ++16 ++18 ++5 ++5 +-1 +-12 ++2 ++15 ++14 ++5 +-3 ++12 ++4 ++12 +-6 ++8 ++17 ++9 +-5 ++14 +-7 ++4 ++6 ++6 ++8 ++11 +-7 ++1 +-17 +-13 ++6 ++12 +-9 ++15 +-1 ++9 +-16 +-2 ++8 ++17 +-2 ++9 ++15 +-7 ++11 +-5 +-10 ++16 +-11 +-13 ++14 ++12 ++17 ++10 +-11 +-17 +-18 ++10 ++13 ++10 ++15 +-6 ++18 +-13 +-13 ++9 +-11 ++18 ++1 ++1 +-14 ++7 ++13 ++7 ++15 ++5 ++11 ++17 ++11 +-13 +-9 +-8 +-10 +-5 ++2 ++19 ++12 +-11 ++9 +-11 ++15 +-5 ++14 ++18 ++7 ++21 ++9 ++14 +-7 ++9 +-7 +-21 +-16 +-22 +-12 ++19 +-2 ++12 ++14 ++15 ++23 ++2 ++6 +-9 +-16 ++27 +-17 ++10 ++19 +-15 ++8 ++15 +-2 +-4 ++8 ++17 ++1 ++12 +-4 +-3 +-12 +-31 +-1 +-13 +-4 +-15 ++5 ++20 ++10 +-5 +-22 +-9 +-22 +-15 +-6 ++23 +-30 +-17 +-10 +-20 +-2 +-4 +-13 ++12 +-3 ++5 +-9 +-11 ++1 +-5 +-19 +-17 ++7 ++19 ++5 ++12 +-15 ++14 ++2 +-17 +-13 ++2 +-15 ++14 +-20 +-24 ++8 ++10 +-20 +-12 +-17 +-5 +-2 +-18 +-14 +-18 ++2 ++19 ++12 +-18 ++4 ++19 ++15 +-2 +-7 +-8 +-12 ++16 +-15 +-13 +-7 +-19 ++2 +-14 ++11 +-15 +-17 ++18 +-2 +-8 +-20 +-4 ++5 ++16 ++4 ++14 ++19 +-2 +-13 ++23 +-9 ++13 ++4 +-7 +-13 +-4 +-8 ++4 +-8 ++17 +-2 +-18 +-12 +-12 ++11 +-3 +-11 ++4 ++5 +-23 ++20 ++6 +-13 +-5 +-16 +-7 ++6 ++10 +-15 ++7 ++6 +-12 ++17 +-9 +-23 ++6 ++11 +-6 +-9 ++20 +-19 ++2 +-1 ++21 ++5 +-18 ++23 ++17 ++8 ++3 ++15 ++3 +-76719 diff --git a/2018/src/day1.rs b/2018/src/day1.rs new file mode 100644 index 0000000..741f5b3 --- /dev/null +++ b/2018/src/day1.rs @@ -0,0 +1,25 @@ +use std::io; +use std::io::BufRead; +use std::collections::HashSet; + +pub fn main(reader: T) -> io::Result<()> { + let values: Vec = + reader.lines().map(|l| l.unwrap().parse::().unwrap()).collect(); + + println!("{}", values.iter().sum::()); + + let mut seen = HashSet::new(); + seen.insert(0); + + let mut res = 0; + for val in values.iter().cycle() { + res += val; + if seen.contains(&res) { + println!("{}", res); + return Ok(()); + } else { + seen.insert(res); + } + } + unreachable!() +} diff --git a/2018/src/main.rs b/2018/src/main.rs new file mode 100644 index 0000000..7e683ca --- /dev/null +++ b/2018/src/main.rs @@ -0,0 +1,78 @@ +extern crate argparse; + +use std::io; +use std::io::{BufRead, BufReader, Error, ErrorKind}; +use std::fs::File; +use std::process::exit; +use argparse::{ArgumentParser, StoreTrue, Store}; + +mod day1; + +fn day_switch(day: i32, reader: T) -> io::Result<()> { + match day { + 1 => day1::main(reader), + _ => Err(Error::new(ErrorKind::Other, "Invalid day")) + } +} + +struct Options { + use_stdin: bool, +} + +fn run_day(day: i32, opts: &Options) -> io::Result<()> { + if opts.use_stdin { + let stdin = io::stdin(); + day_switch(day, BufReader::new(stdin)) + } else { + match File::open(format!("../input/{}.txt", day)) { + Ok(f) => day_switch(day, BufReader::new(f)), + Err(_) => Err(Error::new(ErrorKind::Other, format!("No input file for day {}", day))) + } + } +} + +fn error_handler(func: F) -> io::Result<()> + where F: Fn() -> io::Result<()> { + + match func() { + Ok(()) => Ok(()), + Err(err) => { + if err.kind() == ErrorKind::Other { + println!("Error: {}", err); + exit(1) + } else { + Err(err) + } + } + } +} + +fn main() -> io::Result<()> { + let mut day_string = String::new(); + let mut options = Options { + use_stdin: false + }; + + { + let mut parser = ArgumentParser::new(); + parser.set_description("AOC 2018 solutions by Tom Smeding"); + parser.refer(&mut options.use_stdin) + .add_option(&["-s", "--stdin"], StoreTrue, + "Read from stdin"); + parser.refer(&mut day_string) + .add_argument("day", Store, "Day to execute"); + parser.parse_args_or_exit(); + } + + error_handler(|| + match day_string.parse::() { + Ok(day) => run_day(day, &options), + Err(_) => { + for day in 1..1 { + run_day(day, &options)?; + } + Ok(()) + } + } + ) +} -- cgit v1.2.3-54-g00ecf