Added day 11 part 1
This commit is contained in:
parent
7b58b9d577
commit
4f5e5f6c63
195
inputs/input.txt
195
inputs/input.txt
|
@ -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
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
||||||
|
|
178
src/main.rs
178
src/main.rs
|
@ -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!()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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,
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
//
|
||||||
})
|
})
|
||||||
.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 => {
|
let newwlevel = (monkey.operation)(item);
|
||||||
if sprite & ((1 << 63) >> i) > 0 {
|
let calmed_down_level = newwlevel / 3;
|
||||||
line |= (1 << 63) >> i;
|
|
||||||
|
if calmed_down_level % monkey.div_by_test == 0 {
|
||||||
|
input[monkey.if_true].items.push(calmed_down_level);
|
||||||
|
} else {
|
||||||
|
input[monkey.if_false].items.push(calmed_down_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
cycle += 1;
|
activity[i] += 1;
|
||||||
i += 1;
|
|
||||||
|
|
||||||
if cycle % 40 == 0 {
|
|
||||||
answer.push(line);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
input[i].items.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut output = String::with_capacity(40 * 10);
|
activity.select_nth_unstable_by_key(1, |c| Reverse(*c));
|
||||||
|
|
||||||
for row in answer {
|
activity[0] * activity[1]
|
||||||
for i in 0..40 {
|
|
||||||
if row & ((1 << 63) >> i) > 0 {
|
|
||||||
output.push('\u{2588}');
|
|
||||||
} else {
|
|
||||||
output.push(' ');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
output.push('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user