NickyMeulemanNime
Metadata
  • Date

  • Last update

  • By

    • Nicky Meuleman
  • Tagged

  • Part of series

  • Older post

    Advent of Code 2023 Day 17

  • Newer post

    Advent of Code 2023 Day 19

Table of contents
  1. Day 18: Lavaduct Lagoon
  2. Part 1
    1. Helpers
    2. Code
  3. Part 2
    1. Code
  4. Final code

Advent of Code 2023 Day 18

Day 18: Lavaduct Lagoon

https://adventofcode.com/2023/day/18

The factory needs so much lava to work through its backlog, that the elves are digging a collection pool right in front of the entrance.

Today’s input is a list of instructions to dig the perimeter of the pool.

An example input looks like this:

input.txt
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)

Each instruction tells the digger to move to a different point, and dig every tile it passes through.

When followed, it results in a closed loop of trench being dug.

Part 1

The first letter in an instruction is the direction the digger should move in next:

  • U up
  • D down
  • L left
  • R right

The number tells the digger how far it should move in that direction.

That hexcode looking thing is not used in part1.

Every tile the loop enloses should also be dug out.

The question asks how many tiles are dug out.


I broke this up into 2 parts:

  • Parsing an instruction per line
  • Calculating the area when following the instructions

Helpers

I represented each instruction as an Instr:

struct Instr {
dir: Dir,
amount: i64,
}

Each direction is a Dir:

enum Dir {
Up,
Down,
Left,
Right,
}

The parsing:

let instructions = input.lines().map(|line| {
let (instr, _) = line.split_once(" (").unwrap();
let (dir, amount) = instr.split_once(" ").unwrap();
let dir = match dir {
"U" => Dir::Up,
"D" => Dir::Down,
"L" => Dir::Left,
"R" => Dir::Right,
_ => panic!("wrong dir"),
};
let amount = amount.parse().unwrap();
Instr { dir, amount }
});

If you follow each instruction, the trench is a polygon with square edges. The question is asking for the area of that polygon.

That polygon has a bunch of coordinates at its edges that are specified by following the instructions. So I made a Coord:

struct Coord {
x: i64,
y: i64,
}

I used the shoelace formula to calculate its area.

More information about polygon coordinates and areas

Calculating the area, a function that takes the list of instructions:

fn calc_area(instructions: impl Iterator<Item = Instr>) -> i64 {
let (area, perimeter, _) = instructions.fold(
(0, 0, Coord { x: 0, y: 0 }),
|(area, perimeter, pos), Instr { dir, amount }| {
let new_pos = pos.advance(&dir, amount);
let new_area = area + (pos.x * new_pos.y - new_pos.x * pos.y);
let new_perimeter = (new_pos.x - pos.x).abs() + (new_pos.y - pos.y).abs() + perimeter;
(new_area, new_perimeter, new_pos)
},
);
(area.abs() + perimeter) / 2 + 1
}

That function uses a helper that lets a coordinate move in a direction for an amount of steps:

impl Coord {
pub fn advance(&self, direction: &Dir, amount: i64) -> Self {
match direction {
Dir::Up => Self {
x: self.x + amount,
y: self.y,
},
Dir::Down => Self {
x: self.x - amount,
y: self.y,
},
Dir::Left => Self {
x: self.x,
y: self.y - amount,
},
Dir::Right => Self {
x: self.x,
y: self.y + amount,
},
}
}
}

Code

day_18.rs
pub fn part_1(input: &str) -> i64 {
let instructions = input.lines().map(|line| {
let (instr, _) = line.split_once(" (").unwrap();
let (dir, amount) = instr.split_once(" ").unwrap();
let dir = match dir {
"U" => Dir::Up,
"D" => Dir::Down,
"L" => Dir::Left,
"R" => Dir::Right,
_ => panic!("wrong dir"),
};
let amount = amount.parse().unwrap();
Instr { dir, amount }
});
calc_area(instructions)
}

