1
0

Added day 11 part 1

This commit is contained in:
Ishan Jain 2022-12-11 11:31:27 +05:30
parent 7b58b9d577
commit 4f5e5f6c63
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
3 changed files with 176 additions and 370 deletions

View File

@ -1,141 +1,56 @@
noop Monkey 0:
noop Starting items: 83, 97, 95, 67
noop Operation: new = old * 19
addx 4 Test: divisible by 17
addx 3 If true: throw to monkey 2
addx 3 If false: throw to monkey 7
addx 3
noop Monkey 1:
addx 2 Starting items: 71, 70, 79, 88, 56, 70
addx 1 Operation: new = old + 2
addx -7 Test: divisible by 19
addx 10 If true: throw to monkey 7
addx 1 If false: throw to monkey 0
addx 5
addx -3 Monkey 2:
addx -7 Starting items: 98, 51, 51, 63, 80, 85, 84, 95
addx 13 Operation: new = old + 7
addx 5 Test: divisible by 7
addx 2 If true: throw to monkey 4
addx 1 If false: throw to monkey 3
addx -30
addx -8 Monkey 3:
noop Starting items: 77, 90, 82, 80, 79
addx 3 Operation: new = old + 1
addx 2 Test: divisible by 11
addx 7 If true: throw to monkey 6
noop If false: throw to monkey 4
addx -2
addx 5 Monkey 4:
addx 2 Starting items: 68
addx -7 Operation: new = old * 5
addx 8 Test: divisible by 13
addx 2 If true: throw to monkey 6
addx 5 If false: throw to monkey 5
addx 2
addx -12 Monkey 5:
noop Starting items: 60, 94
addx 17 Operation: new = old + 5
addx 3 Test: divisible by 3
addx -2 If true: throw to monkey 1
addx 2 If false: throw to monkey 0
noop
addx 3 Monkey 6:
addx -38 Starting items: 81, 51, 85
noop Operation: new = old * old
addx 3 Test: divisible by 5
addx 4 If true: throw to monkey 5
noop If false: throw to monkey 1
addx 5
noop Monkey 7:
noop Starting items: 98, 81, 63, 65, 84, 71, 84
noop Operation: new = old + 3
addx 1 Test: divisible by 2
addx 2 If true: throw to monkey 2
addx 5 If false: throw to monkey 3
addx 2
addx -3
addx 4
addx 2
noop
noop
addx 7
addx -30
addx 31
addx 4
noop
addx -24
addx -12
addx 1
addx 5
addx 5
noop
noop
noop
addx -12
addx 13
addx 4
noop
addx 23
addx -19
addx 1
addx 5
addx 12
addx -28
addx 19
noop
addx 3
addx 2
addx 5
addx -40
addx 4
addx 32
addx -31
noop
addx 13
addx -8
addx 5
addx 2
addx 5
noop
noop
noop
addx 2
addx -7
addx 8
addx -7
addx 14
addx 3
addx -2
addx 2
addx 5
addx -40
noop
noop
addx 3
addx 4
addx 1
noop
addx 2
addx 5
addx 2
addx 21
noop
addx -16
addx 3
noop
addx 2
noop
addx 1
noop
noop
addx 4
addx 5
noop
noop
noop
noop
noop
noop
noop

View File

@ -1,146 +1,27 @@
addx 15 Monkey 0:
addx -11 Starting items: 79, 98
addx 6 Operation: new = old * 19
addx -3 Test: divisible by 23
addx 5 If true: throw to monkey 2
addx -1 If false: throw to monkey 3
addx -8
addx 13 Monkey 1:
addx 4 Starting items: 54, 65, 75, 74
noop Operation: new = old + 6
addx -1 Test: divisible by 19
addx 5 If true: throw to monkey 2
addx -1 If false: throw to monkey 0
addx 5
addx -1 Monkey 2:
addx 5 Starting items: 79, 60, 97
addx -1 Operation: new = old * old
addx 5 Test: divisible by 13
addx -1 If true: throw to monkey 1
addx -35 If false: throw to monkey 3
addx 1
addx 24 Monkey 3:
addx -19 Starting items: 74
addx 1 Operation: new = old + 3
addx 16 Test: divisible by 17
addx -11 If true: throw to monkey 0
noop If false: throw to monkey 1
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

View File

