Sblog

AoC 2022 - Days 3 to 10

Date: 2022-12-10

Day 3

Description: https://adventofcode.com/2022/day/3
Solution: https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_3

Day 3 was not very exciting - the exercise consisted of some basic string manipulation.

Day 4

Description: https://adventofcode.com/2022/day/4
Solution: https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_4

The exercise was simple yet again. It did make me stumble into something new about Rust - I learnt that range literals actually return a value of type Range or RangeInclusive. These range structs have some useful methods that I used in my solution. I didn't know that the inclusive range even existed and its literal(x..=y) is something I've not seen in other languages.

Day 5

Description: https://adventofcode.com/2022/day/5
Solution: https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_5

This exercise was a bit more fun as it involved some string parsing and stack manipulation. The string parsing was simple enough that I used the Regex crate together with the Lazy Static crate. The lazy_static crate allows us to easily compile and reuse the same regex with a very succinct syntax.

The first part involved manipulating a regular stack which I did with the vector push() and pop() methods. The second part required that we pop multiple items from one stack and push them in the same order in a second stack which I did with the vector methods split_off() and extend().

Day 6

Description: https://adventofcode.com/2022/day/6
Solution: https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_6

I found this one fun - it involves finding a unique sequence of characters in a text stream. The unique sequence is any sequence of the specified length that does not have any letters repeated. For part one the length of the sequence was 4 and part two - 14. Rust's robust iterators proved helpful once more - this time I used a sliding window iterator. Once I had the slice that the window iterator gave me I would clone it into a new vector and then run the sort() and dedupe() methods and check the new length vs the old one to find out if all characters in the original slice were unique. I wasn't worried that I'm doing vector cloning because the size of the vector is pretty small (either 4 or 14) and the overhead is not big enough to matter.

Day 7

Description: https://adventofcode.com/2022/day/7
Solution (JS): https://github.com/stygian91/advent-of-code-2022/tree/master/js/q_7
Solution (Rust): https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_7

The goal of this one is to build up a file system tree from the output of a bunch of ls and cd commands. I first started with the JS implementation because I knew from experience and reading that recursive data types are tricky in Rust. After I had the algorithm down with JS I went back to Rust and implemented the recursive data type there too. The trick to the file tree recursive structure was using the reference counting smart pointer together with the RefCell container. Reading this part of the Rust book as well as this article helped me achieve that but ultimately there's no replacement for #doingityourself.

Day 8

Description: https://adventofcode.com/2022/day/8
Solution: https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_8

Day 8 was not too interesting - just a bit of 2D array traversal and number comparison.

Day 9

Description: https://adventofcode.com/2022/day/9
Solution: https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_9

This exercise was about simulating how ropes move but with the specific rules that the exercise gives you. The first part has you simulate a rope of 2 segments - the head and tail where the tail has to follow the movements of the head with some specific rules as to how exactly that happens.

The second part requires you to simulate a rope of 10 segments where each segment has to follow the movement of the previous segment the same way as in part 1. To complete the second part I had to generalize my simulation of 2 segments to work with any number of segments (greater or equal to 2). To achieve that I extracted my logic for moving the tail piece and used a sliding window iterator again (see Day#6) to propagate the movement down the rope.

Day 10

Description: https://adventofcode.com/2022/day/10
Solution: https://github.com/stygian91/advent-of-code-2022/tree/master/rs/q_10

I nerded out on this one about as much as on the file system exercise. Day 10 asks you to simulate a very simplistic CPU for part 1 and expands that with the simulation of a CRT display in part 2. I had the simulation down correctly pretty quickly, I only had one annoying problem which stemmed from misunderstanding when I need to read the CPU state in order to calculate my answer. The test data I had was working perfectly up to the 180th cycle but was giving me the wrong answer for the 220th cycle which was a bit puzzling, but I soon(ish) figured out that it was an off-by-one-error.