1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
use bevy::prelude::*;
use std::fs::File;
use std::io::{BufRead, BufReader};
fn main() {
App::build()
.add_resource(WindowDescriptor {
title: "Advent of Code".to_string(),
width: 1920,
height: 1080,
..Default::default()
})
.add_resource(ClearColor(Color::rgb(0., 0., 0.)))
.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_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);
}
|