Change repo layout
This commit is contained in:
46
src/day1.rs
Normal file
46
src/day1.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
#[aoc_generator(day1)]
|
||||
pub fn parse_input(input: &str) -> Vec<String> {
|
||||
input.split('\n')
|
||||
.map(|line| line.into())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[aoc(day1, part1)]
|
||||
fn solve_part1(input: &[String]) -> u32 {
|
||||
let mut sum: u32 = 0;
|
||||
|
||||
for line in input {
|
||||
let chars: Vec<_> = line.chars().filter(|char| char.is_numeric()).collect();
|
||||
let num = chars.first().unwrap().to_string() + &chars.last().unwrap().to_string();
|
||||
let num: u32 = num.parse().unwrap();
|
||||
sum += num;
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
#[aoc(day1, part2)]
|
||||
fn solve_part2(input: &[String]) -> u32 {
|
||||
let mut sum: u32 = 0;
|
||||
|
||||
for line in input {
|
||||
let str_nums: Vec<(&str, &str)> = vec![("one", "1"), ("two", "2"), ("three", "3"), ("four", "4"), ("five", "5"), ("six", "6"), ("seven", "7"), ("eight", "8"), ("nine", "9")];
|
||||
let mut matches: Vec<(usize, &str)> = vec![];
|
||||
for str_num in str_nums {
|
||||
// Get every alphabetic number in the string with it's index
|
||||
let str_match: Vec<_> = line.match_indices(str_num.0).collect();
|
||||
// convert the string to a numeral
|
||||
let mut str_match: Vec<_> = str_match.iter().map(|x| return (x.0, str_num.1)).collect();
|
||||
matches.append(&mut str_match);
|
||||
}
|
||||
// get the numerials from the line with their index
|
||||
let mut num_matches: Vec<(usize, &str)> = line.match_indices(|x: char| x.is_numeric()).collect();
|
||||
matches.append(&mut num_matches);
|
||||
// sort by index
|
||||
matches.sort_by(|lhs, rhs| lhs.cmp(rhs));
|
||||
let num = (matches.first().unwrap().1).to_owned() + (matches.last().unwrap().1);
|
||||
sum += num.parse::<u32>().unwrap();
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
114
src/day2.rs
Normal file
114
src/day2.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
use itertools::Itertools;
|
||||
use std::cmp::max;
|
||||
|
||||
#[aoc_generator(day2)]
|
||||
fn parse_input(input: &str) -> Vec<Vec<Vec<(i32, Color)>>> {
|
||||
input.split('\n')
|
||||
.map(|line| {
|
||||
let rounds = &line[line.find(':').unwrap() + 1..];
|
||||
rounds.split(';')
|
||||
.map(|game| {
|
||||
game.split(',')
|
||||
.map(|round| {
|
||||
let (number, color) = round.split_whitespace().collect_tuple().unwrap();
|
||||
let number = number.parse::<i32>().unwrap();
|
||||
(number, color.into_color())
|
||||
}).collect::<Vec<_>>()
|
||||
}).collect::<Vec<_>>()
|
||||
}).collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
#[aoc(day2, part1)]
|
||||
fn solve_part1(input: &Vec<Vec<Vec<(i32, Color)>>>) -> usize {
|
||||
let (r_max, g_max, b_max) = (12, 13, 14);
|
||||
let mut sum: usize = 0;
|
||||
|
||||
for (game_id, rounds) in input.iter().enumerate() {
|
||||
let mut sad = false;
|
||||
|
||||
for round in rounds {
|
||||
let (mut r_cur, mut g_cur, mut b_cur) = (0, 0, 0);
|
||||
for (num, color) in round {
|
||||
match color {
|
||||
Color::Red => r_cur += num,
|
||||
Color::Blue => b_cur += num,
|
||||
Color::Green => g_cur += num
|
||||
}
|
||||
}
|
||||
|
||||
if r_cur > r_max || b_cur > b_max || g_cur > g_max {
|
||||
sad = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !sad {
|
||||
sum += game_id + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
#[aoc(day2, part2)]
|
||||
fn solve_part2(input: &Vec<Vec<Vec<(i32, Color)>>>) -> usize {
|
||||
let mut sum: usize = 0;
|
||||
|
||||
for rounds in input {
|
||||
let (mut r_max, mut g_max, mut b_max) = (0, 0, 0);
|
||||
for round in rounds {
|
||||
for (num, color) in round {
|
||||
match color {
|
||||
Color::Red => r_max = max(*num, r_max),
|
||||
Color::Blue => b_max = max(*num, b_max),
|
||||
Color::Green => g_max = max(*num, g_max),
|
||||
}
|
||||
}
|
||||
}
|
||||
sum += (r_max * g_max * b_max) as usize;
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Color {
|
||||
Red,
|
||||
Blue,
|
||||
Green
|
||||
}
|
||||
|
||||
trait ColorConvertable {
|
||||
fn into_color(&self) -> Color;
|
||||
}
|
||||
|
||||
impl ColorConvertable for str {
|
||||
fn into_color(&self) -> Color {
|
||||
match self {
|
||||
"red" => Color::Red,
|
||||
"blue" => Color::Blue,
|
||||
"green" => Color::Green,
|
||||
_ => panic!("Invalid Color")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
static TEST_INPUT: &str = r"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green";
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
assert_eq!(solve_part1(&parse_input(TEST_INPUT)), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
assert_eq!(solve_part2(&parse_input(TEST_INPUT)), 2286);
|
||||
}
|
||||
}
|
31
src/day4.rs
Normal file
31
src/day4.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
#[aoc_generator(day4)]
|
||||
fn parse(input: &str) -> String {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[aoc(day4, part1)]
|
||||
fn part1(input: &str) -> String {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[aoc(day4, part2)]
|
||||
fn part2(input: &str) -> String {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
assert_eq!(part1(&parse("<EXAMPLE>")), "<RESULT>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_example() {
|
||||
assert_eq!(part2(&parse("<EXAMPLE>")), "<RESULT>");
|
||||
}
|
||||
}
|
11
src/lib.rs
Normal file
11
src/lib.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
mod day4;
|
||||
mod day2;
|
||||
mod day1;
|
||||
|
||||
extern crate aoc_runner;
|
||||
|
||||
#[macro_use]
|
||||
extern crate aoc_runner_derive;
|
||||
extern crate crypto;
|
||||
|
||||
aoc_lib!{ year = 2023 }
|
7
src/main.rs
Normal file
7
src/main.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
extern crate advent_of_code_2023;
|
||||
extern crate aoc_runner_derive;
|
||||
extern crate aoc_runner;
|
||||
|
||||
use aoc_runner_derive::aoc_main;
|
||||
|
||||
aoc_main! { lib = advent_of_code_2023 }
|
Reference in New Issue
Block a user