From 98b4e0a88986119fe08e0313b2404f4bd014599c Mon Sep 17 00:00:00 2001 From: Dory Date: Tue, 12 Mar 2024 00:59:44 -0700 Subject: [PATCH] d10p1 --- day10/Cargo.lock | 7 +++ day10/Cargo.toml | 8 +++ day10/input.txt | 143 +++++++++++++++++++++++++++++++++++++++++++++ day10/src/main.rs | 44 ++++++++++++++ day10/test.txt | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 348 insertions(+) create mode 100644 day10/Cargo.lock create mode 100644 day10/Cargo.toml create mode 100644 day10/input.txt create mode 100644 day10/src/main.rs create mode 100644 day10/test.txt diff --git a/day10/Cargo.lock b/day10/Cargo.lock new file mode 100644 index 0000000..5f8ea61 --- /dev/null +++ b/day10/Cargo.lock @@ -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" diff --git a/day10/Cargo.toml b/day10/Cargo.toml new file mode 100644 index 0000000..40d2066 --- /dev/null +++ b/day10/Cargo.toml @@ -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] diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..1832939 --- /dev/null +++ b/day10/input.txt @@ -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 diff --git a/day10/src/main.rs b/day10/src/main.rs new file mode 100644 index 0000000..7255c74 --- /dev/null +++ b/day10/src/main.rs @@ -0,0 +1,44 @@ +use std::env; +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 { + match &line[..4] { + "noop" => Ok(Inst::Noop), + "addx" => match line[5..].parse::() { + 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 = 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) => println!("error: {:?}", e), + } + } + let res: i64 = xs.iter().enumerate() + .skip(20).step_by(40) + .map(|(i, &x)| (i as i64)*x) + .sum(); + println!("{:?}", res); +} + diff --git a/day10/test.txt b/day10/test.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/day10/test.txt @@ -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