Articles tagged #rust

Day 14 (Advent of Code 2022)

I like how the day 14 puzzle sounds, because I think it’ll give me an opportunity to show off yet another way to have Rust embedded in a web page.

But first…

Cool bear

Let me guess: parsing?

You bet your furry ass, parsing.

Parsing

The input looks something like this:

498,4 -> 498,6 -> 496,6 503,4 -> 502,4 -> 502,9 -> 494,9

And each line is essentially… a polyline: we’re supposed to draw lines between every point on the path, and that determines rocks on the map.

Day 13 (Advent of Code 2022)

The day 13 puzzle needs a speech therapist.

Cool bear

???

…because it has an awful lisp!! Ahhhahahahhhh

Cool bear

Are you ok? What is.. what is going on with you?

No but seriously we have what are ostensibly S-expressions, except they use JSON-adjacent notation:

[1,1,3,1,1] [1,1,5,1,1] [[1],[2,3,4]] [[1],4] [9] [[8,7,6]] [[4,4],4,4] [[4,4],4,4,4] [7,7,7,7] [7,7,7] [] [3] [[[]]] [[]] [1,[2,[3,[4,[5,6,7]]]],8,9] [1,[2,[3,[4,[5,6,0]]]],8,9]

Day 12 (Advent of Code 2022)

Alright! The day 12 puzzle involves path finding, and it seems like a good time to lean more heavily on the WASM embeds I’ve set up for the previous parts.

Let’s start by setting up the types we’ll want!

Types and parsing

Our input is a heightmap, like so:

Sabqponm abcryxxl accszExk acctuvwj abdefghi

Where 'a'..='z' is a square with a given elevation (from lowest to highest), S is the start, and E is the end.

Day 11 (Advent of Code 2022)

It’s a new day, it’s a new advent of code puzzle.

In that one, we have to apparently cosplay as an IBM mainframe and just.. crunch them numbers. This doesn’t look fun, and I can’t think of a clever twist to make it fun, so let’s try to make it short and sweet.

Parsing

Our input looks like this:

Monkey 0: Starting items: 79, 98 Operation: new = old * 19 Test: divisible by 23 If true: throw to monkey 2 If false: throw to monkey 3 Monkey 1: Starting items: 54, 65, 75, 74 Operation: new = old + 6 Test: divisible by 19 If true: throw to monkey 2 If false: throw to monkey 0 (etc)

Day 10 (Advent of Code 2022)

Onwards! To the day 10 puzzle.

I don’t see a way to make part 1 especially fun — so let’s just get to it.

Parsing

As usual, let’s reach for the nom crate

$ cargo add nom@7 (cut)

…to parse the input into nicely-organized Rust data structures:

// in `src/main.rs` use nom::{ branch::alt, bytes::complete::tag, combinator::{map, value}, sequence::preceded, IResult, }; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] enum Instruction { Noop, Addx(i32), } impl Instruction { fn parse(i: &str) -> IResult<&str, Self> { let noop = tag("noop"); let addx = preceded(tag("addx "), nom::character::complete::i32); alt((value(Self::Noop, noop), map(addx, Self::Addx)))(i) } fn cycles(self) -> u32 { match self { Self::Noop => 1, Self::Addx(_) => 2, } } }

Day 9 (Advent of Code 2022)

The Advent of Code is not a sprint: it’s a marathon: sometimes you’ve got to stop and smell the roses.

Cool bear

I… what? That’s not.. have you done a marathon before?

No, and I haven’t taken any creative writing classes either, I think you can tell. Anyway: Day 8 was a bit aggravating for me. In 2020 I gave up AoC after Day 14 I think, and then I skipped a year. It doesn’t help that it overlaps some holidays and stuff, but!

Day 8 (Advent of Code 2022)

In the day 8 problem, our input is a height map:

30373 25512 65332 33549 35390

This is a 5x5 grid, and every number denotes the height of a tree. For part 1, we must find out how many trees are visible from the outside of the grid.

If we consider the first row, from the left: only the 3 is visible: it obscures the 0. From the right, 3 and 7 are visible.

Day 7 (Advent of Code 2022)

The day 7 challenge talks about trees! File trees that is.

The temptation to solve it before starting to write this article so I don’t look silly is high, but I’m explicitly not doing so, so that we can bang our collective heads against any walls at the same time, and see how we can get out of it! Trees are serious business!

Part 1

The sample input looks like this:

Go back to the homepage.