Compare commits

...

2 Commits

Author SHA1 Message Date
MasterofJOKers ac5b34606b day10: Changes for part 2
Could be less repeating by using some object for the counting.
2023-01-03 18:32:18 +01:00
MasterofJOKers 4274b306c8 day10: Add part 1 solution 2023-01-03 16:58:28 +01:00
3 changed files with 83 additions and 0 deletions

7
day10/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day10"
version = "0.1.0"

8
day10/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day10"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

68
day10/src/main.rs Normal file
View File

@ -0,0 +1,68 @@
use std::error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
const DEBUG: bool = false;
fn main() -> Result<(), Box<dyn error::Error>> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut strength = 1;
let mut cycle = 0;
let mut lines = String::from("");
for line in reader.lines() {
let line = line?;
let mut split_iter = line.split_whitespace();
let cmd = split_iter.next().ok_or("asdf")?;
if DEBUG {
println!("{} {}", cycle, strength);
}
match cmd {
"noop" => {
if (strength - 1) <= cycle && (strength + 1) >= cycle {
lines += "#";
} else {
lines += ".";
}
cycle += 1;
if (cycle % 40) == 0 {
lines += "\n";
cycle = 0;
}
if DEBUG {
println!("{} {}", cycle, strength);
}
},
"addx" => {
if (strength - 1) <= cycle && (strength + 1) >= cycle {
lines += "#";
} else {
lines += ".";
}
cycle += 1;
if (cycle % 40) == 0 {
lines += "\n";
cycle = 0;
}
if (strength - 1) <= cycle && (strength + 1) >= cycle {
lines += "#";
} else {
lines += ".";
}
cycle += 1;
let num: i32 = split_iter.next().ok_or("bla")?.parse()?;
if (cycle % 40) == 0 {
lines += "\n";
cycle = 0;
}
strength += num;
},
_ => Err(format!("Unknown command: {}", cmd))?
};
}
println!("{lines}");
Ok(())
}