1
0
Fork 0

Add another Null instruction which doesn't do _anything_

This is so we can return an array while parsing rather than a vector
Reduces runtime by 1 microsecond
This commit is contained in:
Ishan Jain 2022-12-10 12:27:44 +05:30
parent 6919f8fabc
commit 80020a1003
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
1 changed files with 10 additions and 8 deletions

View File

@ -8,6 +8,7 @@ const INPUTS: [&[u8]; 2] = [
#[derive(Debug)]
enum Ins {
Null,
Noop,
Addx(i32),
}
@ -17,7 +18,7 @@ fn parse(input: &[u8]) -> Vec<Ins> {
.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'] => vec![Ins::Noop],
[b'n', b'o', b'o', b'p'] => [Ins::Noop, Ins::Null],
[b'a', b'd', b'd', b'x'] => {
let is_neg = line[5] == b'-';
let b: i32 = line[5..]
@ -25,7 +26,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);
vec![Ins::Noop, Ins::Addx(if is_neg { -b } else { b })]
[Ins::Noop, Ins::Addx(if is_neg { -b } else { b })]
}
_ => unreachable!(),
})
@ -50,17 +51,18 @@ fn solution(input: Vec<Ins>) -> String {
let mut i = 0;
for ip in input.into_iter() {
if let Ins::Null = ip {
continue;
}
if sprite & ((1 << 63) >> i) > 0 {
line |= (1 << 63) >> i;
}
match ip {
Ins::Noop => {}
Ins::Addx(v) => {
register += v;
if let Ins::Addx(v) = ip {
register += v;
sprite = (0b111 << 61) >> (register - 1);
}
sprite = (0b111 << 61) >> (register - 1);
}
cycle += 1;
i += 1;