Skip to content

Commit

Permalink
add build.rs and input.txt to template
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsella committed Dec 11, 2023
1 parent b405369 commit 99fbe4c
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 2023/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
resolver = "2"
# exclude = ["day11"]
members = [
"day1",
"day2",
Expand All @@ -11,7 +12,7 @@ members = [
"day8",
"day9",
"day10",
# "day11",
"day11",
# "day12",
# "day13",
# "day14",
Expand Down
19 changes: 19 additions & 0 deletions 2023/day11/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
input.txt
flamegraph.svg
perf.data*
### Rust
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

10 changes: 10 additions & 0 deletions 2023/day11/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "day11"
authors = ["mirsella <mirsella@protonmail.com>"]
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

25 changes: 25 additions & 0 deletions 2023/day11/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;
use std::{env, fs};

fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> {
let mut contents = String::new();
File::open(file_path)?.read_to_string(&mut contents)?;
let new_contents = contents.replace(old, new);
fs::write(file_path, new_contents)?;
Ok(())
}

fn main() -> io::Result<()> {
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
replace_in_file(
&"../Cargo.toml".into(),
&format!("# \"{pkg_name}\""),
&format!("\"{pkg_name}\""),
)?;

replace_in_file(&"./Cargo.toml".into(), "\n[workspace]", "")?;

Ok(())
}
98 changes: 98 additions & 0 deletions 2023/day11/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#[derive(PartialEq)]
struct Galaxy {
row: usize,
col: usize,
}
struct Grid(pub Vec<Vec<char>>);
impl Grid {
fn parse(input: &str) -> Self {
let data: Vec<Vec<_>> = input.lines().map(|l| l.chars().collect()).collect();
Self(data)
}
fn expand_galaxy(&self, g: &mut Galaxy, factor: usize) {
let mut row_expansion = 0;
for row in 0..g.row {
if !self.0[row].iter().any(|&c| c == '#') {
row_expansion += 1
}
}
let mut col_expansion = 0;
for col in 0..g.col {
if !(0..self.0.len()).any(|i| self.0[i][col] == '#') {
col_expansion += 1;
}
}
g.row += row_expansion * factor - row_expansion;
g.col += col_expansion * factor - col_expansion;
}
fn get_galaxys(&self, expansion: usize) -> Vec<Galaxy> {
let mut galaxys = Vec::new();
for row in 0..self.0.len() {
for col in 0..self.0[0].len() {
if self.0[row][col] == '#' {
let mut g = Galaxy { row, col };
self.expand_galaxy(&mut g, expansion);
galaxys.push(g);
}
}
}
galaxys
}
}
fn part1(input: &str) -> usize {
let grid = Grid::parse(input);
let galaxys = grid.get_galaxys(2);
let mut count = 0;
for galaxy in &galaxys {
for other in galaxys.iter().skip_while(|&g| g != galaxy) {
let shortest_path = (galaxy.row as isize - other.row as isize).abs()
+ (galaxy.col as isize - other.col as isize).abs();
count += shortest_path as usize;
}
}
count
}
fn part2(input: &str, expansion: usize) -> usize {
let grid = Grid::parse(input);
let galaxys = grid.get_galaxys(expansion);
let mut count = 0;
for galaxy in &galaxys {
for other in galaxys.iter().skip_while(|&g| g != galaxy) {
let shortest_path = (galaxy.row as isize - other.row as isize).abs()
+ (galaxy.col as isize - other.col as isize).abs();
count += shortest_path as usize;
}
}
count
}
fn main() {
let input = include_str!("../input.txt");
println!("Part 1: {}", part1(input));
println!("Part 2: {}", part2(input, 1000000));
}

#[cfg(test)]
mod tests {
const INPUT: &str = "...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....";
#[test]
fn part1() {
assert_eq!(super::part1(INPUT), 374);
}
#[test]
fn part2_1() {
assert_eq!(super::part2(INPUT, 10), 1030);
}
#[test]
fn part2_2() {
assert_eq!(super::part2(INPUT, 100), 8410);
}
}
25 changes: 25 additions & 0 deletions template/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::fs::File;
use std::io::{self, Read};
use std::path::PathBuf;
use std::{env, fs};

fn replace_in_file(file_path: &PathBuf, old: &str, new: &str) -> io::Result<()> {
let mut contents = String::new();
File::open(file_path)?.read_to_string(&mut contents)?;
let new_contents = contents.replace(old, new);
fs::write(file_path, new_contents)?;
Ok(())
}

fn main() -> io::Result<()> {
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
replace_in_file(
&"../Cargo.toml".into(),
&format!("# \"{pkg_name}\""),
&format!("\"{pkg_name}\""),
)?;

replace_in_file(&"./Cargo.toml".into(), "\n[workspace]", "")?;

Ok(())
}
Empty file added template/input.txt
Empty file.

0 comments on commit 99fbe4c

Please sign in to comment.