Part 2

Oops! The instructions should be interpreted differently.

Each hexadecimal code is six digits long.

  • The first five hexadecimal digits encode the distance in meters as a five-digit hexadecimal number.
  • The last hexadecimal digit encodes the direction to dig: 0 means R, 1 means D, 2 means L, and 3 means U.

The question asks how many tiles are dug out.


Only the parsing logic changes.

Code

day_18.rs
pub fn part_2(input: &str) -> i64 {
let instructions = input.lines().map(|line| {
let line = line.strip_suffix(")").unwrap();
let (_, hex) = line.split_once("(#").unwrap();
let (amount, dir) = hex.split_at(5);
let amount = i64::from_str_radix(amount, 16).unwrap();
let dir = match dir {
"3" => Dir::Up,
"1" => Dir::Down,
"2" => Dir::Left,
"0" => Dir::Right,
_ => panic!("wrong dir"),
};
Instr { dir, amount }
});
calc_area(instructions)
}

Final code

day_18.rs
1struct Coord {
2 x: i64,
3 y: i64,
4}
5
6impl Coord {
7 pub fn advance(&self, direction: &Dir, amount: i64) -> Self {
8 match direction {
9 Dir::Up => Self {
10 x: self.x + amount,
11 y: self.y,
12 },
13 Dir::Down => Self {
14 x: self.x - amount,
15 y: self.y,
16 },
17 Dir::Left => Self {
18 x: self.x,
19 y: self.y - amount,
20 },
21 Dir::Right => Self {
22 x: self.x,
23 y: self.y + amount,
24 },
25 }
26 }
27}
28
29struct Instr {
30 dir: Dir,
31 amount: i64,
32}
33
34enum Dir {
35 Up,
36 Down,
37 Left,
38 Right,
39}
40
41fn calc_area(instructions: impl Iterator<Item = Instr>) -> i64 {
42 let (area, perimeter, _) = instructions.fold(
43 (0, 0, Coord { x: 0, y: 0 }),
44 |(area, perimeter, pos), Instr { dir, amount }| {
45 let new_pos = pos.advance(&dir, amount);
46 let new_area = area + (pos.x * new_pos.y - new_pos.x * pos.y);
47 let new_perimeter = (new_pos.x - pos.x).abs() + (new_pos.y - pos.y).abs() + perimeter;
48 (new_area, new_perimeter, new_pos)
49 },
50 );
51
52 (area.abs() + perimeter) / 2 + 1
53}
54
55pub fn part_1(input: &str) -> i64 {
56 let instructions = input.lines().map(|line| {
57 let (instr, _) = line.split_once(" (").unwrap();
58 let (dir, amount) = instr.split_once(" ").unwrap();
59 let dir = match dir {
60 "U" => Dir::Up,
61 "D" => Dir::Down,
62 "L" => Dir::Left,
63 "R" => Dir::Right,
64 _ => panic!("wrong dir"),
65 };
66 let amount = amount.parse().unwrap();
67 Instr { dir, amount }
68 });
69
70 calc_area(instructions)
71}
72
73pub fn part_2(input: &str) -> i64 {
74 let instructions = input.lines().map(|line| {
75 let line = line.strip_suffix(")").unwrap();
76 let (_, hex) = line.split_once("(#").unwrap();
77 let (amount, dir) = hex.split_at(5);
78 let amount = i64::from_str_radix(amount, 16).unwrap();
79 let dir = match dir {
80 "3" => Dir::Up,
81 "1" => Dir::Down,
82 "2" => Dir::Left,
83 "0" => Dir::Right,
84 _ => panic!("wrong dir"),
85 };
86 Instr { dir, amount }
87 });
88
89 calc_area(instructions)
90}

Series navigation for: Advent of Code 2023

1. Advent of Code 2023 Day 1

Designed and developed by Nicky Meuleman

Built with Gatsby. Hosted on Netlify.