1
0

Added day 10 part 2

This commit is contained in:
Ishan Jain 2022-12-10 12:09:28 +05:30
parent 77fb6d71c8
commit 6919f8fabc
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -1,9 +1,9 @@
#![feature(test)] #![feature(test)]
extern crate test; extern crate test;
const INPUTS: [&str; 2] = [ const INPUTS: [&[u8]; 2] = [
include_str!("../inputs/sample.txt"), include_bytes!("../inputs/sample.txt"),
include_str!("../inputs/input.txt"), include_bytes!("../inputs/input.txt"),
]; ];
#[derive(Debug)] #[derive(Debug)]
@ -12,23 +12,22 @@ enum Ins {
Addx(i32), Addx(i32),
} }
fn parse(input: &'static str) -> Vec<Ins> { fn parse(input: &[u8]) -> Vec<Ins> {
input input
.trim() .split(|&c| c == b'\n')
.split('\n') .filter(|c| !c.is_empty())
.flat_map(|line| { .flat_map(|line| match &line[0..4] {
let (a, b) = line.split_at(4); [b'n', b'o', b'o', b'p'] => vec![Ins::Noop],
[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);
match a { vec![Ins::Noop, Ins::Addx(if is_neg { -b } else { b })]
"noop" => vec![Ins::Noop],
"addx" => {
let b = b.trim();
let b: i32 = b.parse::<i32>().unwrap();
vec![Ins::Noop, Ins::Addx(b)]
}
_ => unreachable!(),
} }
_ => unreachable!(),
}) })
.collect() .collect()
} }
@ -41,33 +40,52 @@ fn main() {
} }
} }
fn solution(input: Vec<Ins>) -> i32 { fn solution(input: Vec<Ins>) -> String {
let mut register = 1i32; let mut register = 1i32;
let mut cycle = 0; let mut cycle = 0;
let mut store = [0; 220]; let mut line: u64 = 0;
let mut sprite: u64 = 0b111 << 61;
let mut answer = Vec::with_capacity(10);
let mut i = 0;
for ip in input.into_iter() { for ip in input.into_iter() {
if sprite & ((1 << 63) >> i) > 0 {
line |= (1 << 63) >> i;
}
match ip { match ip {
Ins::Noop => { Ins::Noop => {}
store[cycle] = register;
}
Ins::Addx(v) => { Ins::Addx(v) => {
store[cycle] = register;
register += v; register += v;
sprite = (0b111 << 61) >> (register - 1);
} }
} }
cycle += 1; cycle += 1;
if cycle >= 220 { i += 1;
break;
if cycle % 40 == 0 {
answer.push(line);
line = 0;
i = 0;
} }
} }
store[20 - 1] * 20 let mut output = String::with_capacity(40 * 10);
+ store[60 - 1] * 60
+ store[100 - 1] * 100 for row in answer {
+ store[140 - 1] * 140 for i in 0..40 {
+ store[180 - 1] * 180 if row & ((1 << 63) >> i) > 0 {
+ store[220 - 1] * 220 output.push('\u{1f49c}');
} else {
output.push('\u{1f499}');
}
}
output.push('\n');
}
output
} }
#[bench] #[bench]