| 
 | 1 | +trait Cyclable {  | 
 | 2 | + fn run_noop(&mut self) {  | 
 | 3 | + self.run_cycle();  | 
 | 4 | + }  | 
 | 5 | + | 
 | 6 | + fn run_addx(&mut self) {  | 
 | 7 | + self.run_cycle();  | 
 | 8 | + self.run_cycle();  | 
 | 9 | + }   | 
 | 10 | + | 
 | 11 | + fn run_instruction(&mut self, instruction: String);  | 
 | 12 | + | 
 | 13 | + fn run_cycle(&mut self);  | 
 | 14 | +}  | 
 | 15 | + | 
 | 16 | +struct CycleState {  | 
 | 17 | + cycle: i32,  | 
 | 18 | + register_x: i32,  | 
 | 19 | + signal_sum: i32  | 
 | 20 | +}  | 
 | 21 | + | 
 | 22 | +impl CycleState {  | 
 | 23 | + fn new() -> Self {  | 
 | 24 | + Self{  | 
 | 25 | + cycle: 0,  | 
 | 26 | + register_x: 1,  | 
 | 27 | + signal_sum: 0  | 
 | 28 | + }  | 
 | 29 | + }  | 
 | 30 | +}  | 
 | 31 | + | 
 | 32 | +impl Cyclable for CycleState {  | 
 | 33 | + fn run_instruction(&mut self, instruction: String) {  | 
 | 34 | + let mut instruction_segments = instruction.split(" ");  | 
 | 35 | + | 
 | 36 | + match instruction_segments.next().unwrap() {  | 
 | 37 | + "noop" => self.run_noop(),  | 
 | 38 | + "addx" => {  | 
 | 39 | + self.run_addx();  | 
 | 40 | + self.register_x += instruction_segments.next()  | 
 | 41 | + .unwrap()  | 
 | 42 | + .parse::<i32>()  | 
 | 43 | + .unwrap();  | 
 | 44 | + }  | 
 | 45 | + _ => {}  | 
 | 46 | + }  | 
 | 47 | + }  | 
 | 48 | + | 
 | 49 | + fn run_cycle(&mut self) {  | 
 | 50 | + self.cycle += 1;  | 
 | 51 | + if self.cycle == 20 || (self.cycle - 20) % 40 == 0 {  | 
 | 52 | + self.signal_sum += self.cycle * self.register_x;  | 
 | 53 | + }  | 
 | 54 | + }  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +struct CrtState {  | 
 | 58 | + cycle: i32,  | 
 | 59 | + register_x: i32,  | 
 | 60 | + crt_image: Vec<char>  | 
 | 61 | +}  | 
 | 62 | + | 
 | 63 | +impl CrtState {  | 
 | 64 | + fn new() -> Self {  | 
 | 65 | + Self{  | 
 | 66 | + cycle: 0,  | 
 | 67 | + register_x: 1,  | 
 | 68 | + crt_image: vec![]  | 
 | 69 | + }  | 
 | 70 | + }  | 
 | 71 | +}  | 
 | 72 | + | 
 | 73 | +impl Cyclable for CrtState {  | 
 | 74 | + fn run_instruction(&mut self, instruction: String) {  | 
 | 75 | + let mut instruction_segments = instruction.split(" ");  | 
 | 76 | + | 
 | 77 | + match instruction_segments.next().unwrap() {  | 
 | 78 | + "noop" => self.run_noop(),  | 
 | 79 | + "addx" => {  | 
 | 80 | + self.run_addx();  | 
 | 81 | + self.register_x += instruction_segments.next()  | 
 | 82 | + .unwrap()  | 
 | 83 | + .parse::<i32>()  | 
 | 84 | + .unwrap();  | 
 | 85 | + }  | 
 | 86 | + _ => {}  | 
 | 87 | + }  | 
 | 88 | + }  | 
 | 89 | + | 
 | 90 | + fn run_cycle(&mut self) {  | 
 | 91 | + self.crt_image.push(if self.cycle >= self.register_x - 1 && self.cycle <= self.register_x + 1 {  | 
 | 92 | + '.'  | 
 | 93 | + } else {  | 
 | 94 | + '#'  | 
 | 95 | + });  | 
 | 96 | + self.cycle += 1;  | 
 | 97 | + if self.cycle % 40 == 0 {  | 
 | 98 | + self.cycle = 0;  | 
 | 99 | + self.crt_image.push('\n');  | 
 | 100 | + }  | 
 | 101 | + }  | 
 | 102 | +}  | 
 | 103 | + | 
 | 104 | +fn main() {  | 
 | 105 | + let data = include_str!("../Day10.txt");  | 
 | 106 | + let processed_data = process_data(data);  | 
 | 107 | + | 
 | 108 | + println!("{}", part1(&processed_data));  | 
 | 109 | + println!("{}", part2(&processed_data));  | 
 | 110 | +}  | 
 | 111 | + | 
 | 112 | +fn part1(data: &Vec<String>) -> i32 {  | 
 | 113 | + data.iter()  | 
 | 114 | + .fold(CycleState::new(), |mut state, instruction| {  | 
 | 115 | + state.run_instruction(instruction.to_string());  | 
 | 116 | + state  | 
 | 117 | + }).signal_sum  | 
 | 118 | +}  | 
 | 119 | + | 
 | 120 | +fn part2(data: &Vec<String>) -> String {  | 
 | 121 | + data.iter()  | 
 | 122 | + .fold(CrtState::new(), |mut state, instruction| {  | 
 | 123 | + state.run_instruction(instruction.to_string());  | 
 | 124 | + state  | 
 | 125 | + }).crt_image.iter().collect()  | 
 | 126 | +}  | 
 | 127 | + | 
 | 128 | +fn process_data(data: &'static str) -> Vec<String> {  | 
 | 129 | + data.replace("\r\n", "\n")  | 
 | 130 | + .split("\n")  | 
 | 131 | + .map(|s| s.to_string())  | 
 | 132 | + .collect()  | 
 | 133 | +}  | 
0 commit comments