Skip to content

Commit

Permalink
refactor: Implemented Problem day14
Browse files Browse the repository at this point in the history
  • Loading branch information
jortrr committed Aug 10, 2024
1 parent e676d15 commit e329ba6
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 159 deletions.
72 changes: 25 additions & 47 deletions .vscode/Rust.code-snippets
Original file line number Diff line number Diff line change
@@ -1,57 +1,35 @@
{
// Place your mounted workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"Ranged for-loop": {
"Implement AoC Problem": {
"scope": "rust",
"prefix": "ranged-for-loop",
"prefix": "prob",
"body": [
"for i in 0..=$1 {",
"//TODO: Your code here",
"println!(\"{}\", i);",
"}"
],
"description": "A ranged for-loop, inclusive"
},
"Advent of Code Fetch": {
"scope": "rust",
"prefix": "advent-of-code-fetch",
"body": [
"use aocf::Aoc;",
"struct $1 {}",
"",
"fn main() {",
" let mut aoc = Aoc::new().year(Some(2023)).day(Some($1)).init().unwrap();",
"impl Problem for $1 {",
"const YEAR: Year = $2;",
"const DAY: Day = $3;",
"const PART_ONE_EXAMPLE_EXPECTED: Answer = 0;",
"const PART_ONE_EXPECTED: Answer = 0;",
"const PART_TWO_EXAMPLE_EXPECTED: Answer = 0;",
"const PART_TWO_EXPECTED: Answer = 0;",
"",
" // Get input data (don't force)",
" let input = aoc.get_input(false);",
"fn example_input() -> ExampleInput {",
"\"",
"",
" if let Ok(i) = input {",
" println!(\"Input for day {:02}:\n\", aoc.day.unwrap());",
" println!(\"{}\", i);",
" }",
"\"",
"}",
"",
"fn solve_part_one(input: Input, _is_example: bool) -> Answer {",
"0",
"}",
"",
"fn solve_part_two(input: Input, _is_example: bool) -> Answer {",
"0",
"}",
"}",
"",
"run!($1);",
],
"description": "Insert the Advent of Code Fetch main function"
},
"For-each iter print": {
"scope": "rust",
"prefix": "for-each-iter-print",
"body": [
".for_each(|f| println!(\"{:?}\", f));"
],
"description": "Print each iterator element"
"description": "Implement AoC Problem"
},
}
245 changes: 133 additions & 112 deletions src/day14.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod macros;
mod problem;
use problem::*;

static NUMBER_OF_CYCLES: Int = 1000000000;

type Int = i32;
type Grid<T> = Vec<Vec<T>>;

#[derive(PartialEq, Debug, Clone)]
Expand Down Expand Up @@ -96,6 +99,10 @@ impl Debug for Platform {
}

