2018/day06

This commit is contained in:
alexchao26
2020-11-29 20:44:37 -05:00
parent 884f43e9bf
commit 45ae158f9e
4 changed files with 196 additions and 0 deletions
+1
View File
File diff suppressed because one or more lines are too long
+86
View File
@@ -0,0 +1,86 @@
package main
import (
"errors"
"flag"
"fmt"
"math"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
if part == 1 {
ans := part1(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
} else {
ans := part2(util.ReadFile("./input.txt"))
fmt.Println("Output:", ans)
}
}
func part1(input string) int {
var err error
for err != ErrDone {
input, err = step(input)
}
return len(input)
}
func part2(input string) int {
// find all units (lowercased) that can be removed
units := map[byte]bool{}
for i := 0; i < len(input); i++ {
if input[i] < byte(96) {
units[input[i]] = true
} else {
units[input[i]-ASCIIOffset] = true
}
}
lowestResult := math.MaxInt16
for u := range units {
// remove all unit & its corresponding capital/lowercase version
newStr := removeUnit(input, u)
// run new string through part1 and update lowestResult if possible
if result := part1(newStr); result < lowestResult {
lowestResult = result
}
}
return lowestResult
}
const ASCIIOffset = byte('a' - 'A')
var ErrDone = errors.New("no-op")
func step(units string) (string, error) {
offset := byte('a' - 'A')
for i := 1; i < len(units); i++ {
if units[i-1]-units[i] == offset || units[i-1]-units[i] == -offset {
// remove units i-1 and i
newStr := units[:i-1] + units[i+1:]
return newStr, nil
}
}
return units, ErrDone
}
func removeUnit(input string, unit byte) string {
var newStr string
for i := 0; i < len(input); i++ {
if input[i] == unit || input[i] == unit+ASCIIOffset {
continue
}
newStr += string(input[i])
}
return newStr
}
+48
View File
@@ -0,0 +1,48 @@
package main
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
var tests1 = []struct {
name string
want int
input string
// add extra args if needed
}{
{"ex1", 10, "dabAcCaCBAcCcaDA"},
{"actual", 10180, util.ReadFile("input.txt")},
}
func TestPart1(t *testing.T) {
for _, test := range tests1 {
t.Run(test.name, func(*testing.T) {
got := part1(test.input)
if got != test.want {
t.Errorf("want %v, got %v", test.want, got)
}
})
}
}
var tests2 = []struct {
name string
want int
input string
}{
{"ex1", 4, "dabAcCaCBAcCcaDA"},
{"actual", 5668, util.ReadFile("input.txt")},
}
func TestPart2(t *testing.T) {
for _, test := range tests2 {
t.Run(test.name, func(*testing.T) {
got := part2(test.input)
if got != test.want {
t.Errorf("want %v, got %v", test.want, got)
}
})
}
}
+61
View File
@@ -0,0 +1,61 @@
--- Day 5: Alchemical Reduction ---
You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).
The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, r and R are units with the same type but opposite polarity, whereas r and s are entirely different types and do not react.
For example:
In aA, a and A react, leaving nothing behind.
In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing.
In abAB, no two adjacent units are of the same type, and so nothing happens.
In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing happens.
Now, consider a larger example, dabAcCaCBAcCcaDA:
dabAcCaCBAcCcaDA The first 'cC' is removed.
dabAaCBAcCcaDA This creates 'Aa', which is removed.
dabCBAcCcaDA Either 'cC' or 'Cc' are removed (the result is the same).
dabCBAcaDA No further actions can be taken.
After all possible reactions, the resulting polymer contains 10 units.
How many units remain after fully reacting the polymer you scanned? (Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)
--- Part Two ---
Time to improve the polymer.
One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.
For example, again using the polymer dabAcCaCBAcCcaDA from above:
Removing all A/a units produces dbcCCBcCcD. Fully reacting this polymer produces dbCBcD, which has length 6.
Removing all B/b units produces daAcCaCAcCcaDA. Fully reacting this polymer produces daCAcaDA, which has length 8.
Removing all C/c units produces dabAaBAaDA. Fully reacting this polymer produces daDA, which has length 4.
Removing all D/d units produces abAcCaCBAcCcaA. Fully reacting this polymer produces abCBAc, which has length 6.
In this example, removing all C/c units was best, producing the answer 4.
What is the length of the shortest polymer you can produce by removing all units of exactly one type and fully reacting the result?