Commit cb344df7 authored by Jonathan Klimt's avatar Jonathan Klimt

Solved Day4 (overcomplex)

parent 797076a6
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "day4"
version = "0.1.0"
dependencies = [
"regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "redox_syscall"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "redox_termios"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.45 (registry+https://github.com/rust-lang/crates.io-index)" = "2d2857ec59fadc0773853c664d2d18e7198e83883e7060b63c924cb077bd5c74"
"checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9"
"checksum redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "a84bcd297b87a545980a2d25a0beb72a1f490c31f0a9fde52fca35bfbb1ceb70"
"checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76"
"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f"
"checksum regex-syntax 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4e47a2ed29da7a9e1960e1639e7a982e6edc6d49be308a3b02daf511504a16d1"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[package]
name = "day4"
version = "0.1.0"
authors = ["Jonathan Klimt <jonathan.klimt@rwth-aachen.de>"]
edition = '2018'
[dependencies]
regex = "1"
termion = "1.5"
This diff is collapsed.
This diff is collapsed.
extern crate regex;
extern crate termion;
use self::termion::color;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Error;
use std::collections::HashMap;
use std::collections::BTreeMap;
use regex::Regex;
#[derive(Clone)]
struct Shift {
id: usize,
month: usize,
day: usize,
time: [bool; 60],
}
type Schedule = HashMap<(usize, usize), Shift>;
type GuardSchedule = BTreeMap<usize, Vec<[bool; 60]>>;
fn main() -> Result<(), Error> {
let file = File::open("input_sorted.txt")?;
let (_s, g) = parse_log(&file)?;
let sleepy_guy = get_guard_most_sleep(&g)?;
let sleepy_min = get_most_sleepy_min(sleepy_guy ,&g)?;
println!("Most sleepy guy: {}", sleepy_guy);
println!("Most sleepy min: {}", sleepy_min);
println!("Solution: {}", sleepy_guy * sleepy_min);
Ok(())
}
fn parse_log(f: &File) -> Result<(Schedule, GuardSchedule), Error> {
let bufread = BufReader::new(f);
let mut shift_map : Schedule = Schedule::new();
let guard_shift_re = Regex::new(r"\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] Guard #(\d+) begins shift").unwrap();
let guard_falls_asleep_re = Regex::new(r"\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] falls asleep").unwrap();
let guard_wakes_up_re = Regex::new(r"\[1518-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] wakes up").unwrap();
let mut sleeping = false;
let mut sleep_begin = 0;
for line in bufread.lines() {
let l = line?;
if let Some(caps) = guard_shift_re.captures(&l) {
let active_guard = caps[5].parse().unwrap();
let mut month : usize = caps[1].parse().unwrap();
let mut day : usize = caps[2].parse().unwrap();
let hour : usize = caps[3].parse().unwrap();
// let min : usize = caps[4].parse().unwrap();
if hour == 23 {
match (day, month) {
(31, 1) | (28, 2) | (31, 3) | (30, 4) |
(31, 5) | (30, 6) | (31, 7) | (31, 8) |
(30, 9) | (31, 10) | (30, 11) | (31, 12) => {day = 1; month += 1;},
_ => day += 1,
}
}
assert!(!sleeping);
assert_eq!(shift_map.contains_key(&(month, day)), false);
let s : Shift = Shift {id: active_guard, month: month, day: day, time:[false; 60]};
shift_map.insert((month, day), s);
// println!("active guard {} {}", active_guard, l);
}
else if let Some(caps) = guard_falls_asleep_re.captures(&l) {
let month : usize = caps[1].parse().unwrap();
let day : usize = caps[2].parse().unwrap();
// let hour : usize = caps[3].parse().unwrap();
let min : usize = caps[4].parse().unwrap();
assert!(!sleeping);
assert_eq!(shift_map.contains_key(&(month, day)), true);
sleep_begin = min;
sleeping = true;
// active_guard = caps[5].parse().unwrap();
// println!("fall asleep {}", l);
}
else if let Some(caps) = guard_wakes_up_re.captures(&l) {
let month : usize = caps[1].parse().unwrap();
let day : usize = caps[2].parse().unwrap();
// let hour : usize = caps[3].parse().unwrap();
let min : usize = caps[4].parse().unwrap();
assert!(sleeping);
assert_eq!(shift_map.contains_key(&(month, day)), true);
sleeping = false;
for i in sleep_begin..min {
shift_map.get_mut(&(month, day)).unwrap().time[i] = true;
}
// active_guard = caps[5].parse().unwrap();
// println!("fall asleep {}", l);
}
// println!("line: {}, caps: {:?}", l, c);
}
print_schedule(&shift_map);
let mut guard_map : GuardSchedule = GuardSchedule::new();
for i in shift_map.iter() {
if let Some(v) = guard_map.get_mut(&i.1.id) {
v.push(i.1.time);
} else {
let v = vec!(i.1.time);
guard_map.insert(i.1.id, v);
}
// guard_map.insert(i.1.clone());
}
print_guard_map(&guard_map);
Ok((shift_map, guard_map))
}
fn get_guard_most_sleep(g: &GuardSchedule) -> Result<usize, Error> {
let mut sleepy_id: usize = 0;
let mut sleep_record: usize = 0;
for i in g {
let mut sleep_time = 0;
for v in i.1.into_iter() {
for t in v.into_iter() {
if t == &true {
sleep_time += 1;
}
}
}
if sleep_time > sleep_record {
sleep_record = sleep_time;
sleepy_id = *i.0;
}
}
Ok(sleepy_id)
}
fn get_most_sleepy_min(g_id: usize, g: &GuardSchedule) -> Result<usize, Error> {
let t : Vec<[bool; 60]> = g.get(&g_id).unwrap().clone();
let mut sleepy_min : (usize, usize) = (0,0);
for i in 0..60 {
let mut sleep_count : usize = 0;
for d in t.clone() {
if d[i] {
sleep_count += 1;
}
}
if sleep_count > sleepy_min.1 {
sleepy_min = (i, sleep_count);
}
// println!{"Sleep count of {}: {} | {:?}",i, sleep_count, sleepy_min};
}
Ok(sleepy_min.0)
}
fn print_schedule(s: &Schedule) {
for m in 1..12 {
println!("\nM: {}\t|000000000001111111112222222222333333333344444444445555555555|", m);
println!("\t|012345678901234567890123456789012345678901234567890123456789|");
for d in 1..32 {
if let Some(e) = s.get(&(m, d)) {
print!("{}.{}. \t|", d, m);
for i in e.time.into_iter() {
if i == &true {
print!("{}Z", color::Fg(color::Red));
} else {
print!("{}.", color::Fg(color::Cyan));
}
}
println!("{}| Id: {}", color::Fg(color::Reset), e.id);
}
}
}
}
fn print_guard_map(g: &GuardSchedule) {
for i in g {
println!("\nGuard {:?}", i.0);
for v in i.1.into_iter() {
print!("{}\t|", i.0);
for t in v.into_iter() {
if t == &true {
print!("{}Z", color::Fg(color::Red));
} else {
print!("{}.", color::Fg(color::Cyan));
}
}
println!("{}|", color::Fg(color::Reset));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment