summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-12-12 19:30:30 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-12-12 19:30:30 +0200
commita33020141fe1439209dd46e8caf29144bd4463f8 (patch)
treeafa309cc7d0881da49eb632c01286cbb1f146f12
parent6efb8d0afb70e3fbd06dca5ec4e2bd8283b617a0 (diff)
Day 10
-rw-r--r--inputs/day_10.txt111
-rw-r--r--src/bin/day_10.rs124
2 files changed, 235 insertions, 0 deletions
diff --git a/inputs/day_10.txt b/inputs/day_10.txt
new file mode 100644
index 0000000..fa8ea01
--- /dev/null
+++ b/inputs/day_10.txt
@@ -0,0 +1,111 @@
+71
+183
+111
+89
+92
+142
+25
+101
+52
+86
+18
+22
+70
+2
+135
+163
+34
+143
+153
+35
+144
+24
+23
+94
+100
+102
+17
+57
+76
+182
+134
+38
+7
+103
+66
+31
+11
+121
+77
+113
+128
+82
+99
+148
+137
+41
+32
+48
+131
+60
+127
+138
+73
+28
+10
+84
+180
+63
+125
+53
+176
+165
+114
+145
+152
+72
+107
+167
+59
+164
+78
+126
+118
+136
+83
+79
+58
+14
+106
+69
+51
+39
+157
+42
+177
+173
+93
+141
+3
+33
+13
+19
+45
+154
+95
+170
+54
+181
+6
+151
+1
+112
+96
+115
+85
+108
+166
+160
+40
+122
+12
diff --git a/src/bin/day_10.rs b/src/bin/day_10.rs
new file mode 100644
index 0000000..7ef4fa2
--- /dev/null
+++ b/src/bin/day_10.rs
@@ -0,0 +1,124 @@
+use bevy::{app::AppExit, prelude::*};
+use std::io::{BufRead, BufReader};
+use std::{collections::BTreeMap, fs::File};
+
+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_startup_stage("differences")
+ .add_startup_system_to_stage("differences", add_min_differences.system())
+ .add_startup_system_to_stage("differences", add_all_differences.system())
+ .add_startup_stage("report")
+ .add_startup_system_to_stage("report", print_difference_stats.system())
+ .add_system(add_next_paths_to_end_sum.system())
+ .add_system(report_full_paths_to_end.system())
+ .add_plugins(DefaultPlugins)
+ .run();
+}
+
+fn setup_camera(mut commands: Commands) {
+ commands.spawn(Camera2dComponents::default());
+}
+
+#[derive(PartialEq, Eq, PartialOrd, Ord)]
+struct MinJoltDiff(i64);
+#[derive(Clone)]
+struct NextJolts(Vec<Entity>);
+struct Jolts(i64);
+struct PathsToEnd(u64);
+
+fn read_input_file(mut commands: Commands) {
+ let f = File::open("./inputs/day_10.txt").unwrap();
+
+ commands.spawn((Jolts(0),));
+ for line in BufReader::new(f).lines() {
+ let line = line.unwrap();
+ let line = line.trim();
+ let num = line.parse().unwrap();
+
+ commands.spawn((Jolts(num),));
+ }
+}
+
+fn add_min_differences(
+ mut commands: Commands,
+ jolts_query: Query<(Entity, &Jolts, Option<&MinJoltDiff>)>,
+) {
+ for (entity, jolts, jolt_diff) in jolts_query.iter() {
+ if jolt_diff.is_none() {
+ let min_increase = jolts_query
+ .iter()
+ .map(|(_, other_jolts, _)| other_jolts)
+ .filter(|other_jolts| other_jolts.0 > jolts.0)
+ .map(|other_jolts| MinJoltDiff(other_jolts.0 - jolts.0))
+ .min()
+ .unwrap_or(MinJoltDiff(3));
+ commands.insert_one(entity, min_increase);
+ }
+ }
+}
+
+fn add_all_differences(mut commands: Commands, jolts_query: Query<(Entity, &Jolts)>) {
+ for (entity, jolts) in jolts_query.iter() {
+ let next_entities = jolts_query
+ .iter()
+ .filter(|(_, other_jolts)| other_jolts.0 > jolts.0 && other_jolts.0 - jolts.0 <= 3)
+ .map(|(other_entity, _)| other_entity)
+ .collect();
+ commands.insert_one(entity, NextJolts(next_entities));
+ }
+}
+
+fn print_difference_stats(diffs: Query<&MinJoltDiff>) {
+ let mut buckets: BTreeMap<i64, usize> = BTreeMap::new();
+ for diff in diffs.iter() {
+ *buckets.entry(diff.0).or_insert(0) += 1;
+ }
+ println!("Buckets: {:?}", buckets);
+
+ println!(
+ "Bucket 1 * Bucket 3: {}",
+ buckets.get(&1).unwrap_or(&0) * buckets.get(&3).unwrap_or(&0)
+ );
+}
+
+fn add_next_paths_to_end_sum(
+ mut commands: Commands,
+ potential_to_add: Query<Without<PathsToEnd, (Entity, &NextJolts)>>,
+ already_added: Query<&PathsToEnd>,
+) {
+ for (entity, next) in potential_to_add.iter() {
+ if next.0.is_empty() {
+ commands.insert_one(entity, PathsToEnd(1));
+ } else {
+ let next_paths: Result<Vec<&PathsToEnd>, _> = next
+ .0
+ .iter()
+ .map(|next_entity| already_added.get(*next_entity))
+ .collect();
+ if let Ok(next_paths) = next_paths {
+ let sum = next_paths.iter().map(|paths| paths.0).sum();
+ commands.insert_one(entity, PathsToEnd(sum));
+ }
+ }
+ }
+}
+
+fn report_full_paths_to_end(
+ mut exit_events: ResMut<Events<AppExit>>,
+ jolts: &Jolts,
+ paths: &PathsToEnd,
+) {
+ if jolts.0 == 0 {
+ println!("Paths from the start to the end: {}", paths.0);
+ exit_events.send(AppExit);
+ }
+}