Skip to content

Instantly share code, notes, and snippets.

@sts10
Created December 5, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sts10/f7e7fd13cd14257461a7eb705c7a6a09 to your computer and use it in GitHub Desktop.
Save sts10/f7e7fd13cd14257461a7eb705c7a6a09 to your computer and use it in GitHub Desktop.
my not-working day 4 part 1 solution (Advent of Code 2018)
use std::collections::HashMap;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::io::BufReader;
use std::str::FromStr;
fn main() {
let mut events_vec: Vec<String> = read_by_line("inputs/day04-test.txt").unwrap();
// 1. Sort Events in chronological order
events_vec.sort();
println!("Events, sorted \n{:#?}", events_vec);
let mut events_structs_vec: Vec<Event> = vec![];
for event in &events_vec {
events_structs_vec.push(build_event_structs(event.to_string()));
}
// may need to do a .sort_by(|event| {event.date_time }); here but I'm not sure
// at this point, I have a Vector of Events sorted by time
//
// From this point, the date doesn't matter
//
// 1b. Go through events again and add a new parameter
// minutes_till_next_event_or_1am
// and (maybe) guard_id?
// 2. Build Guard structs into a Hashmap
let mut guards_map: HashMap<usize, [usize; 60]> = HashMap::new();
let mut minute_of_previous_event = 0;
let mut guard_of_previous_event = 10;
for event in &events_structs_vec {
// let this_guard_id;
if event.minute < minute_of_previous_event {
// assume it's a new night
minute_of_previous_event = 0;
}
let this_guard_id = match event.guard_id {
Some(id) => id,
None => guard_of_previous_event,
};
// maybe something wrong with this asleep bool
if event.asleep {
for m in minute_of_previous_event..event.minute {
guards_map
.entry(this_guard_id)
.and_modify(|arr| arr[m as usize] += 1)
.or_insert([0; 60]);
}
}
guard_of_previous_event = this_guard_id;
}
for guard in guards_map {
println!("Guard id {} has minutes:", guard.0);
let mut i = 0;
for m in guard.1.iter() {
println!("{} : {}", i, m);
i += 1;
}
println!("");
}
}
fn build_event_structs(event_string: String) -> Event {
println!("event string: {}", event_string);
// [1518-11-04 00:02] Guard #99 begins shift
// [1518-11-03 00:24] falls asleep
let white_space_split = event_string.split(' ');
let white_space_split_vec: Vec<&str> = white_space_split.collect::<Vec<&str>>();
let mut date_time: String =
white_space_split_vec[0].to_owned() + " " + white_space_split_vec[1];
// let date_time_len = &date_time.len();
date_time.remove(0);
date_time.pop();
// println!("date_time: {}", date_time);
// let date_split = white_space_split_vec[0].split("-");
// let date_split_vec = date_split.collect::<Vec<&str>>();
// let year = date_split_vec[0].parse::<i32>().unwrap();
// let month = date_split_vec[1].parse::<u32>().unwrap();
// let day = date_split_vec[2].parse::<u32>().unwrap();
let time_split = white_space_split_vec[1].split(":");
let time_split_vec: Vec<&str> = time_split.collect::<Vec<&str>>();
let minutes_split = time_split_vec[0].split("\"");
let minutes: &str = minutes_split.collect::<Vec<&str>>()[0];
println!("time_split_vec is -- {:#?} --", time_split_vec);
let hour = time_split_vec[0].parse::<u32>().unwrap();
let mut minutes_str: String = time_split_vec[1].to_string();
minutes_str.pop();
println!("minutes_str before parsing is {}", minutes_str);
let minute = minutes_str.parse::<u32>().unwrap();
let guard_id: Option<usize>;
if white_space_split_vec[2] == "Guard" {
let mut guard_id_string = white_space_split_vec[3].to_string();
guard_id_string.remove(0);
// println!("Guard id is {} ", guard_id_string);
guard_id = match guard_id_string.parse::<usize>() {
Ok(id) => Some(id),
Err(_e) => None,
};
} else {
guard_id = None;
}
println!("This event's guard's id is {:?}", guard_id);
// now assign asleep either true (if "begins shift" or "wakes up") or false (if "falls asleep")
// [1518-03-06 23:59] Guard #997 begins shift
// [1518-11-09 00:21] falls asleep
// [1518-06-18 00:55] wakes up
let asleep: bool;
if white_space_split_vec[2] == "Guard" || white_space_split_vec[2] == "wakes" {
asleep = false;
} else {
asleep = true;
}
println!("Is this guard asleep? {}", asleep);
Event {
date_time: date_time,
minute,
hour,
guard_id: guard_id,
asleep: asleep,
number_of_minutes_till_next_event_or_1am: None,
}
}
#[derive(Debug)]
struct Event {
date_time: String,
// year: usize,
// month: usize,
// day: usize,
hour: u32,
minute: u32,
guard_id: Option<usize>,
asleep: bool,
number_of_minutes_till_next_event_or_1am: Option<u32>,
}
#[derive(Debug)]
struct Guard {
id: usize,
minutes_between_midnight_and_1am_asleep: HashMap<usize, usize>,
number_of_minutes_between_midnight_and_1am_asleep: usize,
}
fn read_by_line<T: FromStr>(file_path: &str) -> io::Result<Vec<T>> {
let mut vec = Vec::new();
let f = match File::open(file_path.trim_matches(|c| c == '\'' || c == ' ')) {
Ok(res) => res,
Err(e) => return Err(e),
};
let file = BufReader::new(&f);
for line in file.lines() {
match line?.parse() {
Ok(l) => vec.push(l),
Err(_e) => {
panic!("Error reading a line of the file");
}
}
}
Ok(vec)
}
#[test]
fn can_sort_list_of_events_chronologically() {
// [1518-11-01 00:00] Guard #10 begins shift
// [1518-11-01 00:05] falls asleep
// [1518-11-01 00:25] wakes up
// [1518-11-01 00:30] falls asleep
// [1518-11-01 00:55] wakes up
// [1518-11-01 23:58] Guard #99 begins shift
// [1518-11-02 00:40] falls asleep
// [1518-11-02 00:50] wakes up
// [1518-11-03 00:05] Guard #10 begins shift
// [1518-11-03 00:24] falls asleep
// [1518-11-03 00:29] wakes up
// [1518-11-04 00:02] Guard #99 begins shift
// [1518-11-04 00:36] falls asleep
// [1518-11-04 00:46] wakes up
// [1518-11-05 00:03] Guard #99 begins shift
// [1518-11-05 00:45] falls asleep
// [1518-11-05 00:55] wakes up
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment