diff options
Diffstat (limited to 'src/bin')
-rw-r--r-- | src/bin/day_1.rs | 70 |
1 files changed, 67 insertions, 3 deletions
diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs index 3b80d88..68a570c 100644 --- a/src/bin/day_1.rs +++ b/src/bin/day_1.rs @@ -1,4 +1,6 @@ use bevy::prelude::*; +use std::fs::File; +use std::io::{BufRead, BufReader}; fn main() { App::build() @@ -9,11 +11,73 @@ fn main() { ..Default::default() }) .add_resource(ClearColor(Color::rgb(0., 0., 0.))) - .add_startup_system(setup.system()) - .add_plugins(DefaultPlugins) + .add_startup_system(setup_camera.system()) + .add_startup_system(read_input_file.system()) + .add_stage("mark") + .add_system_to_stage("mark", mark_sum_pair.system()) + .add_system_to_stage("mark", mark_sum_triple.system()) + .add_stage("report") + .add_system_to_stage("report", print_product_pair.system()) + .add_system_to_stage("report", print_product_triple.system()) + //.add_plugins(DefaultPlugins) .run(); } -fn setup(mut commands: Commands) { +fn setup_camera(mut commands: Commands) { commands.spawn(Camera2dComponents::default()); } + +struct Expense(i32); +struct FlaggedPair; +struct FlaggedTriple; + +fn read_input_file(mut commands: Commands) { + let f = File::open("./inputs/day_1.txt").unwrap(); // TODO: Error handling on systems? + for line in BufReader::new(f).lines() { + let expense = line.unwrap().trim().parse().unwrap(); + commands.spawn((Expense(expense),)); + } +} + +fn mark_sum_pair(mut commands: Commands, expenses: Query<(Entity, &Expense)>) { + for (entity_1, expense_1) in expenses.iter() { + for (entity_2, expense_2) in expenses.iter() { + if expense_1.0 + expense_2.0 == 2020 { + commands.insert_one(entity_1, FlaggedPair); + commands.insert_one(entity_2, FlaggedPair); + } + } + } +} + +fn mark_sum_triple(mut commands: Commands, expenses: Query<(Entity, &Expense)>) { + for (entity_1, expense_1) in expenses.iter() { + for (entity_2, expense_2) in expenses.iter() { + for (entity_3, expense_3) in expenses.iter() { + if expense_1.0 + expense_2.0 + expense_3.0 == 2020 { + commands.insert_one(entity_1, FlaggedTriple); + commands.insert_one(entity_2, FlaggedTriple); + commands.insert_one(entity_3, FlaggedTriple); + } + } + } + } +} + +fn print_product_pair(flagged_expenses: Query<With<FlaggedPair, &Expense>>) { + let mut product = 1; + for expense in flagged_expenses.iter() { + product *= expense.0; + } + // TODO: Put it on the screen! + println!("Product Pair: {}", product); +} + +fn print_product_triple(flagged_expenses: Query<With<FlaggedTriple, &Expense>>) { + let mut product = 1; + for expense in flagged_expenses.iter() { + product *= expense.0; + } + // TODO: Put it on the screen! + println!("Product Triple: {}", product); +} |