day 10 part 1

This commit is contained in:
Bizcochito 2022-12-11 20:31:42 +01:00
parent f46bd5dfb7
commit 6029d045be
6 changed files with 2099 additions and 1755 deletions

4
Cargo.lock generated
View File

@ -38,6 +38,10 @@ version = "0.1.0"
name = "day09"
version = "0.1.0"
[[package]]
name = "day10"
version = "0.1.0"
[[package]]
name = "tiesto"
version = "0.1.0"

View File

@ -11,7 +11,7 @@ members = [
"day07",
"day08",
"day09",
# "day10",
"day10",
# "day11",
# "day12",
# "day13",

File diff suppressed because it is too large Load Diff

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]

146
day10/input Normal file
View File

@ -0,0 +1,146 @@
noop
addx 5
addx -2
noop
noop
addx 7
addx 15
addx -14
addx 2
addx 7
noop
addx -2
noop
addx 3
addx 4
noop
noop
addx 5
noop
noop
addx 1
addx 2
addx 5
addx -40
noop
addx 5
addx 2
addx 15
noop
addx -10
addx 3
noop
addx 2
addx -15
addx 20
addx -2
addx 2
addx 5
addx 3
addx -2
noop
noop
noop
addx 5
addx 2
addx 5
addx -38
addx 3
noop
addx 2
addx 5
noop
noop
addx -2
addx 5
addx 2
addx -2
noop
addx 7
noop
addx 10
addx -5
noop
noop
noop
addx -15
addx 22
addx 3
noop
noop
addx 2
addx -37
noop
noop
addx 13
addx -10
noop
addx -5
addx 10
addx 5
addx 2
addx -6
addx 11
addx -2
addx 2
addx 5
addx 3
noop
addx 3
addx -2
noop
addx 6
addx -22
addx 23
addx -38
noop
addx 7
noop
addx 5
noop
noop
noop
addx 9
addx -8
addx 2
addx 7
noop
noop
addx 2
addx -4
addx 5
addx 5
addx 2
addx -26
addx 31
noop
addx 3
noop
addx -40
addx 7
noop
noop
noop
noop
addx 2
addx 4
noop
addx -1
addx 5
noop
addx 1
noop
addx 2
addx 5
addx 2
noop
noop
noop
addx 5
addx 1
noop
addx 4
addx 3
noop
addx -24
noop

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

@ -0,0 +1,186 @@
use std::fs;
use std::str::FromStr;
fn main() {
const FILE_PATH: &str = "input";
println!("Hi this is the nineth day of AOC2022, first we will read the file {}", FILE_PATH);
let contents = fs::read_to_string(FILE_PATH)
.expect("Should have been able to read the file");
println!("The sum of the asked for signals is: {}", get_dump(&contents).iter().enumerate().take_while(|(n, _)| n<=&220).map(|(c, rx)| (c+1 ,(c+1) as i32*rx))
.filter(|(n, _)| (n/20)%2==1).step_by(20).map(|(_, out)| out).sum::<i32>());
}
fn get_dump(s: &str) -> Vec<i32> {
let mut rx = 1;
let mut history = Vec::new();
for line in s.lines() {
let mut line = line.split(" ");
match line.next().expect("there must be a command") {
"noop" => history.push(rx),
"addx" => {
history.push(rx);
history.push(rx);
rx += i32::from_str(line.next().expect("There must be an entry")).expect("NaN");
}
_ => unreachable!()
}
}
history
}
#[cfg(test)]
mod test {
const INPUT: &str = r#"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"#;
use super::*;
#[test]
fn crt1() {
assert_eq!(get_dump(INPUT).iter().enumerate().take_while(|(n, _)| n<=&220).map(|(c, rx)| (c+1 ,(c+1) as i32*rx))
.filter(|(n, _)| (n/20)%2==1).step_by(20).map(|(_, out)| out).sum::<i32>(), 13140);
}
}