diff --git a/day07/src/main.rs b/day07/src/main.rs index 808468e..1716172 100644 --- a/day07/src/main.rs +++ b/day07/src/main.rs @@ -58,11 +58,11 @@ fn main() { }); for dir in dirs.iter() { for sub_dir in dir.1.iter().filter(|dir| dir.1.is_none()) { - let balls = (dir.0.to_owned() + &sub_dir.0.replace("dir ", "/")).replace("//", "/"); + let dir_name = (dir.0.to_owned() + &sub_dir.0.replace("dir ", "/")).replace("//", "/"); dir_totals.insert( dir.0.to_owned(), - *dir_totals.get(&dir.0.to_owned()).unwrap() + *dir_totals.get(&balls).unwrap(), + *dir_totals.get(&dir.0.to_owned()).unwrap() + *dir_totals.get(&dir_name).unwrap(), ); } } diff --git a/day11/Cargo.lock b/day11/Cargo.lock new file mode 100644 index 0000000..4331ddb --- /dev/null +++ b/day11/Cargo.lock @@ -0,0 +1,42 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "day11" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" diff --git a/day11/Cargo.toml b/day11/Cargo.toml new file mode 100644 index 0000000..504eece --- /dev/null +++ b/day11/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day11" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1" diff --git a/day11/src/main.rs b/day11/src/main.rs new file mode 100644 index 0000000..dfbcf78 --- /dev/null +++ b/day11/src/main.rs @@ -0,0 +1,189 @@ +use regex::Regex; +use std::collections::{HashMap, VecDeque}; + +struct Monke { + operation: Box usize>, + test: usize, + result_recipient: (usize, usize), + items: VecDeque, +} + +enum Star { + Silver, + Gold, +} + +fn main() { + let mut monkes = HashMap::new(); + + let s = std::fs::read_to_string("/dev/stdin").unwrap(); + let thing = s.split("\n\n"); + + let mut monke_magic_number = 1; + for monke_str in thing { + let (monke, id) = monke_parser(monke_str); + monke_magic_number *= monke.test; + monkes.insert(id, monke); + } + + let mut inspections_silver: HashMap = HashMap::new(); + let mut inspections_gold = inspections_silver.clone(); + + monke_processor(&mut monkes, &mut inspections_silver, Star::Silver, 0); + monke_processor( + &mut monkes, + &mut inspections_gold, + Star::Gold, + monke_magic_number, + ); + + println!( + "P1: {}, P2: {}", + calc_monke_business(&inspections_silver), + calc_monke_business(&inspections_gold) + ); +} + +fn calc_monke_business(inspections: &HashMap) -> usize { + let mut ins = inspections.iter().map(|v| v.1).collect::>(); + ins.sort(); + ins.pop().unwrap() * ins.pop().unwrap() +} + +fn monke_processor( + monkes: &mut HashMap, + inspections: &mut HashMap, + part: Star, + monke_magic_number: usize, +) { + let num_turns = if matches!(part, Star::Gold) { + 10_000 + } else { + 20 + }; + + for _ in 0..num_turns { + for id in 0..monkes.len() { + let monke = monkes.get_mut(&id).unwrap(); + let mut pass_queue = Vec::new(); + + let mut inspections_for_monke = *inspections.get(&id).unwrap_or(&0); + + while let Some(mut worry_level) = monke.items.pop_front() { + inspections_for_monke += 1; + worry_level = (monke.operation)(worry_level); + + match part { + Star::Silver => worry_level /= 3, + Star::Gold => worry_level %= monke_magic_number, + } + + let passes_test = worry_level % monke.test == 0; + let recipient_id = if passes_test { + monke.result_recipient.0 + } else { + monke.result_recipient.1 + }; + pass_queue.push((recipient_id, worry_level)); + } + + inspections.insert(id, inspections_for_monke); + + for (recipient_id, val) in pass_queue { + let recipient_monke = monkes.get_mut(&recipient_id).unwrap(); + recipient_monke.items.push_back(val); + } + } + } +} + +fn monke_parser(monke_str: &str) -> (Monke, usize) { + let mut id = 0; + let mut items = VecDeque::default(); + let mut operation: Box usize>; + let mut test = 0; + let mut result_recipient = (0, 0); + operation = Box::new(|n| n); + + for mut line in monke_str.lines() { + line = line.trim(); + if line.starts_with("Monkey") { + id = Regex::new(r"\d+") + .unwrap() + .find(line) + .unwrap() + .as_str() + .parse::() + .unwrap(); + } + if line.starts_with("Starting") { + items = Regex::new(r"\d+(\d+, )?") + .unwrap() + .find_iter(line) + .map(|item| item.as_str().parse::().unwrap()) + .collect::>(); + } + if line.starts_with("Operation") { + let op = Regex::new(r"[*+/-].+") + .unwrap() + .find(line) + .unwrap() + .as_str() + .split_once(' ') + .unwrap(); + + operation = match op.1 { + "old" => match op.0 { + "+" => Box::new(|n| n + n), + "*" => Box::new(|n| n * n), + _ => panic!("https://music.portal2sounds.com/2627"), + }, + _ => { + let num = op.1.parse::().unwrap(); + match op.0 { + "+" => Box::new(move |n| n + num), + "*" => Box::new(move |n| n * num), + _ => panic!("https://music.portal2sounds.com/2627"), + } + } + }; + } + if line.starts_with("Test") { + test = Regex::new(r"[0-9]+") + .unwrap() + .find(line) + .unwrap() + .as_str() + .parse::() + .unwrap(); + } + if line.starts_with("If true") { + result_recipient.0 = Regex::new(r"[0-9]") + .unwrap() + .find(line) + .unwrap() + .as_str() + .parse::() + .unwrap(); + } + if line.starts_with("If false") { + result_recipient.1 = Regex::new(r"[0-9]") + .unwrap() + .find(line) + .unwrap() + .as_str() + .parse::() + .unwrap(); + } + } + + ( + Monke { + items, + operation, + test, + result_recipient, + }, + id, + ) +} diff --git a/fetchinput.sh b/fetchinput.sh index 97ae01f..9e3b904 100755 --- a/fetchinput.sh +++ b/fetchinput.sh @@ -3,7 +3,7 @@ # defaults to current day unless specified day is passed in DAY=$(printf "%01d" `date +%d`) -! [ "$1" = "" ] && printf "%d" $1 >/dev/null 2>&1 && DAY=$1 +! [ "$1" = "" ] && printf "%i" $1 >/dev/null 2>&1 && DAY=$1 [ "$AOC_SESSION_COOKIE" = "" ] && echo "\$AOC_SESSION_COOKIE not set" && exit 1 -curl -s https://adventofcode.com/2022/day/$DAY/input --cookie "session=$AOC_SESSION_COOKIE" -o day`printf "%02d" $DAY`/input +curl -s https://adventofcode.com/2022/day/$DAY/input --cookie "session=$AOC_SESSION_COOKIE" -o day`printf "%02i" $DAY`/input