Advertisement
Guest User

Day 10 - AoC - Avenger___

a guest
Dec 10th, 2022
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.26 KB | None | 0 0
  1. use std::fs;
  2.  
  3. struct CRT {
  4.     // Devices
  5.     cpu: CPU,
  6.     gpu: GPU,
  7.     // Registrars
  8.     clock: f64,
  9.     signal_strenght: f64,
  10.     checkpoints: Vec<f64>,
  11. }
  12.  
  13. struct CPU {
  14.     current_instruction: usize,
  15.     instruction_active: bool,
  16.     register: f64,
  17.     instructions: Vec<String>,
  18.     shutdown: bool
  19. }
  20.  
  21. struct GPU {
  22.     width: i32,
  23.     display: Vec<String>
  24. }
  25.  
  26. impl CRT {
  27.     fn new(instruction_set: Vec<String>, width: i32) -> CRT {
  28.         CRT {
  29.             cpu: CPU::new(instruction_set),
  30.             gpu: GPU::new(width),
  31.             clock: 1.0,
  32.             signal_strenght: 0.0,
  33.             checkpoints: vec![ 20.0, 60.0, 100.0, 140.0, 180.0, 220.0 ]
  34.         }
  35.     }
  36.  
  37.     fn start(&mut self) {
  38.         while !self.cpu.shutdown {
  39.             self.tick();
  40.         }
  41.  
  42.         self.gpu.draw();
  43.         println!("Signal Strenght: {}", self.signal_strenght);
  44.     }
  45.  
  46.     fn tick(&mut self) {
  47.         if self.checkpoints.contains(&self.clock) {
  48.             self.signal_strenght += self.cpu.register * self.clock;
  49.         }
  50.  
  51.         self.gpu.tick(self.clock, &self.cpu);
  52.         self.cpu.tick(self.clock);
  53.  
  54.         self.clock += 1.0;
  55.     }
  56. }
  57.  
  58. impl CPU {
  59.     fn new(instruction_set: Vec<String>) -> CPU {
  60.         CPU { current_instruction: 0, instruction_active: false, register: 1.0, instructions: instruction_set, shutdown: false }
  61.     }
  62.  
  63.     fn tick(&mut self, clock: f64) {
  64.         if self.instructions.len() - 1 <= self.current_instruction {
  65.             self.shutdown = true;
  66.         }
  67.  
  68.         let instruction: Vec<&str> = self.instructions[self.current_instruction].split(" ").collect();
  69.        
  70.         if instruction[0] == "addx" {
  71.             let value = instruction[1].parse::<f64>().expect(&format!("I{} - addx - FATAL: Unparseable Value", clock));
  72.             if self.instruction_active {
  73.                 self.addx(value);
  74.                 self.instruction_active = false;
  75.                 self.current_instruction += 1;
  76.             } else {
  77.                 self.instruction_active = true;
  78.             }
  79.         } else {
  80.             self.noop();
  81.             self.current_instruction += 1;
  82.         }
  83.     }
  84.  
  85.     fn addx(&mut self, value: f64) {
  86.         self.register += value;
  87.     }
  88.  
  89.     fn noop(&mut self) {    }
  90. }
  91.  
  92. impl GPU {
  93.     fn new(width: i32) -> GPU {
  94.         GPU { display: vec![], width: width }
  95.     }
  96.  
  97.     fn tick(&mut self, clock: f64, cpu: &CPU) {
  98.         let mut normalized_clock = clock - 1.0;
  99.         while normalized_clock > 39.0 {
  100.             normalized_clock -= 40.0;
  101.         }
  102.  
  103.         let mut cur_pixel = String::from(".");
  104.         let sprite = (cpu.register-1.1)..(cpu.register+1.1);
  105.         if sprite.contains(&normalized_clock) {
  106.             cur_pixel = String::from("#");
  107.         }
  108.  
  109.         self.display.push(cur_pixel);
  110.     }
  111.  
  112.     fn draw(&self) {
  113.         let display_buffer = self.display.chunks(self.width as usize);
  114.         for line in display_buffer {
  115.             println!("{}", line.join(""));
  116.         }
  117.     }
  118. }
  119.  
  120. fn main() {
  121.     let raw_instructions = fs::read_to_string("source.txt").expect("Unable to read file");
  122.     let instructions: Vec<String> = raw_instructions.lines().map(String::from).collect();
  123.  
  124.     let mut crt = CRT::new(instructions, 40 );
  125.  
  126.     crt.start();    
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement