Day 11 part 1 done
This commit is contained in:
parent
786ed49808
commit
3e5b448af7
2 changed files with 226 additions and 11 deletions
|
@ -58,4 +58,60 @@ path = "src/day10.rs"
|
|||
name = "day11"
|
||||
path = "src/day11.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day12"
|
||||
path = "src/day12.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day13"
|
||||
path = "src/day13.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day14"
|
||||
path = "src/day14.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day15"
|
||||
path = "src/day15.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day16"
|
||||
path = "src/day16.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day17"
|
||||
path = "src/day17.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day18"
|
||||
path = "src/day18.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day19"
|
||||
path = "src/day19.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day20"
|
||||
path = "src/day20.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day21"
|
||||
path = "src/day21.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day22"
|
||||
path = "src/day22.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day23"
|
||||
path = "src/day23.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day24"
|
||||
path = "src/day24.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day25"
|
||||
path = "src/day25.rs"
|
||||
|
||||
[dependencies]
|
||||
|
|
|
@ -1,14 +1,148 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
mod common;
|
||||
|
||||
type Input = String;
|
||||
#[derive(Debug)]
|
||||
enum Operator {
|
||||
Mul,
|
||||
Add,
|
||||
}
|
||||
|
||||
impl Operator {
|
||||
fn apply(&self, operand: &Operand, old: i64) -> i64 {
|
||||
match self {
|
||||
Self::Mul => old * operand.amount(old),
|
||||
Self::Add => old + operand.amount(old),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Operand {
|
||||
I64(i64),
|
||||
Old,
|
||||
}
|
||||
|
||||
impl Operand {
|
||||
fn amount(&self, old: i64) -> i64 {
|
||||
match self {
|
||||
Self::I64(n) => n.clone(),
|
||||
Self::Old => old,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Operand {
|
||||
type Err = ParseOperandError;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if let Ok(n) = s.parse::<i64>() {
|
||||
Ok(Self::I64(n))
|
||||
} else {
|
||||
Ok(Self::Old)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ParseOpError;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ParseOperandError;
|
||||
|
||||
impl FromStr for Operator {
|
||||
type Err = ParseOpError;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s.bytes().next().unwrap() {
|
||||
43 => Ok(Self::Add),
|
||||
_ => Ok(Self::Mul),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Monkey {
|
||||
items: Vec<i64>,
|
||||
operator: Operator,
|
||||
operand: Operand,
|
||||
test_div: (i64, usize, usize),
|
||||
num_inspections: usize,
|
||||
}
|
||||
|
||||
impl Monkey {
|
||||
fn act(&mut self) -> Vec<(i64, usize)> {
|
||||
let mut result = vec![];
|
||||
while let Some(item) = self.items.pop() {
|
||||
self.num_inspections += 1;
|
||||
let new_item = self.operator.apply(&self.operand, item) / 3;
|
||||
if new_item % self.test_div.0 == 0 {
|
||||
result.push((new_item, self.test_div.1));
|
||||
} else {
|
||||
result.push((new_item, self.test_div.2));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ParseMonkeyError;
|
||||
|
||||
impl FromStr for Monkey {
|
||||
type Err = ParseMonkeyError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
println!("FROM Jk:{:?}", s);
|
||||
let mut lines = s.split("\n").skip(1);
|
||||
let mut p2 = |s| lines.next().unwrap().split(s).skip(1).next().unwrap();
|
||||
let items = p2(": ")
|
||||
.split(", ")
|
||||
.map(|s| s.parse::<i64>().unwrap())
|
||||
.collect();
|
||||
let oper = p2("old ");
|
||||
let operator = oper.parse::<Operator>().unwrap();
|
||||
let operand = oper[2..].parse::<Operand>().unwrap();
|
||||
let test_div = (
|
||||
p2("by ").parse::<i64>().unwrap(),
|
||||
p2("monkey ").parse::<usize>().unwrap(),
|
||||
p2("monkey ").parse::<usize>().unwrap(),
|
||||
);
|
||||
Ok(Monkey {
|
||||
items,
|
||||
operator,
|
||||
operand,
|
||||
test_div,
|
||||
num_inspections: 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type Input = Vec<Monkey>;
|
||||
type Answer = usize;
|
||||
|
||||
fn processed_input(input: &str) -> Input {
|
||||
input.to_owned()
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|s| s.parse::<Monkey>().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> Answer {
|
||||
100
|
||||
fn part1(input: &mut Input) -> Answer {
|
||||
for _round in 0..20 {
|
||||
for monkey_id in 0..input.len() {
|
||||
let monkey = &mut input[monkey_id];
|
||||
let items_goto = monkey.act();
|
||||
for (new_item, monkey_id) in items_goto {
|
||||
input[monkey_id].items.push(new_item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut num_inspections = input
|
||||
.iter()
|
||||
.map(|m| m.num_inspections)
|
||||
.collect::<Vec<usize>>();
|
||||
num_inspections.sort();
|
||||
num_inspections.iter().rev().take(2).product()
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> Answer {
|
||||
|
@ -16,12 +150,10 @@ fn part2(input: &Input) -> Answer {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let input_text = common::day_input(panic!(
|
||||
"PUT THE CORRECT DAY NUMBER HERE AND ADD bin TO Cargo.toml"
|
||||
));
|
||||
let input_text = common::day_input(11);
|
||||
eprintln!("{}\n\nAbove is your input file.\n\n", input_text);
|
||||
let input = processed_input(&input_text);
|
||||
common::show_answers(&part1(&input), &part2(&input))
|
||||
let mut input = processed_input(&input_text);
|
||||
common::show_answers(&part1(&mut input), &part2(&input))
|
||||
// common::show_both_answers(&both_parts(&input))
|
||||
}
|
||||
|
||||
|
@ -33,12 +165,39 @@ fn main() {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const TEST_INPUT: &str = "";
|
||||
const TEST_INPUT: &str = "Monkey 0:
|
||||
Starting items: 79, 98
|
||||
Operation: new = old * 19
|
||||
Test: divisible by 23
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 54, 65, 75, 74
|
||||
Operation: new = old + 6
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 79, 60, 97
|
||||
Operation: new = old * old
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 74
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 1";
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let mut input = processed_input(TEST_INPUT);
|
||||
assert_eq!(part1(&mut input), 10605);
|
||||
let input = processed_input(TEST_INPUT);
|
||||
assert_eq!(part1(&input), 0);
|
||||
assert_eq!(part2(&input), 0);
|
||||
// assert_eq!(both_parts(&input), (0, 0));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue