Code for the Advent of Code event
aoc advent-of-code
at rust 58 lines 1.2 kB view raw
1use std::io::{self, Read}; 2 3/// Input data container for AoC problems. 4#[derive(Debug, Clone, PartialEq, Eq)] 5pub struct Input { 6 /// The raw input data as a string. 7 data: String, 8} 9 10impl Input { 11 pub fn new(data: String) -> Self { 12 Self { data } 13 } 14 15 pub fn from_stdin() -> Self { 16 let mut buffer = String::new(); 17 io::stdin() 18 .read_to_string(&mut buffer) 19 .expect("Failed to read from stdin"); 20 21 Self::new(buffer) 22 } 23 24 pub fn from_file(path: &std::path::Path) -> Self { 25 let data = std::fs::read_to_string(path).expect("Failed to read input file"); 26 Self::new(data) 27 } 28 29 pub fn as_str(&self) -> &str { 30 &self.data 31 } 32 33 pub fn len(&self) -> usize { 34 self.data.len() 35 } 36 37 pub fn is_empty(&self) -> bool { 38 self.data.is_empty() 39 } 40} 41 42impl From<&str> for Input { 43 fn from(s: &str) -> Self { 44 Self::new(s.to_string()) 45 } 46} 47 48impl From<String> for Input { 49 fn from(s: String) -> Self { 50 Self::new(s) 51 } 52} 53 54impl std::fmt::Display for Input { 55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 56 write!(f, "{}", self.data) 57 } 58}