Skip to content

Commit

Permalink
[2023] Complete day 18
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 18, 2023
1 parent 3b178fe commit f48eea7
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
- [Day 15: Lens Library](aoc_2023/src/day_15.rs)
- [Day 16: The Floor Will Be Lava](aoc_2023/src/day_16.rs)
- [Day 17: Clumsy Crucible](aoc_2023/src/day_17.rs)
- [Day 18: Lavaduct Lagoon](aoc_2023/src/day_18.rs)
<!-- MARKER -->

## [2022](https://adventofcode.com/2022) [![aoc_2022](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml/badge.svg)](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml)
Expand Down
11 changes: 10 additions & 1 deletion aoc_2023/src/aoc_lib/direction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use nd_vec::{vector, Vec2};
use num_traits::Num;
use num_traits::{Num, Signed};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Direction {
Expand All @@ -18,6 +18,15 @@ impl Direction {
Direction::Right,
];

pub fn as_vector<T: Num + Signed + Copy>(&self) -> Vec2<T> {
match self {
Self::Up => vector!(T::zero(), -T::one()),
Self::Down => vector!(T::zero(), T::one()),
Self::Left => vector!(-T::one(), T::zero()),
Self::Right => vector!(T::one(), T::zero()),
}
}

#[rustfmt::skip]
pub fn try_advance<T: Num + Copy + PartialOrd>(&self, pos: Vec2<T>) -> Option<Vec2<T>> {
Some(match self {
Expand Down
108 changes: 108 additions & 0 deletions aoc_2023/src/day_18.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use common::{Answer, Solution};
use nd_vec::vector;

use crate::aoc_lib::direction::Direction;

pub struct Day18;

impl Solution for Day18 {
fn name(&self) -> &'static str {
"Lavaduct Lagoon"
}

fn part_a(&self, input: &str) -> Answer {
solve(&parse_a(input)).into()
}

fn part_b(&self, input: &str) -> Answer {
solve(&parse_b(input)).into()
}
}

fn solve(instructions: &[(Direction, u32)]) -> i64 {
let mut pos = vector!(0, 0);
let mut perimeter = 0;
let mut area = 0;

for (dir, steps) in instructions.iter().copied() {
let dir = dir.as_vector();
let cng = dir * (steps as i64);
pos += cng;

perimeter += steps as i64;
area += pos.x() * cng.y();
}

area + perimeter / 2 + 1
}

fn parse_b(input: &str) -> Vec<(Direction, u32)> {
input
.lines()
.map(|line| {
let hex = &line[line.find('#').unwrap() + 1..line.len() - 1];
let steps = u32::from_str_radix(&hex[0..5], 16).unwrap();
let dir = match &hex[5..6] {
"0" => Direction::Right,
"1" => Direction::Down,
"2" => Direction::Left,
"3" => Direction::Up,
_ => panic!("Invalid direction"),
};
(dir, steps)
})
.collect()
}

fn parse_a(input: &str) -> Vec<(Direction, u32)> {
input
.lines()
.map(|line| {
let mut parts = line.split_whitespace();
let dir = match parts.next().unwrap() {
"R" => Direction::Right,
"L" => Direction::Left,
"U" => Direction::Up,
"D" => Direction::Down,
_ => panic!("Invalid direction"),
};
let steps = parts.next().unwrap();
(dir, steps.parse().unwrap())
})
.collect()
}

#[cfg(test)]
mod test {
use common::Solution;
use indoc::indoc;

use super::Day18;

const CASE: &str = indoc! {"
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)
"};

#[test]
fn part_a() {
assert_eq!(Day18.part_a(CASE), 62.into());
}

#[test]
fn part_b() {
assert_eq!(Day18.part_b(CASE), 952408144115i64.into());
}
}
2 changes: 2 additions & 0 deletions aoc_2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod day_14;
mod day_15;
mod day_16;
mod day_17;
mod day_18;
// [import_marker]

#[rustfmt::skip]
Expand All @@ -40,5 +41,6 @@ pub const ALL: &[&dyn Solution] = &[
&day_15::Day15,
&day_16::Day16,
&day_17::Day17,
&day_18::Day18,
// [list_marker]
];

0 comments on commit f48eea7

Please sign in to comment.