Skip to content

Commit

Permalink
Day 18: Lavaduct Lagoon
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 18, 2023
1 parent 4993a07 commit f2705f7
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Development occurs in language-specific directories:
|[Day15.hs](hs/src/Day15.hs)|[Day15.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day15.kt)|[day15.py](py/aoc2023/day15.py)|[day15.rs](rs/src/day15.rs)|
|[Day16.hs](hs/src/Day16.hs)|[Day16.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day16.kt)|[day16.py](py/aoc2023/day16.py)|[day16.rs](rs/src/day16.rs)|
|[Day17.hs](hs/src/Day17.hs)|[Day17.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day17.kt)|[day17.py](py/aoc2023/day17.py)|[day17.rs](rs/src/day17.rs)|
|[Day18.hs](hs/src/Day18.hs)|[Day18.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day18.kt)|[day18.py](py/aoc2023/day18.py)||
|[Day18.hs](hs/src/Day18.hs)|[Day18.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day18.kt)|[day18.py](py/aoc2023/day18.py)|[day18.rs](rs/src/day18.rs)|
10 changes: 8 additions & 2 deletions rs/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use aoc2023::{
day1, day10, day11, day12, day13, day14, day15, day16, day17, day2, day3, day4, day5, day6,
day7, day8, day9,
day1, day10, day11, day12, day13, day14, day15, day16, day17, day18, day2, day3, day4, day5,
day6, day7, day8, day9,
};
use criterion::{black_box, Criterion};
use std::env;
Expand Down Expand Up @@ -119,6 +119,12 @@ fn aoc2023_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day17::part2(black_box(&data))));
g.finish();

let data = get_day_input(18)?;
let mut g = c.benchmark_group("day 18");
g.bench_function("part 1", |b| b.iter(|| day18::part1(black_box(&data))));
g.bench_function("part 2", |b| b.iter(|| day18::part2(black_box(&data))));
g.finish();

Ok(())
}

Expand Down
104 changes: 104 additions & 0 deletions rs/src/day18.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
enum Direction {
R,
D,
L,
U,
}

fn solve<I: IntoIterator<Item = (Direction, u32)>>(data: I) -> u64 {
let (mut x, mut y, mut a, mut l) = (0, 0, 0, 0);
for (d, n) in data {
match d {
Direction::R => {
x += i64::from(n);
a += y * i64::from(n);
}
Direction::D => {
y += i64::from(n);
}
Direction::L => {
x -= i64::from(n);
a -= y * i64::from(n);
}
Direction::U => {
y -= i64::from(n);
}
}
l += u64::from(n);
}
debug_assert_eq!((x, y), (0, 0));
a.unsigned_abs() + l / 2 + 1
}

pub fn part1(data: &str) -> u64 {
solve(data.lines().filter_map(|line| {
let mut iter = line.split(' ');
let d = iter.next()?;
let d = if d == "R" {
Direction::R
} else if d == "D" {
Direction::D
} else if d == "L" {
Direction::L
} else if d == "U" {
Direction::U
} else {
return None;
};
Some((d, iter.next()?.parse::<u32>().ok()?))
}))
}

pub fn part2(data: &str) -> u64 {
solve(data.lines().filter_map(|line| {
let line = line
.split(' ')
.next_back()?
.strip_prefix("(#")?
.strip_suffix(')')?;
let d = match line.bytes().last()? {
b'0' => Direction::R,
b'1' => Direction::D,
b'2' => Direction::L,
b'3' => Direction::U,
_ => {
return None;
}
};
Some((d, u32::from_str_radix(&line[..line.len() - 1], 16).ok()?))
}))
}

#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;

static EXAMPLE: &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 part1_examples() {
assert_eq!(62, part1(EXAMPLE));
}

#[test]
fn part2_examples() {
assert_eq!(952408144115, part2(EXAMPLE));
}
}
1 change: 1 addition & 0 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod day14;
pub mod day15;
pub mod day16;
pub mod day17;
pub mod day18;
pub mod day2;
pub mod day3;
pub mod day4;
Expand Down
12 changes: 10 additions & 2 deletions rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use aoc2023::{
day1, day10, day11, day12, day13, day14, day15, day16, day17, day2, day3, day4, day5, day6,
day7, day8, day9,
day1, day10, day11, day12, day13, day14, day15, day16, day17, day18, day2, day3, day4, day5,
day6, day7, day8, day9,
};
use std::collections::HashSet;
use std::env;
Expand Down Expand Up @@ -155,5 +155,13 @@ fn main() -> io::Result<()> {
println!();
}

if args.is_empty() || args.contains("18") {
println!("Day 18");
let data = get_day_input(18)?;
println!("{:?}", day18::part1(&data));
println!("{:?}", day18::part2(&data));
println!();
}

Ok(())
}

0 comments on commit f2705f7

Please sign in to comment.