diff --git a/aoc2024/src/day01.rs b/aoc2024/src/day01.rs index c37246f..5b1cab5 100644 --- a/aoc2024/src/day01.rs +++ b/aoc2024/src/day01.rs @@ -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, Vec) { + input.lines() + .map(|line| line.split_once(" ") + .unwrap() + ) + .map(|(x, y)| (x.parse::().unwrap(), y.parse::().unwrap())) + .unzip() +} diff --git a/aoc2024/src/day02.rs b/aoc2024/src/day02.rs index fbe59f7..4190cd4 100644 --- a/aoc2024/src/day02.rs +++ b/aoc2024/src/day02.rs @@ -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> { + input.lines().map(|l| + l.split_whitespace().map(|x| x.parse().unwrap()).collect() + ).collect() +} + +fn line_is_safe(line: &&Vec) -> bool { + (line.is_sorted() || line.iter().rev().is_sorted()) + && line.windows(2) + .all(|w| (1..=3).contains(&w[0].abs_diff(w[1])) + ) +} diff --git a/src/main.rs b/src/main.rs index b1cf701..e65dc88 100644 --- a/src/main.rs +++ b/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; - 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) else { panic!("No Input Data"); }; @@ -39,14 +44,21 @@ fn fetch_data(year: usize, day: usize) -> Result<(), Box> 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::().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(()) }