master
george 2 years ago
parent db450943a0
commit 2b36afe92b

@ -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(),
);
}
}

42
day11/Cargo.lock generated

@ -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"

@ -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"

@ -0,0 +1,189 @@
use regex::Regex;
use std::collections::{HashMap, VecDeque};
struct Monke {
operation: Box<dyn Fn(usize) -> usize>,
test: usize,
result_recipient: (usize, usize),
items: VecDeque<usize>,
}
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<usize, usize> = 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, usize>) -> usize {
let mut ins = inspections.iter().map(|v| v.1).collect::<Vec<_>>();
ins.sort();
ins.pop().unwrap() * ins.pop().unwrap()
}
fn monke_processor(
monkes: &mut HashMap<usize, Monke>,
inspections: &mut HashMap<usize, usize>,
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<dyn Fn(usize) -> 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::<usize>()
.unwrap();
}
if line.starts_with("Starting") {
items = Regex::new(r"\d+(\d+, )?")
.unwrap()
.find_iter(line)
.map(|item| item.as_str().parse::<usize>().unwrap())
.collect::<VecDeque<usize>>();
}
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::<usize>().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::<usize>()
.unwrap();
}
if line.starts_with("If true") {
result_recipient.0 = Regex::new(r"[0-9]")
.unwrap()
.find(line)
.unwrap()
.as_str()
.parse::<usize>()
.unwrap();
}
if line.starts_with("If false") {
result_recipient.1 = Regex::new(r"[0-9]")
.unwrap()
.find(line)
.unwrap()
.as_str()
.parse::<usize>()
.unwrap();
}
}
(
Monke {
items,
operation,
test,
result_recipient,
},
id,
)
}

@ -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

Loading…
Cancel
Save