From 9eaf7517afb524ad6de863ca8789b8506f246662 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 2 Dec 2020 11:43:17 -0800 Subject: [PATCH] init --- Cargo.toml | 12 ++++++ src/main.rs | 22 +++++++++++ src/problem1/.part1.rs.swp | Bin 0 -> 12288 bytes src/problem1/mod.rs | 1 + src/problem1/part1.rs | 76 +++++++++++++++++++++++++++++++++++++ src/util.rs | 46 ++++++++++++++++++++++ 6 files changed, 157 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/problem1/.part1.rs.swp create mode 100644 src/problem1/mod.rs create mode 100644 src/problem1/part1.rs create mode 100644 src/util.rs 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 0000000000000000000000000000000000000000..b81565a4ccb8234d326f59c792494ab88836d781 GIT binary patch literal 12288 zcmeI2&x;&I6vr#c!5?u|^rVO6?Fg}ZGut!Mdr2nQy+i^YHi(NkDPvD}b*9|@WmWYI z%Wm{0#GC(s;=xk_f)^3Ic=IX>Lhvez2aS00Ao#8B>Fvq7Mpy8#Pz9f@?y7pPUVZCT z!LU86|f3e1*`&Afn%$HE>5tw5&g;e3a-rS69?+~tF5d8RspMkRlq7> z6|f3e1*`&A0jq#jz$#!BIED%Y5o2FI%-Fq05Ip|>-~9gn;|az-1lPbO@IeoF;92kt zI0b%soUtFl58zvH2iyj?z#Bk-=fQVNjC~5;0efH<2oQn*ybfLfSHKx?8vOhiV_$)f z!7VTbD_{xSJI&Z#@HzMd+yS@2dte5pU=u8XQ{W`H`zT}IfUm)4-~;eJD8Xgmg8}#* zTKoon1xA-&fGt)5tAJI&Dqt0`3Rne>tpYb0q11oPQz1pBlWf`Pyqt&IToZq@47A8I zic6l5P_$0FH#^veUWUYdW+IULH$>8Nq)tW4p<7?=|0lBOgw zQap*hG*D`k2&FxtxpZ9GZ6`|*SX%04nttsg&PbNo$b2&DJLim(eWNzIc7a#_`+^WR z1&ZP|pL(?B^<6Jc^4tW8R)N-4^8c504gPjr?Fwr#u_LY*=1Ias?YLdmv@X_vwP|{< zF)MlD7s29Rx-54A;$Vgdvt2;5&Y3=@fX3r)f;p7~_)};;V8naQKAf`=j&VUQ#{2qULhz`dUEDi>I9h5ZRNmFJ28 literal 0 HcmV?d00001 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) +}