impl Platform {
fn parse(input: Input) -> Platform {
Platform::from_strings(input.lines().map(|s| s.to_string()).collect::<Vec<_>>())
}

fn run_spin_cycle(&mut self) {
vec![North, West, South, East]
.iter()
Expand Down Expand Up @@ -248,11 +255,6 @@ impl Platform {
self.get_total_load()
}

fn from_string_slices(input: &Vec<&str>) -> Platform {
let input_strings = input.iter().map(|s| s.to_string()).collect();
Platform::from_strings(input_strings)
}

fn from_strings(input: Vec<String>) -> Platform {
let grid: Grid<Terrain> = input
.iter()
Expand Down Expand Up @@ -285,112 +287,131 @@ fn grid_to_string(grid: &Grid<Terrain>) -> String {
result
}

fn main() {
println!("Hello, World! from src/day14.rs!");
// Part 1 - Example
let example_input = vec![
"O....#....",
"O.OO#....#",
".....##...",
"OO.#O....O",
".O.....O#.",
"O.#..O.#.#",
"..O..#O..O",
".......O..",
"#....###..",
"#OO..#....",
];
let mut example_platform = Platform::from_string_slices(&example_input);
dbg!(&example_platform);
example_platform.tilt(North);
dbg!(&example_platform);
let example_total_load = example_platform.get_total_load();
test!(136, example_total_load);
let example_input_tilted = vec![
"OOOO.#.O..",
"OO..#....#",
"OO..O##..O",
"O..#.OO...",
"........#.",
"..#....#.#",
"..O..#.O.O",
"..O.......",
"#....###..",
"#....#....",
];
let example_platform_tilted = Platform::from_string_slices(&example_input_tilted);
//dbg!(&example_platform_tilted);
for y in 0..example_platform_tilted.rows {
for x in 0..example_platform_tilted.columns {
let point = Point::new(x as Int, y as Int);
let tilted_terrain = example_platform_tilted.get(&point).unwrap();
test!(
tilted_terrain,
example_platform.get(&point).unwrap(),
"Tilt: {:?}",
point
);
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_tilt_part_one() {
let mut example_platform = Platform::parse(DayFourteen::trimmed_example_input());
dbg!(&example_platform);
example_platform.tilt(North);
dbg!(&example_platform);
let example_total_load = example_platform.get_total_load();
test!(136, example_total_load);
let example_input_tilted = vec_of_strings![
"OOOO.#.O..",
"OO..#....#",
"OO..O##..O",
"O..#.OO...",
"........#.",
"..#....#.#",
"..O..#.O.O",
"..O.......",
"#....###..",
"#....#....",
];
let example_platform_tilted = Platform::from_strings(example_input_tilted);
//dbg!(&example_platform_tilted);
for y in 0..example_platform_tilted.rows {
for x in 0..example_platform_tilted.columns {
let point = Point::new(x as Int, y as Int);
let tilted_terrain = example_platform_tilted.get(&point).unwrap();
test!(
tilted_terrain,
example_platform.get(&point).unwrap(),
"Tilt: {:?}",
point
);
}
}
}
test!(136, example_total_load);

// Part 1
let mut platform = Platform::from_strings(aoc::get(2023, 14));
platform.tilt(North);
let total_load = platform.get_total_load();
test!(109098, total_load);

// Part 2 - Example
static NUMBER_OF_CYCLES: Int = 1000000000;
let mut example_platform = Platform::from_string_slices(&example_input);
let example_platform_1_cycle = Platform::from_string_slices(&vec![
".....#....",
"....#...O#",
"...OO##...",
".OO#......",
".....OOO#.",
".O#...O#.#",
"....O#....",
"......OOOO",
"#...O###..",
"#..OO#....",
]);
let example_platform_2_cycle = Platform::from_string_slices(&vec![
".....#....",
"....#...O#",
".....##...",
"..O#......",
".....OOO#.",
".O#...O#.#",
"....O#...O",
".......OOO",
"#..OO###..",
"#.OOO#...O",
]);
let example_platform_3_cycle = Platform::from_string_slices(&vec![
".....#....",
"....#...O#",
".....##...",
"..O#......",
".....OOO#.",
".O#...O#.#",
"....O#...O",
".......OOO",
"#...O###.O",
"#.OOO#...O",
]);
example_platform.run_spin_cycle();
test!(example_platform_1_cycle, example_platform);
example_platform.run_spin_cycle();
test!(example_platform_2_cycle, example_platform);
example_platform.run_spin_cycle();
test!(example_platform_3_cycle, example_platform);

let total_load = example_platform.get_total_load_after_cycles(NUMBER_OF_CYCLES - 3);
test!(64, total_load);

// Part 2
let total_load_after_many_cycles =
Platform::from_strings(aoc::get(2023, 14)).get_total_load_after_cycles(NUMBER_OF_CYCLES);
test!(100064, total_load_after_many_cycles);

#[test]
fn test_tilt_part_two() {
let mut example_platform = Platform::parse(DayFourteen::trimmed_example_input());
let example_platform_1_cycle = Platform::from_strings(vec_of_strings![
".....#....",
"....#...O#",
"...OO##...",
".OO#......",
".....OOO#.",
".O#...O#.#",
"....O#....",
"......OOOO",
"#...O###..",
"#..OO#....",
]);
let example_platform_2_cycle = Platform::from_strings(vec_of_strings![
".....#....",
"....#...O#",
".....##...",
"..O#......",
".....OOO#.",
".O#...O#.#",
"....O#...O",
".......OOO",
"#..OO###..",
"#.OOO#...O",
]);
let example_platform_3_cycle = Platform::from_strings(vec_of_strings![
".....#....",
"....#...O#",
".....##...",
"..O#......",
".....OOO#.",
".O#...O#.#",
"....O#...O",
".......OOO",
"#...O###.O",
"#.OOO#...O",
]);
example_platform.run_spin_cycle();
test!(example_platform_1_cycle, example_platform);
example_platform.run_spin_cycle();
test!(example_platform_2_cycle, example_platform);
example_platform.run_spin_cycle();
test!(example_platform_3_cycle, example_platform);
}
}

struct DayFourteen {}

impl Problem for DayFourteen {
const YEAR: Year = 2023;
const DAY: Day = 14;
const PART_ONE_EXAMPLE_EXPECTED: Answer = 136;
const PART_ONE_EXPECTED: Answer = 109098;
const PART_TWO_EXAMPLE_EXPECTED: Answer = 64;
const PART_TWO_EXPECTED: Answer = 100064;

fn example_input() -> ExampleInput {
"
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
"
}

fn solve_part_one(input: Input, _is_example: bool) -> Answer {
let mut platform = Platform::parse(input);
platform.tilt(North);
let total_load = platform.get_total_load();
total_load
}

fn solve_part_two(input: Input, _is_example: bool) -> Answer {
let total_load_after_many_cycles =
Platform::parse(input).get_total_load_after_cycles(NUMBER_OF_CYCLES);
total_load_after_many_cycles
}
}

run!(DayFourteen);

0 comments on commit e329ba6

Please sign in to comment.