This commit is contained in:
Spectre 2024-12-03 00:56:35 +01:00
parent b2fc33499f
commit 6b868037d1
3 changed files with 91 additions and 12 deletions

View file

@ -4,10 +4,41 @@ pub struct Day01;
impl Solution for Day01 { impl Solution for Day01 {
fn part_1(&self, input: &str) -> Answer { 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 { 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()
}

View file

@ -4,10 +4,46 @@ pub struct Day02;
impl Solution for Day02 { impl Solution for Day02 {
fn part_1(&self, input: &str) -> Answer { 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 { 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]))
)
}

View file

@ -4,16 +4,21 @@ use solution::solution;
mod input_data; mod input_data;
use input_data::load_actual; use input_data::load_actual;
use reqwest::blocking::Client; use reqwest::{blocking::Client, Url};
use reqwest::header::COOKIE;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::sync::Arc;
use std::{time::Instant, env}; use std::{time::Instant, env};
fn main() { fn main() {
let year = 2024; let year = 2024;
let day = 2; let day = 1;
fetch_data(year, day).unwrap(); if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists() {
fetch_data(year, day).unwrap();
}
let Ok(data) = load_actual(year, day) let Ok(data) = load_actual(year, day)
else { panic!("No Input Data"); }; 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 url = format!("https://adventofcode.com/{year}/day/{day}/input");
let session_cookie = env::var("ADVENT_TOKEN")?; 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 let response = client
.get(url) .get(url)
.header(COOKIE, session_cookie)
.send()?; .send()?;
let body = response.text(); let body = response.text()?;
println!("Response: {:?}", body);
let path = format!("./data/{}/day{:02}", year, day);
let mut output = File::create(path)?;
output.write_all(body.as_bytes())?;
Ok(()) Ok(())
} }