This commit is contained in:
Dory 2024-03-12 00:59:44 -07:00
parent 017345beb6
commit 176149a167
5 changed files with 349 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]

143
day10/input.txt Normal file
View File

@ -0,0 +1,143 @@
noop
noop
addx 5
noop
noop
addx 6
addx 4
addx -4
addx 4
addx -6
addx 11
addx -1
addx 2
addx 4
addx 3
noop
addx 2
addx -30
addx 2
addx 33
noop
addx -37
noop
noop
noop
addx 3
addx 2
addx 5
addx 20
addx 7
addx -24
addx 2
noop
addx 7
addx -2
addx -6
addx 13
addx 3
addx -2
addx 2
noop
addx -5
addx 10
addx 5
addx -39
addx 1
addx 5
noop
addx 3
noop
addx -5
addx 10
addx -2
addx 2
noop
noop
addx 7
noop
noop
noop
noop
addx 3
noop
addx 3
addx 2
addx 8
addx -1
addx -20
addx 21
addx -38
addx 5
addx 2
noop
noop
noop
addx 8
noop
noop
addx -2
addx 2
addx -7
addx 14
addx 5
noop
noop
noop
addx -16
addx 17
addx 2
addx -12
addx 19
noop
noop
addx -37
noop
noop
noop
addx 3
addx 2
addx 2
addx 5
addx 20
addx -19
addx 2
noop
noop
noop
addx 5
addx 19
addx -12
addx 3
addx -2
addx 2
addx -18
addx 25
addx -14
addx -22
addx 1
noop
noop
noop
addx 3
addx 5
addx -4
addx 7
addx 4
noop
addx 1
noop
noop
addx 2
addx -6
addx 15
addx -1
addx 4
noop
noop
addx 1
addx 4
addx -33
noop
addx 21
noop

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

@ -0,0 +1,45 @@
use std::env;
use std::process;
use std::fs::File;
use std::io::{self, BufRead};
#[derive(Debug)]
enum Inst{
AddX(i64),
Noop,
}
#[derive(Debug)]
struct ParseError(String);
fn parse_line(line: String) -> Result<Inst, ParseError> {
match &line[..4] {
"noop" => Ok(Inst::Noop),
"addx" => match line[5..].parse::<i64>() {
Ok(operand) => Ok(Inst::AddX(operand)),
Err(e) => Err(ParseError(e.to_string())),
},
_ => Err(ParseError(String::from("bad instruction"))),
}
}
fn main() {
let file = File::open(&env::args().nth(1).unwrap()).unwrap();
let reader = io::BufReader::new(file);
let mut xs: Vec<i64> = vec![0, 1];
for line in reader.lines().flatten() {
let last_x = *xs.last().unwrap();
match parse_line(line) {
Ok(Inst::Noop) => { xs.push(last_x) },
Ok(Inst::AddX(x)) => { xs.push(last_x); xs.push(last_x + x) },
Err(e) => { eprintln!("error: {:?}", e); process::exit(1) },
}
}
let res: i64 = xs.iter().enumerate()
.skip(20).step_by(40)
.map(|(i, &x)| (i as i64)*x)
.sum();
println!("{:?}", res);
}

146
day10/test.txt 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