Compare commits
4 commits
c54571a447
...
83f51b4452
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
83f51b4452 | ||
|
|
2c9790a690 | ||
|
|
19fe0a53ff | ||
|
|
8d8fe53574 |
9 changed files with 97 additions and 58 deletions
6
.envrc
6
.envrc
|
|
@ -1 +1,7 @@
|
||||||
use flake
|
use flake
|
||||||
|
|
||||||
|
if [[ $(date +'%m') -eq 12 ]]
|
||||||
|
then
|
||||||
|
export ADVENT_YEAR=$(date +'%Y')
|
||||||
|
export ADVENT_DAY=$(date +'%d')
|
||||||
|
fi
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
# My Advent of Code solutions, written as an exercise to learn Rust.
|
# My Advent of Code solutions, written as an exercise to learn Rust.
|
||||||
|
|
||||||
To run, simply edit ´src/main.rs´ and run it. Optionally, set the year and day via ´ADVENT_YEAR´ and ´ADVENT_DAY´ environment variables. By setting ´ADVENT_TOKEN´ to your browser session token, your personal input can optionally be fetched automatically.
|
To run, simply edit `src/main.rs` and run it. Optionally, set the year and day via `ADVENT_YEAR` and `ADVENT_DAY` environment variables. By setting `ADVENT_TOKEN` to your browser session token, your personal input can optionally be fetched automatically.
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ impl Solution for Day01 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sum += 10 * first.unwrap() + last.unwrap()
|
sum += 10 * first.expect(word) + last.expect(word)
|
||||||
}
|
}
|
||||||
|
|
||||||
Answer::from(sum)
|
Answer::from(sum)
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ impl Solution for Day01 {
|
||||||
fn parse(input: &str) -> (Vec<u32>, Vec<u32>) {
|
fn parse(input: &str) -> (Vec<u32>, Vec<u32>) {
|
||||||
input.lines()
|
input.lines()
|
||||||
.map(|line| line.split_once(" ")
|
.map(|line| line.split_once(" ")
|
||||||
.unwrap()
|
.expect(line)
|
||||||
)
|
)
|
||||||
.map(|(x, y)| (x.parse::<u32>().unwrap(), y.parse::<u32>().unwrap()))
|
.map(|(x, y)| (x.parse::<u32>().unwrap(), y.parse::<u32>().unwrap()))
|
||||||
.unzip()
|
.unzip()
|
||||||
|
|
|
||||||
|
|
@ -1 +1,14 @@
|
||||||
## The author of adventofcode.com, Eric Wastl, does not want puzzle input to be shared: https://adventofcode.com/about
|
## The author of adventofcode.com, Eric Wastl, does not want puzzle input to be shared: https://adventofcode.com/about
|
||||||
|
|
||||||
|
Either manually place it here, with the folder structure
|
||||||
|
|
||||||
|
```
|
||||||
|
data/
|
||||||
|
2024/
|
||||||
|
day01
|
||||||
|
day02
|
||||||
|
day03
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
or set ADVENT_TOKEN to your session cookie to automatically fetch your input at runtime.
|
||||||
|
|
|
||||||
29
src/fetch_input.rs
Normal file
29
src/fetch_input.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
use reqwest::{blocking::Client, Url};
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
pub fn fetch_input(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 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)
|
||||||
|
.send()?;
|
||||||
|
|
||||||
|
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(())
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,8 @@ pub fn load_actual(year: usize, day: usize) -> io::Result<String> {
|
||||||
fs::read_to_string(path)
|
fs::read_to_string(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_test(year: usize, day: usize) -> io::Result<String> {
|
pub fn load_test(year: usize, day: usize) -> (io::Result<String>, io::Result<String>) {
|
||||||
let path = format!("./examples/{year}/day{:02}", day);
|
let input_path = format!("./examples/{year}/day{:02}", day);
|
||||||
fs::read_to_string(path)
|
let result_path = format!("./examples/{year}/day{:02}_solution", day);
|
||||||
|
(fs::read_to_string(input_path), fs::read_to_string(result_path))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
86
src/main.rs
86
src/main.rs
|
|
@ -4,61 +4,51 @@ use solution::solution;
|
||||||
mod input_data;
|
mod input_data;
|
||||||
use input_data::load_actual;
|
use input_data::load_actual;
|
||||||
|
|
||||||
use reqwest::{blocking::Client, Url};
|
mod fetch_input;
|
||||||
|
use fetch_input::fetch_input;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::{path::Path, time::Instant, env};
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::{time::Instant, env};
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let year = 2024;
|
let year = env::var("ADVENT_YEAR")
|
||||||
let day = 1;
|
.unwrap_or("2024".into())
|
||||||
|
.parse()
|
||||||
|
.unwrap();
|
||||||
|
let day = env::var("ADVENT_DAY")
|
||||||
|
.unwrap_or("1".into())
|
||||||
|
.parse()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists() {
|
if ! Path::new(&format!("./data/{}/day{:02}", year, day)).exists()
|
||||||
fetch_data(year, day).unwrap();
|
&& env::var("ADVENT_TOKEN").is_ok() {
|
||||||
|
fetch_input(year, day).expect("Set ADVENT_TOKEN to the correct session cookie to fetch input automatically");
|
||||||
}
|
}
|
||||||
|
|
||||||
let Ok(data) = load_actual(year, day)
|
run_all();
|
||||||
else { panic!("No Input Data"); };
|
}
|
||||||
|
|
||||||
|
fn run_all() {
|
||||||
|
let years_to_run = [2023, 2024];
|
||||||
|
|
||||||
|
if env::var("ADVENT_TOKEN").is_ok() {
|
||||||
|
for year in years_to_run {
|
||||||
|
for day in 1..=25 {
|
||||||
|
println!("Pre-fetching input for {year}/{:02}", day);
|
||||||
|
fetch_input(year, day).expect("Set ADVENT_TOKEN to the correct session cookie to fetch input automatically");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for year in years_to_run {
|
||||||
|
for day in 1..=25 {
|
||||||
|
for part in [1, 2] {
|
||||||
|
let input = load_actual(year, day).unwrap_or_else(|_| panic!("Missing input for {}/{:02}", year, day));
|
||||||
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
|
let result = solution(&input, year, day, part).unwrap_or_else(|| panic!("No solution at {}/{:02}", year, day));
|
||||||
let part1 = solution(&data, year, day, 1).unwrap();
|
let time_passed = now.elapsed().as_millis();
|
||||||
|
println!("Running Year {}, Day {:02}, Part {} - Result {} - took {}ms", year, day, part, result, time_passed);
|
||||||
let elapsed1 = now.elapsed();
|
}
|
||||||
|
}
|
||||||
let now = Instant::now();
|
|
||||||
|
|
||||||
let part2 = solution(&data, year, day, 2).unwrap();
|
|
||||||
|
|
||||||
let elapsed2 = now.elapsed();
|
|
||||||
|
|
||||||
println!("Part 1 result is {}, took {}ms", part1, elapsed1.as_millis());
|
|
||||||
println!("Part 2 result is {}, took {}ms", part2, elapsed2.as_millis());
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 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)
|
|
||||||
.send()?;
|
|
||||||
|
|
||||||
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(())
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use shared::Answer;
|
||||||
|
|
||||||
pub fn solution(input: &str, year: usize, day: usize, part: usize) -> Option<Answer> {
|
pub fn solution(input: &str, year: usize, day: usize, part: usize) -> Option<Answer> {
|
||||||
match year {
|
match year {
|
||||||
2023 => aoc2024::solution(input, day, part),
|
2023 => aoc2023::solution(input, day, part),
|
||||||
2024 => aoc2024::solution(input, day, part),
|
2024 => aoc2024::solution(input, day, part),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue