nairby

2022 Day 10

Dec 10th, 2022
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.08 KB | None | 0 0
  1. use std::env;
  2. use std::io::{self, prelude::*, BufReader};
  3. use std::fs::File;
  4.  
  5. enum Status {
  6.     Waiting,
  7.     Processing,
  8.     Done,
  9. }
  10. enum InstKind {
  11.     Noop,
  12.     Addx,
  13. }
  14. struct Instruction {
  15.     kind: InstKind,
  16.     value: Option<i64>,
  17.     status: Status,
  18. }
  19. impl Instruction {
  20.     fn from(s: &String) -> Self {
  21.         let parts: Vec<_> = s.split(" ").collect();
  22.         match parts[0] {
  23.             "noop" => Self { kind: InstKind::Noop, value: None, status: Status::Waiting },
  24.             "addx" => Self { kind: InstKind::Addx, value: Some(parts[1].parse().unwrap()), status: Status::Waiting },
  25.             other => panic!("Unknown instruction: {other}"),
  26.         }
  27.     }
  28. }
  29.  
  30. fn solve(input: &str) -> io::Result<()> {
  31.     let file = File::open(input).expect("Input file not found.");
  32.     let reader = BufReader::new(file);
  33.  
  34.     // Input
  35.     let input: Vec<String> = match reader.lines().collect() {
  36.         Err(err) => panic!("Unknown error reading input: {}", err),
  37.         Ok(result) => result,
  38.     };
  39.  
  40.     // Initializations
  41.     let mut instructions: Vec<_> = input.iter().map(Instruction::from).collect();
  42.     let mut reg: i64 = 1;
  43.     let mut instr: usize = 0;
  44.     let mut part1: i64 = 0;
  45.     const PIX_PER_ROW: usize = 40;
  46.     const NUM_ROWS: usize = 6;
  47.     let mut screen = [[false; PIX_PER_ROW]; NUM_ROWS];
  48.  
  49.     // Processing
  50.     'main_lp: for cyc in 1.. {
  51.        if instr >= instructions.len() { break 'main_lp; }
  52.         let ins = &instructions[instr];
  53.  
  54.         // Gather signal strengths
  55.         match cyc {
  56.             20 | 60 | 100 | 140 | 180 | 220 => {
  57.                 println!("Cycle {cyc}, reg: {reg}, signal={}",cyc*reg);
  58.                 part1 += cyc * reg;
  59.             },
  60.             _ => {},
  61.         }
  62.  
  63.         // Handle instruction
  64.         match ins.kind {
  65.             InstKind::Noop => { instr += 1 },
  66.             InstKind::Addx => {
  67.                 match ins.status {
  68.                     Status::Waiting => {
  69.                         instructions[instr].status = Status::Processing;
  70.                         },
  71.                     Status::Processing => {
  72.                         reg += ins.value.unwrap();
  73.                         instructions[instr].status = Status::Done;
  74.                         instr += 1;
  75.                         },
  76.                     _ => panic!("Shouldn't be able to get here"),
  77.                 }
  78.             },
  79.         }
  80.  
  81.         // Render image
  82.         let row: usize = cyc as usize / PIX_PER_ROW;
  83.         let col: usize = cyc as usize % PIX_PER_ROW;
  84.         let coli = col as i64;
  85.         let (px0,px1,px2) = (reg - 1, reg, reg + 1);
  86.         if coli == px0 || coli == px1 || coli == px2 { screen[row][col] = true; }
  87.     }
  88.     println!("Part 1: {part1}"); // 13480
  89.  
  90.     // Part 2 Output: EGJBGCFK
  91.     for y in 0..NUM_ROWS {
  92.         for x in 0..PIX_PER_ROW {
  93.             if screen[y][x] { print!("█"); } else { print!(" "); }
  94.         }
  95.         println!();
  96.     }
  97.  
  98.     Ok(())
  99. }
  100.  
  101. fn main() {
  102.     let args: Vec<String> = env::args().collect();
  103.     let filename = &args[1];
  104.     solve(&filename).unwrap();
  105. }
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment