commit 9eaf7517afb524ad6de863ca8789b8506f246662 Author: mitchellhansen Date: Wed Dec 2 11:43:17 2020 -0800 init diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..19c9ed6 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "advent-2020" +version = "0.1.0" +authors = ["mitchellhansen "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +reqwest = "0.9.22" +tempfile = "3.1.0" +futures = "0.3.1" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3332bc7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,22 @@ +extern crate reqwest; +extern crate tempfile; + +use crate::problem1::part1::Problem1; + +mod problem1; +mod util; + +pub trait Problem { + // Parses input and generates state + fn new(input: &String) -> Self; + + // Runs on state + fn run_part1(&self); + fn run_part2(&self); +} + +fn main() { + let problem1 = Problem1::new(&util::get_problem(1)); + problem1.run_part1(); + problem1.run_part2(); +} \ No newline at end of file diff --git a/src/problem1/.part1.rs.swp b/src/problem1/.part1.rs.swp new file mode 100644 index 0000000..b81565a Binary files /dev/null and b/src/problem1/.part1.rs.swp differ diff --git a/src/problem1/mod.rs b/src/problem1/mod.rs new file mode 100644 index 0000000..e44fa95 --- /dev/null +++ b/src/problem1/mod.rs @@ -0,0 +1 @@ +pub mod part1; \ No newline at end of file diff --git a/src/problem1/part1.rs b/src/problem1/part1.rs new file mode 100644 index 0000000..643c907 --- /dev/null +++ b/src/problem1/part1.rs @@ -0,0 +1,76 @@ +/* +Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together. +*/ + +use crate::Problem; + +pub struct Problem1 { + number_list: Vec, +} + +impl Problem1 {} + +impl Problem for Problem1 { + fn new(input: &String) -> Self { + Problem1 { + number_list: input + .split("\n") + .filter_map(|s| { + let s = s.trim(); + if !s.is_empty() { + Some(s.parse::().unwrap()) + } else { + None + } + }).collect(), + } + } + + fn run_part1(&self) { + let mut a_list = Vec::new(); + let mut b_list = Vec::new(); + + for i in &self.number_list { + if *i >= 2020 / 2 { + a_list.push(i); + } else { + b_list.push(i); + } + } + + for a in &a_list { + for b in &b_list { + if *a + *b == 2020 { + println!("Sum found, {} and {}", a, b) + } + } + } + } + + fn run_part2(&self) { + let mut a_list = Vec::new(); + let mut b_list = Vec::new(); + let mut c_list = Vec::new(); + + for i in &self.number_list { + a_list.push(i); + b_list.push(i); + c_list.push(i); + if *i >= 2020 / 2 { + } else if *i >= 2020 / 3 { + } else { + } + } + + for a in &a_list { + for b in &b_list { + for c in &c_list { + if *a + *b + *c == 2020 { + println!("Sum found, {}, {}, and {}", a, b, c) + } + } + } + } + } +} + diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..d771427 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,46 @@ +extern crate reqwest; +extern crate tempfile; + +use std::io::{copy, Write}; +use std::fs::File; +use tempfile::Builder; +use std::collections::HashMap; +use reqwest::header::HeaderValue; +use std::fs; +use std::path::Path; +use std::env; + +fn get_body(problem: i32) -> String { + + fs::create_dir_all("./cache").expect(""); + + let filepath = format!("./cache/problem-{}", problem); + let url = format!("https://adventofcode.com/2020/day/{}/input", problem); + + if Path::new(&filepath).exists() { + fs::read_to_string(filepath).unwrap() + } else { + + let session_token = match env::var("SESSION") { + Ok(token) => token, + Err(e) => panic!("No session token"), + }; + + let session_token = format!("session={};", session_token); + + let client = reqwest::Client::new(); + let body = client + .get(&url) + .header("Cookie", session_token) + .send().expect("").text().expect(""); + + let mut f = File::create(format!("./cache/problem-{}", problem)).expect(""); + f.write_all(body.as_bytes()); + + body + } +} + +pub fn get_problem(problem: i32) -> String { + get_body(problem) +}