package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
file, err := os.Open("/workspace/days/03/assembly_codes.txt")
defer file.Close()
if err != nil {
log.Fatalln(err)
}
scanner := bufio.NewScanner(bufio.NewReader(file))
valid, added, removed := 0, 0, 0
for scanner.Scan() {
line := scanner.Text()
invalid := false
// Must end with "CP"
if line[len(line)-2:] != "CP" {
invalid = true
}
// Last two chars don't matter anymore after checking above
for _, c := range line[:len(line)-2] {
// Both A and C are added, but C makes it invalid
// Both R and P are removed, but P makes it invalid
switch c {
case 'C':
invalid = true
fallthrough
case 'A':
added++
case 'P':
invalid = true
fallthrough
case 'R':
removed++
}
}
if !invalid {
valid++
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Invalid input: %s\n", err)
}
fmt.Printf("Valid: %d\n", valid)
fmt.Printf("Added: %d, Removed: %d\n", added, removed)
}
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
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 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.