commit
9eaf7517af
@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "advent-2020"
|
||||
version = "0.1.0"
|
||||
authors = ["mitchellhansen <mitchellhansen0@gmail.com>"]
|
||||
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"
|
@ -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();
|
||||
}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
pub mod part1;
|
@ -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<i32>,
|
||||
}
|
||||
|
||||
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::<i32>().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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
Loading…
Reference in new issue