day 4 solution
parent
ed8b988482
commit
9c31ed0700
|
@ -0,0 +1,96 @@
|
|||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.6.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"memchr 2.1.1 (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 = [
|
||||
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 1.1.0 (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.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.1.1"
|
||||
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.44 (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 = "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.1 (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 = "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.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
|
||||
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
|
||||
"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 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"
|
|
@ -4,3 +4,5 @@ version = "0.1.0"
|
|||
authors = ["Andrew Coleman <penguincoder@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
regex = "1"
|
||||
lazy_static = "1"
|
||||
|
|
|
@ -1,3 +1,123 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate regex;
|
||||
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::iter::Iterator;
|
||||
|
||||
type Day4Map = HashMap<u16, [u16; 60]>;
|
||||
|
||||
fn calc_minutes_from_lines(lines: &mut Vec<String>, map: &mut Day4Map, gid: u16, start: u16) {
|
||||
lazy_static! {
|
||||
static ref GUARD_RE: Regex =
|
||||
Regex::new(r"^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}\] Guard #(\d+) begins shift$").unwrap();
|
||||
static ref ASLEEP_RE: Regex =
|
||||
Regex::new(r"^\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\] falls asleep$").unwrap();
|
||||
static ref AWAKE_RE: Regex =
|
||||
Regex::new(r"^\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\] wakes up$").unwrap();
|
||||
}
|
||||
|
||||
let line = lines.remove(0);
|
||||
let line = line.as_str();
|
||||
let guard_id = if GUARD_RE.is_match(line) {
|
||||
let caps = GUARD_RE.captures(line).unwrap();
|
||||
caps[1].parse::<u16>().unwrap()
|
||||
} else {
|
||||
gid
|
||||
};
|
||||
|
||||
let start_minute = if ASLEEP_RE.is_match(line) {
|
||||
let caps = ASLEEP_RE.captures(line).unwrap();
|
||||
caps[1].parse::<u16>().unwrap()
|
||||
} else {
|
||||
start
|
||||
};
|
||||
|
||||
if AWAKE_RE.is_match(line) {
|
||||
let caps = AWAKE_RE.captures(line).unwrap();
|
||||
let end_minute = caps[1].parse::<u16>().unwrap();
|
||||
map.entry(guard_id)
|
||||
.and_modify(|e| {
|
||||
for minute in (start_minute..end_minute).into_iter() {
|
||||
e[usize::from(minute)] += 1;
|
||||
}
|
||||
})
|
||||
.or_insert([0u16; 60]);
|
||||
};
|
||||
|
||||
if lines.is_empty() {
|
||||
return;
|
||||
} else {
|
||||
calc_minutes_from_lines(lines, map, guard_id, start_minute)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_max_from_map(map: &mut Day4Map) -> HashMap<u16, u16> {
|
||||
let mut count_map: HashMap<u16, u16> = HashMap::new();
|
||||
let mut max_gid = 0;
|
||||
let mut max_count = 0;
|
||||
let mut max_minute = 0;
|
||||
let mut most_max_gid = 0;
|
||||
let mut most_max_minute = 0;
|
||||
let mut most_max_minute_index = 0;
|
||||
|
||||
for (key, value) in map {
|
||||
let sum = value.into_iter().sum();
|
||||
if sum > max_count {
|
||||
max_gid = *key;
|
||||
max_count = sum;
|
||||
let mut cur_max_min = 0;
|
||||
for i in 0..value.len() {
|
||||
if value[i] > cur_max_min {
|
||||
cur_max_min = value[i];
|
||||
max_minute = i as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i in 0..value.len() {
|
||||
if value[i] > most_max_minute {
|
||||
most_max_minute = value[i];
|
||||
most_max_minute_index = i as u16;
|
||||
most_max_gid = *key;
|
||||
}
|
||||
}
|
||||
|
||||
count_map.insert(*key, sum);
|
||||
}
|
||||
println!(
|
||||
"max guard id is {} sum {}, max minute of {} with val of {}",
|
||||
max_gid,
|
||||
max_count,
|
||||
max_minute,
|
||||
max_gid * max_minute
|
||||
);
|
||||
println!(
|
||||
"guard {} most asleep on a single minute {} with a count of {} is {}",
|
||||
most_max_gid,
|
||||
most_max_minute_index,
|
||||
most_max_minute,
|
||||
most_max_gid * most_max_minute_index
|
||||
);
|
||||
count_map
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut lines: Vec<String> = BufReader::new(File::open("sorted-input").unwrap())
|
||||
.lines()
|
||||
.map(|line| line.unwrap())
|
||||
.collect();
|
||||
let mut map = Day4Map::new();
|
||||
calc_minutes_from_lines(&mut lines, &mut map, 0, 0);
|
||||
/*for (key, value) in &map {
|
||||
print!("{}: ", key);
|
||||
for r in value.iter() {
|
||||
print!("{} ", r);
|
||||
}
|
||||
println!();
|
||||
}*/
|
||||
get_max_from_map(&mut map);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue