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:
parent
6919f8fabc
commit
80020a1003
18
src/main.rs
18
src/main.rs
|
@ -8,6 +8,7 @@ const INPUTS: [&[u8]; 2] = [
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Ins {
|
enum Ins {
|
||||||
|
Null,
|
||||||
Noop,
|
Noop,
|
||||||
Addx(i32),
|
Addx(i32),
|
||||||
}
|
}
|
||||||
|
@ -17,7 +18,7 @@ fn parse(input: &[u8]) -> Vec<Ins> {
|
||||||
.split(|&c| c == b'\n')
|
.split(|&c| c == b'\n')
|
||||||
.filter(|c| !c.is_empty())
|
.filter(|c| !c.is_empty())
|
||||||
.flat_map(|line| match &line[0..4] {
|
.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'] => {
|
[b'a', b'd', b'd', b'x'] => {
|
||||||
let is_neg = line[5] == b'-';
|
let is_neg = line[5] == b'-';
|
||||||
let b: i32 = line[5..]
|
let b: i32 = line[5..]
|
||||||
|
@ -25,7 +26,7 @@ fn parse(input: &[u8]) -> Vec<Ins> {
|
||||||
.filter(|&&c| (b'0'..=b'9').contains(&c))
|
.filter(|&&c| (b'0'..=b'9').contains(&c))
|
||||||
.fold(0, |a, &x| (a * 10) + (x - b'0') as i32);
|
.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!(),
|
_ => unreachable!(),
|
||||||
})
|
})
|
||||||
|
@ -50,17 +51,18 @@ fn solution(input: Vec<Ins>) -> String {
|
||||||
|
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for ip in input.into_iter() {
|
for ip in input.into_iter() {
|
||||||
|
if let Ins::Null = ip {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if sprite & ((1 << 63) >> i) > 0 {
|
if sprite & ((1 << 63) >> i) > 0 {
|
||||||
line |= (1 << 63) >> i;
|
line |= (1 << 63) >> i;
|
||||||
}
|
}
|
||||||
|
|
||||||
match ip {
|
if let Ins::Addx(v) = ip {
|
||||||
Ins::Noop => {}
|
register += v;
|
||||||
Ins::Addx(v) => {
|
|
||||||
register += v;
|
|
||||||
|
|
||||||
sprite = (0b111 << 61) >> (register - 1);
|
sprite = (0b111 << 61) >> (register - 1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
cycle += 1;
|
cycle += 1;
|
||||||
i += 1;
|
i += 1;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user