optimized day 10 part 2 by eliminating padded noops and repeating the noop block once in addx block
This commit is contained in:
parent
7fa967e4d3
commit
7b58b9d577
66
src/main.rs
66
src/main.rs
|
@ -8,7 +8,6 @@ const INPUTS: [&[u8]; 2] = [
|
|||
|
||||
#[derive(Debug)]
|
||||
enum Ins {
|
||||
Null,
|
||||
Noop,
|
||||
Addx(i32),
|
||||
}
|
||||
|
@ -17,8 +16,8 @@ fn parse(input: &[u8]) -> Vec<Ins> {
|
|||
input
|
||||
.split(|&c| c == b'\n')
|
||||
.filter(|c| !c.is_empty())
|
||||
.flat_map(|line| match &line[0..4] {
|
||||
[b'n', b'o', b'o', b'p'] => [Ins::Noop, Ins::Null],
|
||||
.map(|line| match &line[0..4] {
|
||||
[b'n', b'o', b'o', b'p'] => Ins::Noop,
|
||||
[b'a', b'd', b'd', b'x'] => {
|
||||
let is_neg = line[5] == b'-';
|
||||
let b: i32 = line[5..]
|
||||
|
@ -26,7 +25,7 @@ fn parse(input: &[u8]) -> Vec<Ins> {
|
|||
.filter(|&&c| (b'0'..=b'9').contains(&c))
|
||||
.fold(0, |a, &x| (a * 10) + (x - b'0') as i32);
|
||||
|
||||
[Ins::Noop, Ins::Addx(if is_neg { -b } else { b })]
|
||||
Ins::Addx(if is_neg { -b } else { b })
|
||||
}
|
||||
_ => unreachable!(),
|
||||
})
|
||||
|
@ -51,26 +50,53 @@ fn solution(input: Vec<Ins>) -> String {
|
|||
|
||||
let mut i = 0;
|
||||
for ip in input.into_iter() {
|
||||
if let Ins::Null = ip {
|
||||
continue;
|
||||
}
|
||||
match ip {
|
||||
Ins::Noop => {
|
||||
if sprite & ((1 << 63) >> i) > 0 {
|
||||
line |= (1 << 63) >> i;
|
||||
}
|
||||
|
||||
if sprite & ((1 << 63) >> i) > 0 {
|
||||
line |= (1 << 63) >> i;
|
||||
}
|
||||
cycle += 1;
|
||||
i += 1;
|
||||
|
||||
if let Ins::Addx(v) = ip {
|
||||
register += v;
|
||||
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;
|
||||
}
|
||||
|
||||
sprite = (0b111 << 61) >> (register - 1);
|
||||
}
|
||||
cycle += 1;
|
||||
i += 1;
|
||||
cycle += 1;
|
||||
i += 1;
|
||||
|
||||
if cycle % 40 == 0 {
|
||||
answer.push(line);
|
||||
line = 0;
|
||||
i = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user