wiwi/
aoc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::prelude::*;
use crate::chain::GenericChainConversion as _;
use std::io::Read;

#[inline]
pub fn get_input(year: usize, day: usize) -> String {
	String::from_utf8(get_input_buf(year, day)).unwrap()
}

#[inline]
pub fn get_input_buf(year: usize, day: usize) -> Vec<u8> {
	let path = env::current_dir()
		.expect("failed to get current dir")
		.into_generic_chain()
		.with_inner(|p| p.push("input"))
		.with_inner(|p| p.push(&*format!("{year:04}-{day:02}")))
		.into_inner();

	let file = fs::OpenOptions::new()
		.read(true)
		.open(&*path)
		.unwrap_or_else(|e| panic!("failed to read input file (tried `{path:?}`): {e}"));

	(file, Vec::new())
		.into_generic_chain()
		.with_inner(|(f, v)| {
			f.read_to_end(v)
				.unwrap_or_else(|e| panic!("error occured reading input file at `{path:?}`: {e}"))
		})
		.map(|(_f, v)| v)
		.into_inner()
}

#[inline]
pub fn print_p1<T>(result: T)
where
	T: fmt::Display
{
	println!("part1: {result}")
}

#[inline]
pub fn print_p2<T>(result: T)
where
	T: fmt::Display
{
	println!("part2: {result}")
}

pub mod prelude {
	pub use crate::prelude::*;
	pub use crate::aoc::{ self, * };
	pub use crate::chain::{ self, * };
	pub use crate::num::*;
}