This commit is contained in:
gbrochar 2023-01-07 09:45:40 +01:00
parent b5b9fb1b73
commit 9ecb37b48d
8 changed files with 427 additions and 0 deletions

12
aoc_10a/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# ---> Rust
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

8
aoc_10a/Cargo.toml Normal file
View File

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

139
aoc_10a/input Normal file
View File

@ -0,0 +1,139 @@
addx 2
addx 3
addx -2
addx 3
noop
addx 6
addx -1
addx 4
addx 1
noop
addx 3
addx 1
addx 7
noop
noop
addx -1
addx 3
addx 2
noop
addx 4
addx 2
addx -25
addx -7
addx -4
addx 2
addx 2
addx 19
addx -8
addx -5
addx 2
addx -9
addx 16
addx 3
addx -2
addx 12
addx -5
addx 2
addx -15
noop
noop
noop
addx 5
addx 16
addx -22
addx -14
addx 5
noop
addx 29
noop
noop
noop
addx -21
addx 2
noop
noop
addx 5
addx -1
addx 1
noop
noop
addx 8
addx -2
addx 4
noop
addx -22
addx 29
noop
addx -36
noop
addx -2
addx 6
addx -2
addx 2
noop
noop
noop
addx 8
addx 2
addx 10
noop
addx -5
addx 3
addx -2
addx 9
addx -2
addx 2
addx -21
addx 10
addx 17
addx -38
noop
noop
noop
addx 34
addx -27
addx 2
addx -6
addx 7
addx 5
addx 2
addx 5
noop
noop
noop
addx 3
addx -2
addx 2
addx 5
addx 2
addx -29
addx 35
addx -3
addx -25
addx -8
addx 1
noop
addx 4
addx 3
addx -2
addx 5
noop
addx 8
addx -6
noop
addx -3
addx 10
noop
noop
addx 6
addx -1
addx -18
addx 21
addx -30
addx 37
addx 1
noop
noop
noop
noop

146
aoc_10a/input2 Normal file
View File

@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

50
aoc_10a/src/main.rs Normal file
View File

@ -0,0 +1,50 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn update_signal(cycle: i32, register: i32, signal: i32) -> i32 {
match cycle % 40 {
20 => signal + register * cycle,
_ => signal
}
}
fn main() {
let file_path = String::from("input");
let mut cycle: i32 = 0;
let mut register: i32 = 1;
let mut signal: i32 = 0;
println!("In file {}", file_path);
if let Ok(lines) = read_lines(file_path) {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
let split: Vec<&str> = ip.split(" ").collect();
let v = split.get(1).unwrap_or(&"").parse::<i32>();
match v {
Ok(n) => {
cycle += 1;
signal = update_signal(cycle, register, signal);
cycle += 1;
signal = update_signal(cycle, register, signal);
register += n;
},
Err(_) => {
cycle += 1;
signal = update_signal(cycle, register, signal);
},
}
}
}
}
println!("signal {}", signal);
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

12
aoc_10b/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
# ---> Rust
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

8
aoc_10b/Cargo.toml Normal file
View File

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

52
aoc_10b/src/main.rs Normal file
View File

@ -0,0 +1,52 @@
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn update_crt(cycle: i32, register: i32) {
let m = cycle % 40;
if m - register >= 0 && m - register <= 2 {
print!("#");
} else {
print!(".");
}
if m == 0 {
println!("");
}
}
fn main() {
let file_path = String::from("../aoc_10a/input");
let mut cycle: i32 = 0;
let mut register: i32 = 1;
println!("In file {}", file_path);
if let Ok(lines) = read_lines(file_path) {
// Consumes the iterator, returns an (Optional) String
for line in lines {
if let Ok(ip) = line {
let split: Vec<&str> = ip.split(" ").collect();
let v = split.get(1).unwrap_or(&"").parse::<i32>();
match v {
Ok(n) => {
cycle += 1;
update_crt(cycle, register);
cycle += 1;
update_crt(cycle, register);
register += n;
},
Err(_) => {
cycle += 1;
update_crt(cycle, register);
},
}
}
}
}
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}