From 60f0fce822527372135ead5ca2a4843ee9b8d31c Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 8 Dec 2020 20:56:43 +0200 Subject: Improvement! --- src/bin/day_8.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/bin') diff --git a/src/bin/day_8.rs b/src/bin/day_8.rs index c7df9f5..2c0c227 100644 --- a/src/bin/day_8.rs +++ b/src/bin/day_8.rs @@ -1,7 +1,7 @@ use bevy::{app::AppExit, prelude::*}; -use std::fs::File; +use std::convert::TryFrom; use std::io::{BufRead, BufReader}; -use std::{collections::BTreeMap, convert::TryFrom}; +use std::{collections::BTreeSet, fs::File}; fn main() { App::build() @@ -45,7 +45,7 @@ struct Computer { termiated: bool, } #[derive(Default)] -struct LineTracker(BTreeMap); +struct LineTracker(BTreeSet); impl Computer { fn exec(&mut self, program: &Program) { @@ -123,8 +123,7 @@ fn track_program_line_execution( computer: &Computer, unaltered: Option<&UnalteredProgram>, ) { - *line_tracker.0.entry(computer.program_counter).or_insert(0) += 1; - if *line_tracker.0.get(&computer.program_counter).unwrap() > 1 { + if line_tracker.0.contains(&computer.program_counter) { if unaltered.is_some() { println!( "About to execute a line for the second time! Line {}. Accumulator: {}", @@ -132,6 +131,8 @@ fn track_program_line_execution( ); } commands.despawn(entity); + } else { + line_tracker.0.insert(computer.program_counter); } } -- cgit v1.2.3