LogoWCF

cheyenneson's Solution

Open in the sandbox

2025 Solution Rust

use std::fs;

fn solution(contents: &str) {
    let mut location = (0,0);
    let mut lines: Vec<(i32, i32, char)> = Vec::new();

    for line in contents.lines() {
        let a = line.as_bytes()[0] as char;
        let b = &line[1..];

        match a {
            'U' => {
                let num: i32 = b.parse().unwrap();
                location.1 -= num;
            }
            'D' => {
                let num: i32 = b.parse().unwrap();
                location.1 += num;
            }
            'L' => {
                if b == "R" {
                    location = (0,0);
                } else {
                    let num: i32 = b.parse().unwrap();
                    location.0 -= num;
                }
            }
            'R' => {
                let num: i32 = b.parse().unwrap();
                location.0 += num;
            }
            'P' => {
                lines.push((location.0, location.1, b.chars().next().unwrap()));
            }
            _ => {
                println!("Error!");
            }
        }
    }

    // render
    lines.sort_by_key(|item| (item.1, item.0));
    let mut prev_line = 0;
    let mut prev_x = 0;

    for line in lines {
        while prev_line != line.1 {
            println!();
            prev_x = 0;
            prev_line += 1;
        }
        while prev_x != line.0 {
            print!(" ");
            prev_x += 1;
        }
        print!("{}", line.2);
        prev_x += 1;
    }

    println!("\n");
}

fn main() {
    for entry in fs::read_dir("../days/04").unwrap() {
        let path = entry.unwrap().path();

        if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("txt") {
            println!("{}: \n", path.to_str().unwrap());
            solution(&fs::read_to_string(&path).unwrap());
        }
    }
}

Chillenge 4: Santa Printer Party

Santa has a printer that he uses to create stocking stuffers. Sadly, all of the printers lost their rendering firmware! Luckily, the source files for the designs he prints are pretty simple.

For this challenge, create a renderer that uses the following rules:

Here is an example of how the canvas coordinate system works:

                    +X

       0   4   8
      ┌─────────────────>
     0│
      │
      │
      │
     4│
      │ ┼
      │   (1,5)
      │
+Y   8│
      │
      V

The canvas doesn’t have explicit dimensions, but must be large enough to include all points.

Here’s an example command file:

R1
P1
LR
D1
P2
R1
D1
P3
LR
R2
D1
P4

Should output:

 1
2 4
 3

Problem: Render the files 0.txt, 1.txt, 2.txt, 3.txt, 4.txt, 5.txt. 0.txt is small and may be a good starting point.