use std::fs;
fn main() {
let contents = fs::read_to_string("src/assembly_codes.txt")
.expect("should find file");
let mut count = 0;
let mut added = 0;
let mut removed = 0;
for line in contents.split_whitespace() {
let mut line_str = String::from(line);
let mut a_ct = 0;
let mut valid = true;
if line_str.pop() != Some('P') {
valid = false;
}
if line_str.pop() == Some('C') {
valid = false;
}
for c in line_str.chars() {
match c {
'A' => {
a_ct += 1;
added += 1;
}
'R' => {
removed += 1;
if a_ct > 0 {
a_ct -= 1;
} else {
valid = false;
}
}
'C' => {
added += 1;
valid = false;
}
'P' => {
removed += 1;
valid = false;
}
_ => {
println!("Invalid input!");
}
}
}
if valid {
count += 1;
}
}
println!("Part 1: {count}");
println!("Part 2: {added}, {removed}");
}On a particular day at the North Pole, the elves are running an assembly line packaging presents. Each present is assigned a code upon completion that contains a sequence of instructions for how to assemble it. However, there seems to be a problem with the printer, and now, many presents have invalid sequences!
For each present, its assembly code consists of a string with the following commands:
A for “Add part”R for “Remove part”C for “Check assembly”P for “Package present”An assembly code is considered valid if there is a sequence of A and R commands leading to a correctly assembled present, followed by a single C, and ending in P. If there are multiple C or P commands, then the code is invalid, as the elves should only check and package it once. Also, there cannot be an R without a corresponding A before it (as the elves can’t remove a part they haven’t added).
For example, the assembly code AACP is valid (add two parts, check, and then package), while RCP (removal without corresponding addition) and ACCP (two checks) is not.
Problem: Your task is to write a function that receives a list from assembly_codes.txt and returns the number of valid codes.
Example input:
ACP
CRARACP
ARAAACP
ARAPCP
AARRACP
ACR
ARCAP
ARACP
ARACP
ARCP
Example output (number of valid codes):
6
The elves want to get to the bottom of this and to start they need to know how many assembly codes are still valid. Count how many are complete and ready to be packaged.
After a while, and thanks to you for getting how many presents have a correct code, the elves figure out what is wrong with the printer. The assembly commands are getting mixed up when printing. The A and C commands sometimes get swapped, and the R and P commands sometimes get swapped. Since every code should end with a single check and then package, the elves know how to fix them:
C, and the last command is always really a PC anywhere else is really an AP anywhere else is really an ROnce the codes are fixed, they are all valid! The elves want to know how many parts were added and removed in total. Fix all of the assembly codes, then count all of the parts added (A) and removed (R) across every code and report that back to the elves.
For example, ARCAP is fixed to ARACP (2 parts added and 1 part removed), and ACR is fixed to ACP (1 part added). For the whole example input above, 21 parts were added and 11 were removed.