Skip to content

Commit

Permalink
Year 2016: Day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Mar 19, 2024
1 parent 2c184ad commit aa49f11
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Of note:
- The changelog 2015.5.2 has been rewritten from each commit content.
- This file may be amended entirely in the future to adhere to the [GNU Changelog style](https://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html#Style-of-Change-Logs)

## [2016.13.1]
### Added
- Solved [exercice for 2016, day 13](src/year_2016/day_13.rs).

## [2016.12.1]
### Added
- Solved [exercice for 2016, day 12](src/year_2016/day_12.rs).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "advent-rs"
version = "2016.12.1"
version = "2016.13.1"
edition = "2021"
authors = ["Arnaud 'red' Rouyer"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ As I said, [Consistency is hard](https://github.com/joshleaves/advent-rb), and I

I'm also adding notes that may be useful if you're (like me) discovering Rust:
- [2015, complete!](NOTES_2015.md)
- [2016, up to day 12](NOTES_2016.md)
- [2016, up to day 13](NOTES_2016.md)

# Regarding style rules
I'm gonna use a mix of what `cargo fmt` does, with some stuff that feels more natural to me.
Expand Down
44 changes: 43 additions & 1 deletion benches/year_2016.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use advent_rs::year_2016::day_07;
use advent_rs::year_2016::day_08;
use advent_rs::year_2016::day_09;
use advent_rs::year_2016::day_10;
use advent_rs::year_2016::day_11;
use advent_rs::year_2016::day_12;
use advent_rs::year_2016::day_13;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

pub fn year_2016_day_01(c: &mut Criterion) {
Expand Down Expand Up @@ -130,6 +133,42 @@ pub fn year_2016_day_10(c: &mut Criterion) {
g2016_day_10.finish();
}

pub fn year_2016_day_11(c: &mut Criterion) {
let mut g2016_day_11 = c.benchmark_group("year_2016::day_11");
let input_year_2016_day_11 = include_str!("../inputs/year_2016/day_11_input");
g2016_day_11.bench_function("year_2016::day_11_v1", |b| {
b.iter(|| day_11::day_11_v1(black_box(input_year_2016_day_11)))
});
g2016_day_11.bench_function("year_2016::day_11_v2", |b| {
b.iter(|| day_11::day_11_v2(black_box(input_year_2016_day_11)))
});
g2016_day_11.finish();
}

pub fn year_2016_day_12(c: &mut Criterion) {
let mut g2016_day_12 = c.benchmark_group("year_2016::day_12");
let input_year_2016_day_12 = include_str!("../inputs/year_2016/day_12_input");
g2016_day_12.bench_function("year_2016::day_12_v1", |b| {
b.iter(|| day_12::day_12_v1(black_box(input_year_2016_day_12)))
});
g2016_day_12.bench_function("year_2016::day_12_v2", |b| {
b.iter(|| day_12::day_12_v2(black_box(input_year_2016_day_12)))
});
g2016_day_12.finish();
}

pub fn year_2016_day_13(c: &mut Criterion) {
let mut g2016_day_13 = c.benchmark_group("year_2016::day_13");
let input_year_2016_day_13 = include_str!("../inputs/year_2016/day_13_input");
g2016_day_13.bench_function("year_2016::day_13_v1", |b| {
b.iter(|| day_13::day_13_v1(black_box(input_year_2016_day_13)))
});
g2016_day_13.bench_function("year_2016::day_13_v2", |b| {
b.iter(|| day_13::day_13_v2(black_box(input_year_2016_day_13)))
});
g2016_day_13.finish();
}

criterion_group!(
benches,
year_2016_day_01,
Expand All @@ -141,6 +180,9 @@ criterion_group!(
year_2016_day_07,
year_2016_day_08,
year_2016_day_09,
year_2016_day_10
year_2016_day_10,
year_2016_day_11,
year_2016_day_12,
year_2016_day_13
);
criterion_main!(benches);
1 change: 1 addition & 0 deletions inputs/year_2016/day_13_input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1352,31,39
9 changes: 9 additions & 0 deletions src/year_2016.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod day_09;
pub mod day_10;
pub mod day_11;
pub mod day_12;
pub mod day_13;

pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part != 1 && part != 2 {
Expand All @@ -34,6 +35,7 @@ pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
10 => Some(format!("{}", day_10::day_10(part, input))),
11 => Some(format!("{}", day_11::day_11(part, input))),
12 => Some(format!("{}", day_12::day_12(part, input))),
13 => Some(format!("{}", day_13::day_13(part, input))),
_ => None,
}
}
Expand Down Expand Up @@ -133,4 +135,11 @@ mod tests {
assert_eq!(day_12::day_12_v1(input), 318_007);
assert_eq!(day_12::day_12_v2(input), 9_227_661);
}

#[test]
fn day_13() {
let input = include_str!("../inputs/year_2016/day_13_input");
assert_eq!(day_13::day_13_v1(input), 90);
assert_eq!(day_13::day_13_v2(input), 135);
}
}
1 change: 0 additions & 1 deletion src/year_2016/day_11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ fn next_moves(current_state: Vec<u8>) -> Vec<Vec<u8>> {
}

fn search(states: Vec<Vec<u8>>, mut moves_tried: HashSet<Vec<u8>>, depth: u16) -> u16 {
// println!("NEXT MOVES ({depth}) => {}", states.len());
if states
.iter()
.any(|state| state.iter().all(|floor| *floor == 3))
Expand Down
98 changes: 98 additions & 0 deletions src/year_2016/day_13.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use std::collections::HashSet;
use std::collections::VecDeque;

enum PositionOrCount {
Position((usize, usize)),
Count(u8),
}

impl PositionOrCount {
fn reached_position(&self, reach: (usize, usize)) -> bool {
match self {
PositionOrCount::Position(target) => *target == reach,
PositionOrCount::Count(_) => false,
}
}

fn reached_count(&self, steps_count: u8) -> bool {
match self {
PositionOrCount::Position(_) => false,
PositionOrCount::Count(count) => steps_count > *count,
}
}
}

fn cell_is_wall(favorite: usize, position: (usize, usize)) -> bool {
let x = position.0;
let y = position.1;
let value = x * x + 3 * x + 2 * x * y + y + y * y + favorite;
return value.count_ones() % 2 == 1;
}

fn next_moves(position: (usize, usize)) -> Vec<(usize, usize)> {
let mut new_moves: Vec<(usize, usize)> = vec![];
if position.0 > 0 {
new_moves.push((position.0 - 1, position.1));
}
new_moves.push((position.0 + 1, position.1));
if position.1 > 0 {
new_moves.push((position.0, position.1 - 1));
}
new_moves.push((position.0, position.1 + 1));

new_moves
}

fn traverse_until(favorite: usize, reach_condition: PositionOrCount) -> u8 {
let mut visited: HashSet<(usize, usize)> = HashSet::from([(1, 1)]);
let mut queue: VecDeque<((usize, usize), u8)> = VecDeque::from([((1, 1), 0)]);

while let Some((position, depth)) = queue.pop_front() {
if reach_condition.reached_position(position) {
return depth;
}
if reach_condition.reached_count(depth) {
return visited.len() as u8;
}

visited.insert(position);
for next_move in next_moves(position) {
if visited.contains(&next_move) || cell_is_wall(favorite, next_move) {
continue;
}
queue.push_back((next_move, depth + 1));
}
}

0
}

pub fn day_13_v1(input: impl Into<String>) -> u8 {
let binding = input.into();
let input_parts: Vec<_> = binding.lines().next().unwrap().split(",").collect();
let favorite = input_parts[0].parse::<usize>().unwrap();
let reach_x = input_parts[1].parse::<usize>().unwrap();
let reach_y = input_parts[2].parse::<usize>().unwrap();

traverse_until(favorite, PositionOrCount::Position((reach_x, reach_y)))
}

pub fn day_13_v2(input: impl Into<String>) -> u8 {
let binding = input.into();
let input_parts: Vec<_> = binding.split(",").collect();
let favorite = input_parts[0].parse::<usize>().unwrap();

traverse_until(favorite, PositionOrCount::Count(50))
}

solvable!(day_13, day_13_v1, day_13_v2, u8);

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

#[test]
fn works_with_samples_v1() {
assert_eq!(day_13_v1("10,7,4"), 11);
}
}

0 comments on commit aa49f11

Please sign in to comment.