@ -1,33 +1,87 @@
#![feature(test)] #![feature(test)]
use std::{cmp::Reverse, rc::Rc};
extern crate test; extern crate test;
const INPUTS: [&[u8]; 2] = [ const INPUTS: [&str; 2] = [
include_bytes!("../inputs/sample.txt"), include_str!("../inputs/sample.txt"),
include_bytes!("../inputs/input.txt"), include_str!("../inputs/input.txt"),
]; ];
#[derive(Debug)] #[derive(Clone)]
enum Ins { struct Monkey {
Noop, items: Vec<usize>,
Addx(i32), operation: Rc<Box<dyn Fn(usize) -> usize>>,
div_by_test: usize,
if_true: usize,
if_false: usize,
} }
fn parse(input: &[u8]) -> Vec<Ins> { fn parse(input: &'static str) -> Vec<Monkey> {
input input
.split(|&c| c == b'\n') .split("\n\n")
.filter(|c| !c.is_empty()) .filter(|c| !c.is_empty())
.map(|line| match &line[0..4] { .map(|set| {
[b'n', b'o', b'o', b'p'] => Ins::Noop, let mut lines = set.lines().skip(1);
[b'a', b'd', b'd', b'x'] => {
let is_neg = line[5] == b'-';
let b: i32 = line[5..]
.iter()
.filter(|&&c| (b'0'..=b'9').contains(&c))
.fold(0, |a, &x| (a * 10) + (x - b'0') as i32);
Ins::Addx(if is_neg { -b } else { b }) let sitems: Vec<usize> = lines
.next()
.unwrap()
.split(',')
.map(|c| {
c.chars()
.filter(|c| c.is_numeric())
.fold(0, |a, x| (a * 10) + (x as u8 - b'0') as usize)
})
.collect();
let op = lines.next().unwrap();
let nop = op
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as usize);
let op = move |old: usize| -> usize {
if op.contains("old * old") {
old * old
} else if op.contains("old +") {
old + nop
} else if op.contains("old *") {
old * nop
} else {
unreachable!()
} }
_ => unreachable!(), };
let test = lines
.next()
.unwrap()
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as usize);
let true_result = lines
.next()
.unwrap()
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as usize);
let false_result = lines
.next()
.unwrap()
.bytes()
.filter(|c| (b'0'..=b'9').contains(c))
.fold(0, |a, x| (a * 10) + (x - b'0') as usize);
Monkey {
items: sitems,
operation: Rc::new(Box::new(op)),
div_by_test: test,
if_true: true_result,
if_false: false_result,
}
//
}) })
.collect() .collect()
} }
@ -40,80 +94,36 @@ fn main() {
} }
} }
fn solution(input: Vec<Ins>) -> String { fn solution(mut input: Vec<Monkey>) -> usize {
let mut register = 1i32; let mlen = input.len();
let mut cycle = 0; let mut activity = vec![0; mlen];
let mut line: u64 = 0;
let mut sprite: u64 = 0b111 << 61;
let mut answer = Vec::with_capacity(10); for _ in 0..20 {
for i in 0..mlen {
let monkey = &input[i].clone();
let ilen = monkey.items.len();
let mut i = 0; for j in 0..ilen {
for ip in input.into_iter() { let item = monkey.items[j];
match ip {
Ins::Noop => {
if sprite & ((1 << 63) >> i) > 0 {
line |= (1 << 63) >> i;
}
cycle += 1; let newwlevel = (monkey.operation)(item);
i += 1; let calmed_down_level = newwlevel / 3;
if cycle % 40 == 0 { if calmed_down_level % monkey.div_by_test == 0 {
answer.push(line); input[monkey.if_true].items.push(calmed_down_level);
line = 0;
i = 0;
}
}
Ins::Addx(v) => {
// Noop
if sprite & ((1 << 63) >> i) > 0 {
line |= (1 << 63) >> i;
}
cycle += 1;
i += 1;
if cycle % 40 == 0 {
answer.push(line);
line = 0;
i = 0;
}
// Add
if sprite & ((1 << 63) >> i) > 0 {
line |= (1 << 63) >> i;
}
register += v;
sprite = (0b111 << 61) >> (register - 1);
cycle += 1;
i += 1;
if cycle % 40 == 0 {
answer.push(line);
line = 0;
i = 0;
}
}
}
}
let mut output = String::with_capacity(40 * 10);
for row in answer {
for i in 0..40 {
if row & ((1 << 63) >> i) > 0 {
output.push('\u{2588}');
} else { } else {
output.push(' '); input[monkey.if_false].items.push(calmed_down_level);
}
}
output.push('\n');
} }
output activity[i] += 1;
}
input[i].items.clear();
}
}
activity.select_nth_unstable_by_key(1, |c| Reverse(*c));
activity[0] * activity[1]
} }
#[bench] #[bench]