From cf351b153aa62a023c5c4d0c8d1d014e1a1cb6f3 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Fri, 14 Jan 2022 14:14:06 +0100 Subject: [PATCH] Added day 24 code for part 1 and 2 Signed-off-by: Louis Vallat --- .gitlab-ci.yml | 5 + day24/Cargo.toml | 8 ++ day24/input | 252 ++++++++++++++++++++++++++++++++++++++++++++++ day24/src/main.rs | 55 ++++++++++ 4 files changed, 320 insertions(+) create mode 100644 day24/Cargo.toml create mode 100644 day24/input create mode 100644 day24/src/main.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c646087..8013f33 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -118,3 +118,8 @@ day-23: stage: build script: - cd day23; cargo run --release ./input + +day-24: + stage: build + script: + - cd day24; cargo run --release ./input diff --git a/day24/Cargo.toml b/day24/Cargo.toml new file mode 100644 index 0000000..207a374 --- /dev/null +++ b/day24/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day24" +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/day24/input b/day24/input new file mode 100644 index 0000000..c38b423 --- /dev/null +++ b/day24/input @@ -0,0 +1,252 @@ +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 11 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 1 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 10 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 10 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 13 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 2 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -10 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 5 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 11 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 6 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 11 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 0 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 12 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 16 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -11 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 12 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -7 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 15 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 13 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 7 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -13 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 6 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x 0 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 5 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -11 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 6 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x 0 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 15 +mul y x +add z y diff --git a/day24/src/main.rs b/day24/src/main.rs new file mode 100644 index 0000000..1e22bfb --- /dev/null +++ b/day24/src/main.rs @@ -0,0 +1,55 @@ +use std::{fs, env}; + +fn read_input(path: &str) -> String { + return fs::read_to_string(path).expect("Cannot read file."); +} + +fn parse_input(s: &str) -> Vec<(usize, usize, i32)> { + let mut v = vec![]; + let mut p: Vec<(usize, i32)> = vec![]; + let mut a = false; + let mut i = 0usize; + for (n, l) in s.lines().enumerate() { + let c = n % (s.lines().count() / 14); + if c == 0 && n != 0 { i += 1; } + let l = l.split_whitespace().collect::>(); + if c == 4 { a = l[2].parse::().unwrap() == 1; } + else if c == 5 && !a { + let x = p.pop().unwrap(); + let o = x.1 + l[2].parse::().unwrap(); + if o >= 0 { v.push((i, x.0, o)); } + else { v.push((x.0, i, -o)); } + } else if c == 15 && a { p.push((i, l[2].parse::().unwrap())); } + } + return v; +} + +fn get_max(l: &Vec<(usize, usize, i32)>) -> u64 { + let mut x = [0u64; 14]; + for i in l { + x[i.0] = 9; + x[i.1] = (9 - i.2) as u64; + } + return x.iter().fold(0, |acc, i| acc * 10 + *i); +} + +fn get_min(l: &Vec<(usize, usize, i32)>) -> u64 { + let mut x = [0u64; 14]; + for i in l { + x[i.1] = 1; + x[i.0] = (1 + i.2) as u64; + } + return x.iter().fold(0, |acc, i| acc * 10 + *i); +} + +fn main() { + let args: Vec = env::args().collect(); + for arg in args.iter().skip(1) { + let input = read_input(&arg); + let vec_in = parse_input(&input); + println!("[{}]", &arg); + println!("\t[Part 1] => Answer is '{}'.", get_max(&vec_in)); + println!("\t[Part 2] => Answer is '{}'.", get_min(&vec_in)); + } +} +