day1+2
This commit is contained in:
parent
b2fc33499f
commit
6b868037d1
3 changed files with 91 additions and 12 deletions
|
|
@ -4,10 +4,41 @@ pub struct Day01;
|
|||
|
||||
impl Solution for Day01 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
Answer::Unimplemented
|
||||
let (mut first, mut second) = parse(input);
|
||||
|
||||
first.sort();
|
||||
second.sort();
|
||||
|
||||
let res: u32 = first.iter().zip(second)
|
||||
.map(|(a, b)| a.abs_diff(b))
|
||||
.sum();
|
||||
|
||||
Answer::Number(res as u64)
|
||||
}
|
||||
|
||||
fn part_2(&self, input: &str) -> Answer {
|
||||
Answer::Unimplemented
|
||||
let (mut first, mut second) = parse(input);
|
||||
|
||||
first.sort();
|
||||
second.sort();
|
||||
|
||||
let res: u32 = first.iter()
|
||||
.map(|a|
|
||||
a * second.iter()
|
||||
.filter(|&b| b==a)
|
||||
.count() as u32
|
||||
)
|
||||
.sum();
|
||||
|
||||
Answer::Number(res as u64)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> (Vec<u32>, Vec<u32>) {
|
||||
input.lines()
|
||||
.map(|line| line.split_once(" ")
|
||||
.unwrap()
|
||||
)
|
||||
.map(|(x, y)| (x.parse::<u32>().unwrap(), y.parse::<u32>().unwrap()))
|
||||
.unzip()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,46 @@ pub struct Day02;
|
|||
|
||||
impl Solution for Day02 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
Answer::Unimplemented
|
||||
let data = parse(input);
|
||||
|
||||
let res = data.iter()
|
||||
.filter(line_is_safe)
|
||||
.count();
|
||||
|
||||
Answer::Number(res as u64)
|
||||
}
|
||||
|
||||
fn part_2(&self, input: &str) -> Answer {
|
||||
Answer::Unimplemented
|
||||
let data = parse(input);
|
||||
|
||||
let res = data.iter()
|
||||
.filter(|line|
|
||||
(0..line.len()).any(|i| {
|
||||
let mut new_vec = vec![];
|
||||
for j in 0..line.len() {
|
||||
if i != j {
|
||||
new_vec.push(line[j])
|
||||
}
|
||||
}
|
||||
line_is_safe(&&new_vec)
|
||||
}
|
||||
)
|
||||
)
|
||||
.count();
|
||||
|
||||
Answer::Number(res as u64)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> Vec<Vec<u32>> {
|
||||
input.lines().map(|l|
|
||||
l.split_whitespace().map(|x| x.parse().unwrap()).collect()
|
||||
).collect()
|
||||
}
|
||||
|
||||
fn line_is_safe(line: &&Vec<u32>) -> bool {
|
||||
(line.is_sorted() || line.iter().rev().is_sorted())
|
||||
&& line.windows(2)
|
||||
.all(|w| (1..=3).contains(&w[0].abs_diff(w[1]))
|
||||
)
|
||||
}
|
||||
|
|
|
|||
26
src/main.rs
26
src/main.rs
|
|
@ -4,16 +4,21 @@ use solution::solution;
|
|||
mod input_data;
|
||||
use input_data::load_actual;
|
||||
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::header::COOKIE;
|
||||
use reqwest::{blocking::Client, Url};
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use std::{time::Instant, env};
|
||||
|
||||
fn main() {
|
||||
let year = 2024;
|
||||
let day = 2;
|
||||
let day = 1;
|
||||
|
||||
if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists() {
|
||||
fetch_data(year, day).unwrap();
|
||||
}
|
||||
|
||||
let Ok(data) = load_actual(year, day)
|
||||
else { panic!("No Input Data"); };
|
||||
|
|
@ -39,14 +44,21 @@ fn fetch_data(year: usize, day: usize) -> Result<(), Box<dyn std::error::Error>>
|
|||
let url = format!("https://adventofcode.com/{year}/day/{day}/input");
|
||||
let session_cookie = env::var("ADVENT_TOKEN")?;
|
||||
|
||||
let client = Client::new();
|
||||
let jar = reqwest::cookie::Jar::default();
|
||||
jar.add_cookie_str(&format!("session={session_cookie}; Domain=.adventofcode.com"), &"https://adventofcode.com".parse::<Url>().unwrap());
|
||||
|
||||
let client = Client::builder().cookie_provider(Arc::new(jar)).build().unwrap();
|
||||
let response = client
|
||||
.get(url)
|
||||
.header(COOKIE, session_cookie)
|
||||
.send()?;
|
||||
|
||||
let body = response.text();
|
||||
println!("Response: {:?}", body);
|
||||
let body = response.text()?;
|
||||
|
||||
let path = format!("./data/{}/day{:02}", year, day);
|
||||
|
||||
let mut output = File::create(path)?;
|
||||
|
||||
output.write_all(body.as_bytes())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue