Ok maybe RuboCop is a little weird, it changes if c == 'A' || c == 'C' to if %w[A C].include?(c) and self.part1 to part1.
Sure those are more compact, but to my eyes it’s a little too compact. I tend to like more explicit code. These are also just the default settings for RuboCop, I have no idea how many people actually use the defaults or not.
require 'optparse'
class Day03
def initialize(filename)
@filename = filename
end
def solve(options)
if !options[:challenge]
part1
else
part2
end
end
def part1
valid = 0
File.open(@filename).each_line do |code|
parts = 0
checked = false
code = code.rstrip
break if
code.each_char.with_index do |c, i|
case c
when 'A'
parts += 1
when 'R'
break if parts.zero?
parts -= 1
when 'C'
break if i != (code.size - 2)
checked = true
when 'P'
valid += 1 if checked && i == (code.size - 1)
break
end
end
end.close
puts "Valid: #{valid}"
end
def part2
added = 0
removed = 0
File.open(@filename).each_line do |code|
code = code.rstrip
code.each_char.with_index do |c, i|
if %w[A C].include?(c)
added += 1
elsif %w[R P].include?(c)
removed += 1
end
break if i >= (code.size - 3)
end
end.close
puts "Added #{added}, Removed #{removed}"
end
end
# Expects the input file to be named 03.txt and in the current working directory
if __FILE__ == $PROGRAM_NAME
options = {}
OptionParser.new do |parser|
parser.banner = "Usage: #{$PROGRAM_NAME} [options]"
parser.on('-c', '--challenge', 'Solve with the challenge input') do
options[:challenge] = true
end
end.parse!
challenge = Day03.new('03.txt')
challenge.solve options
end
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 the P commands can get swapped. So codes that have a C command anywhere else but as the second to last command are actually A commands, and codes that have a P command anywhere else but at the end are actually a R code.
The elves want to know how many parts were added and removed in total. After fixing the assembly codes count all the parts added and removed and report that back to the elves.