day14 part1

This commit is contained in:
Andrew Glaze 2023-12-14 14:31:43 -05:00
parent ffb43d4860
commit 97d8acbad8
2 changed files with 90 additions and 0 deletions

89
src/day14.rs Normal file
View File

@ -0,0 +1,89 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Debug)]
enum Rock {
Round,
Square,
None
}
trait RockConvertable {
fn to_rock(&self) -> Rock;
}
impl RockConvertable for char {
fn to_rock(&self) -> Rock {
match self {
'O' => Rock::Round,
'#' => Rock::Square,
'.' => Rock::None,
_ => panic!("Invalid rock char")
}
}
}
#[aoc_generator(day14)]
fn parse(input: &str) -> Vec<Vec<Rock>> {
let input: Vec<Vec<Rock>> = input.lines()
.map(|line| {
line.chars().map(|char| {
char.to_rock()
}).collect::<Vec<_>>()
}).collect::<Vec<_>>();
input
}
#[aoc(day14, part1)]
fn part1(input: &Vec<Vec<Rock>>) -> usize {
let mut load: usize = 0;
for j in 0..input[0].len() {
let mut cur_weight = input[0].len() + 1;
for i in 0..input.len() {
match input[i][j] {
Rock::Round => {
cur_weight -= 1;
load += cur_weight;
//println!("{}", cur_weight);
},
Rock::Square => {
//println!("{}", input[0].len() - i);
cur_weight = input[0].len() - i;
},
Rock::None => continue
}
}
}
load
}
// #[aoc(day14, part2)]
// fn part2(input: &Vec<Vec<Rock>>) -> String {
// todo!()
// }
#[cfg(test)]
mod tests {
use super::*;
const EX: &str = r"O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....";
#[test]
fn part1_example() {
assert_eq!(part1(&parse(EX)), 136);
}
// #[test]
// fn part2_example() {
// assert_eq!(part2(&parse(EX)), "<RESULT>");
// }
}

View File

@ -1,3 +1,4 @@
mod day14;
mod day13;
mod day12;
mod day4;