A simple CPU in Rust | Advent of Code 2022 Day 10

preview_player
Показать описание

# Chapters

00:00:00 Leaderboard
00:00:24 Part 1
00:02:46 Building a Parser
00:04:58 Alternate uses of use
00:13:21 Part 1 Recap
00:16:05 Part 2 Discovery
00:19:24 Part 2 Logic
00:28:44 Part 2 Recap
00:32:34 Future Work

Рекомендации по теме
Комментарии
Автор

Approach I took was to create a Vec of i32s that represented the register at each cycle, then for parts one and two I iterated over the ints to transform them into the data required for the respective part.

For part one `skip(20)` and `step_by(40)` made it pretty elegant. Part two was a simple map over the ints that was collected and then `chunk(40)` was printed out to get a readable view of the "screen".

Muaahaa
Автор

way easier to read if you just print out " " for a dark pixel instead of "."

jepcdd
Автор

I got tangled with the variable duration of the two instruction. So I ended up decomposing Addx instruction into two single-cycle instructions [ Noop, Addx ].
This way I got rid of the nested for loops and I could just draw a single pixel per instruction.

BlackSharkfr
Автор

I think this day's problem looked a lot more intimidating than it actually was lol.
Also made the same mistake with the exclusive range, good old off-by-one errors. 🙂


Btw, I think for longer test inputs it might be better to include them with `include_str!`, just for better readability.

valeth
Автор

I did it with using iterators. Much easier to collect and process data. But not the best in terms of memory usage.

artemivasyuk
Автор

Out of curiosity, which version of Rust are you running? I've used chunks and windows for a few of the solutions but notice you're using "chars().chunks()" which I don't seem to be able to do. I'm guessing you run Nightly?

To achieve the same effect I must do ".chars()
.collect::<Vec<_>>()
.chunks(40)
.map(|char|
.collect::<Vec<_>>()
.join("\n")"

ColinM