Refactoring, Added day10
This commit is contained in:
parent
5f243befac
commit
5366017271
77
src/day10/1.rs
Normal file
77
src/day10/1.rs
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
#![feature(test)]
|
||||||
|
extern crate test;
|
||||||
|
|
||||||
|
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Ins {
|
||||||
|
Noop,
|
||||||
|
Addx(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &'static str) -> Vec<Ins> {
|
||||||
|
input
|
||||||
|
.trim()
|
||||||
|
.split('\n')
|
||||||
|
.flat_map(|line| {
|
||||||
|
let (a, b) = line.split_at(4);
|
||||||
|
|
||||||
|
match a {
|
||||||
|
"noop" => vec![Ins::Noop],
|
||||||
|
"addx" => {
|
||||||
|
let b = b.trim();
|
||||||
|
let b: i32 = b.parse::<i32>().unwrap();
|
||||||
|
|
||||||
|
vec![Ins::Noop, Ins::Addx(b)]
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
for input in INPUTS.iter() {
|
||||||
|
let output = parse(input);
|
||||||
|
let score = solution(output);
|
||||||
|
println!("{}", score);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solution(input: Vec<Ins>) -> i32 {
|
||||||
|
let mut register = 1i32;
|
||||||
|
let mut cycle = 0;
|
||||||
|
let mut store = [0; 220];
|
||||||
|
|
||||||
|
for ip in input.into_iter() {
|
||||||
|
match ip {
|
||||||
|
Ins::Noop => {
|
||||||
|
store[cycle] = register;
|
||||||
|
}
|
||||||
|
Ins::Addx(v) => {
|
||||||
|
store[cycle] = register;
|
||||||
|
register += v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cycle += 1;
|
||||||
|
if cycle >= 220 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
store[20 - 1] * 20
|
||||||
|
+ store[60 - 1] * 60
|
||||||
|
+ store[100 - 1] * 100
|
||||||
|
+ store[140 - 1] * 140
|
||||||
|
+ store[180 - 1] * 180
|
||||||
|
+ store[220 - 1] * 220
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn solution_bench(b: &mut test::Bencher) {
|
||||||
|
b.iter(|| {
|
||||||
|
let input = parse(INPUTS[1]);
|
||||||
|
let result = solution(input);
|
||||||
|
test::black_box(result);
|
||||||
|
})
|
||||||
|
}
|
126
src/day10/2.rs
Normal file
126
src/day10/2.rs
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
#![feature(test)]
|
||||||
|
extern crate test;
|
||||||
|
|
||||||
|
const INPUTS: [&[u8]; 2] = [
|
||||||
|
include_bytes!("./sample.txt"),
|
||||||
|
include_bytes!("./input.txt"),
|
||||||
|
];
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Ins {
|
||||||
|
Noop,
|
||||||
|
Addx(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &[u8]) -> Vec<Ins> {
|
||||||
|
input
|
||||||
|
.split(|&c| c == b'\n')
|
||||||
|
.filter(|c| !c.is_empty())
|
||||||
|
.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..]
|
||||||
|
.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 })
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
for input in INPUTS.iter() {
|
||||||
|
let output = parse(input);
|
||||||
|
let score = solution(output);
|
||||||
|
println!("{}", score);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solution(input: Vec<Ins>) -> String {
|
||||||
|
let mut register = 1i32;
|
||||||
|
let mut cycle = 0;
|
||||||
|
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() {
|
||||||
|
match ip {
|
||||||
|
Ins::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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut output = String::with_capacity(40 * 10);
|
||||||
|
|
||||||
|
for row in answer {
|
||||||
|
for i in 0..40 {
|
||||||
|
if row & ((1 << 63) >> i) > 0 {
|
||||||
|
output.push('\u{2588}');
|
||||||
|
} else {
|
||||||
|
output.push(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output.push('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
|
#[bench]
|
||||||
|
fn solution_bench(b: &mut test::Bencher) {
|
||||||
|
b.iter(|| {
|
||||||
|
let input = parse(INPUTS[1]);
|
||||||
|
let result = solution(input);
|
||||||
|
test::black_box(result);
|
||||||
|
})
|
||||||
|
}
|
141
src/day10/input.txt
Normal file
141
src/day10/input.txt
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 4
|
||||||
|
addx 3
|
||||||
|
addx 3
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
addx -7
|
||||||
|
addx 10
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx -3
|
||||||
|
addx -7
|
||||||
|
addx 13
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
addx -30
|
||||||
|
addx -8
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx -2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -7
|
||||||
|
addx 8
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -12
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -38
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
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
|
||||||
|
|
146
src/day10/sample.txt
Normal file
146
src/day10/sample.txt
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
addx 15
|
||||||
|
addx -11
|
||||||
|
addx 6
|
||||||
|
addx -3
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -8
|
||||||
|
addx 13
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -35
|
||||||
|
addx 1
|
||||||
|
addx 24
|
||||||
|
addx -19
|
||||||
|
addx 1
|
||||||
|
addx 16
|
||||||
|
addx -11
|
||||||
|
noop
|
||||||
|
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
|
Loading…
Reference in New Issue
Block a user