Combine AoC Repos
This commit is contained in:
45
2015/day19/input
Normal file
45
2015/day19/input
Normal file
@@ -0,0 +1,45 @@
|
||||
Al => ThF
|
||||
Al => ThRnFAr
|
||||
B => BCa
|
||||
B => TiB
|
||||
B => TiRnFAr
|
||||
Ca => CaCa
|
||||
Ca => PB
|
||||
Ca => PRnFAr
|
||||
Ca => SiRnFYFAr
|
||||
Ca => SiRnMgAr
|
||||
Ca => SiTh
|
||||
F => CaF
|
||||
F => PMg
|
||||
F => SiAl
|
||||
H => CRnAlAr
|
||||
H => CRnFYFYFAr
|
||||
H => CRnFYMgAr
|
||||
H => CRnMgYFAr
|
||||
H => HCa
|
||||
H => NRnFYFAr
|
||||
H => NRnMgAr
|
||||
H => NTh
|
||||
H => OB
|
||||
H => ORnFAr
|
||||
Mg => BF
|
||||
Mg => TiMg
|
||||
N => CRnFAr
|
||||
N => HSi
|
||||
O => CRnFYFAr
|
||||
O => CRnMgAr
|
||||
O => HP
|
||||
O => NRnFAr
|
||||
O => OTi
|
||||
P => CaP
|
||||
P => PTi
|
||||
P => SiRnFAr
|
||||
Si => CaSi
|
||||
Th => ThCa
|
||||
Ti => BP
|
||||
Ti => TiTi
|
||||
e => HF
|
||||
e => NAl
|
||||
e => OMg
|
||||
|
||||
CRnSiRnCaPTiMgYCaPTiRnFArSiThFArCaSiThSiThPBCaCaSiRnSiRnTiTiMgArPBCaPMgYPTiRnFArFArCaSiRnBPMgArPRnCaPTiRnFArCaSiThCaCaFArPBCaCaPTiTiRnFArCaSiRnSiAlYSiThRnFArArCaSiRnBFArCaCaSiRnSiThCaCaCaFYCaPTiBCaSiThCaSiThPMgArSiRnCaPBFYCaCaFArCaCaCaCaSiThCaSiRnPRnFArPBSiThPRnFArSiRnMgArCaFYFArCaSiRnSiAlArTiTiTiTiTiTiTiRnPMgArPTiTiTiBSiRnSiAlArTiTiRnPMgArCaFYBPBPTiRnSiRnMgArSiThCaFArCaSiThFArPRnFArCaSiRnTiBSiThSiRnSiAlYCaFArPRnFArSiThCaFArCaCaSiThCaCaCaSiRnPRnCaFArFYPMgArCaPBCaPBSiRnFYPBCaFArCaSiAl
|
123
2015/day19/main.go
Normal file
123
2015/day19/main.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var input []string
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
input = append(input, scanner.Text())
|
||||
}
|
||||
|
||||
transforms := make(map[string][]string)
|
||||
var stMolecule string
|
||||
|
||||
for i := range input {
|
||||
if strings.Contains(input[i], "=>") {
|
||||
parts := strings.Split(input[i], " => ")
|
||||
transforms[parts[0]] = append(transforms[parts[0]], parts[1])
|
||||
} else if len(input[i]) > 0 {
|
||||
// Must be the calibration molecule
|
||||
stMolecule = input[i]
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("*** CALIBRATION ***")
|
||||
res := calibrate(stMolecule, transforms)
|
||||
fmt.Println("Found " + strconv.Itoa(len(res)) + " combinations")
|
||||
fmt.Println("\n")
|
||||
fmt.Println("*** MANUFACTURING ***")
|
||||
backTrans := reverseTransforms(transforms)
|
||||
manufacture(stMolecule, backTrans, 0)
|
||||
}
|
||||
|
||||
// stepBack takes an ending string and a reversed list of the transformations
|
||||
func manufacture(molecule string, transforms map[string][]string, steps int) {
|
||||
fmt.Println(molecule)
|
||||
|
||||
if molecule == "e" {
|
||||
fmt.Println("Found in " + strconv.Itoa(steps) + " steps")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
for k, v := range transforms {
|
||||
if strings.Contains(molecule, k) {
|
||||
for i := range v {
|
||||
tstVal := strings.Replace(molecule, k, v[i], 1)
|
||||
manufacture(tstVal, transforms, steps+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func reverseTransforms(t map[string][]string) map[string][]string {
|
||||
ret := make(map[string][]string)
|
||||
for v := range t {
|
||||
for j := range t[v] {
|
||||
ret[t[v][j]] = append(ret[t[v][j]], v)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func calibrate(molecule string, transforms map[string][]string) []string {
|
||||
var ret []string
|
||||
atoms := getAtomsFromMolecule(molecule)
|
||||
|
||||
for i := range atoms {
|
||||
// Build a string of atoms before i
|
||||
start := strings.Join(atoms[:i], "")
|
||||
// And all after i too
|
||||
end := strings.Join(atoms[i+1:], "")
|
||||
// Find all transforms for atoms[i]
|
||||
if v, ok := transforms[atoms[i]]; ok {
|
||||
for j := range v {
|
||||
ret = uniqueInsert(ret, start+v[j]+end)
|
||||
//ret = append(ret, start+v[j]+end)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func getAtomsFromMolecule(molecule string) []string {
|
||||
var atoms []string
|
||||
var bldAtom string
|
||||
|
||||
for i := range molecule {
|
||||
if molecule[i] >= 'A' && molecule[i] <= 'Z' {
|
||||
if len(bldAtom) > 0 {
|
||||
atoms = append(atoms, bldAtom)
|
||||
}
|
||||
bldAtom = string(molecule[i])
|
||||
continue
|
||||
}
|
||||
bldAtom = bldAtom + string(molecule[i])
|
||||
}
|
||||
return append(atoms, bldAtom)
|
||||
}
|
||||
|
||||
func uniqueInsert(curr []string, ins string) []string {
|
||||
for i := range curr {
|
||||
if curr[i] == ins {
|
||||
return curr
|
||||
}
|
||||
}
|
||||
return append(curr, ins)
|
||||
}
|
||||
|
||||
func mustAtoi(s string) int {
|
||||
var i int
|
||||
var err error
|
||||
if i, err = strconv.Atoi(s); err != nil {
|
||||
fmt.Println("Tried to atoi " + s)
|
||||
os.Exit(1)
|
||||
}
|
||||
return i
|
||||
}
|
107
2015/day19/problem
Normal file
107
2015/day19/problem
Normal file
@@ -0,0 +1,107 @@
|
||||
Advent of Code
|
||||
|
||||
br0xen 40*
|
||||
|
||||
• [About]
|
||||
• [Stats]
|
||||
• [Leaderboard]
|
||||
• [Settings]
|
||||
• [Log out]
|
||||
|
||||
--- Day 19: Medicine for Rudolph ---
|
||||
|
||||
Rudolph the Red-Nosed Reindeer is sick! His nose isn't shining very brightly, and he needs
|
||||
medicine.
|
||||
|
||||
Red-Nosed Reindeer biology isn't similar to regular reindeer biology; Rudolph is going to need
|
||||
custom-made medicine. Unfortunately, Red-Nosed Reindeer chemistry isn't similar to regular
|
||||
reindeer chemistry, either.
|
||||
|
||||
The North Pole is equipped with a Red-Nosed Reindeer nuclear fusion/fission plant, capable of
|
||||
constructing any Red-Nosed Reindeer molecule you need. It works by starting with some input
|
||||
molecule and then doing a series of replacements, one per step, until it has the right
|
||||
molecule.
|
||||
|
||||
However, the machine has to be calibrated before it can be used. Calibration involves
|
||||
determining the number of molecules that can be generated in one step from a given starting
|
||||
point.
|
||||
|
||||
For example, imagine a simpler machine that supports only the following replacements:
|
||||
|
||||
H => HO
|
||||
H => OH
|
||||
O => HH
|
||||
|
||||
Given the replacements above and starting with HOH, the following molecules could be generated:
|
||||
|
||||
• HOOH (via H => HO on the first H).
|
||||
• HOHO (via H => HO on the second H).
|
||||
• OHOH (via H => OH on the first H).
|
||||
• HOOH (via H => OH on the second H).
|
||||
• HHHH (via O => HH).
|
||||
|
||||
So, in the example above, there are 4 distinct molecules (not five, because HOOH appears twice)
|
||||
after one replacement from HOH. Santa's favorite molecule, HOHOHO, can become 7 distinct
|
||||
molecules (over nine replacements: six from H, and three from O).
|
||||
|
||||
The machine replaces without regard for the surrounding characters. For example, given the
|
||||
string H2O, the transition H => OO would result in OO2O.
|
||||
|
||||
Your puzzle input describes all of the possible replacements and, at the bottom, the medicine
|
||||
molecule for which you need to calibrate the machine. How many distinct molecules can be
|
||||
created after all the different ways you can do one replacement on the medicine molecule?
|
||||
|
||||
Your puzzle answer was 518.
|
||||
|
||||
--- Part Two ---
|
||||
|
||||
Now that the machine is calibrated, you're ready to begin molecule fabrication.
|
||||
|
||||
Molecule fabrication always begins with just a single electron, e, and applying replacements
|
||||
one at a time, just like the ones during calibration.
|
||||
|
||||
For example, suppose you have the following replacements:
|
||||
|
||||
e => H
|
||||
e => O
|
||||
H => HO
|
||||
H => OH
|
||||
O => HH
|
||||
|
||||
If you'd like to make HOH, you start with e, and then make the following replacements:
|
||||
|
||||
• e => O to get O
|
||||
• O => HH to get HH
|
||||
• H => OH (on the second H) to get HOH
|
||||
|
||||
So, you could make HOH after 3 steps. Santa's favorite molecule, HOHOHO, can be made in 6
|
||||
steps.
|
||||
|
||||
How long will it take to make the medicine? Given the available replacements and the medicine
|
||||
molecule in your puzzle input, what is the fewest number of steps to go from e to the medicine
|
||||
molecule?
|
||||
|
||||
Your puzzle answer was 200.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your advent calendar and try another puzzle.
|
||||
|
||||
If you still want to see it, you can get your puzzle input.
|
||||
|
||||
You can also [Shareon Twitter Google+ Reddit] this puzzle.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
. http://adventofcode.com/
|
||||
. http://adventofcode.com/about
|
||||
. http://adventofcode.com/stats
|
||||
. http://adventofcode.com/leaderboard
|
||||
. http://adventofcode.com/settings
|
||||
. http://adventofcode.com/auth/logout
|
||||
. http://adventofcode.com/
|
||||
. http://adventofcode.com/day/19/input
|
||||
. https://twitter.com/intent/tweet?text=I%27ve+completed+%22Medicine+for+Rudolph%22+%2D+Day+19+%2D+Advent+of+Code&url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F19&related=ericwastl&hashtags=AdventOfCode
|
||||
. https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F19
|
||||
. http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2Fday%2F19&title=I%27ve+completed+%22Medicine+for+Rudolph%22+%2D+Day+19+%2D+Advent+of+Code
|
5
2015/day19/sample_input
Normal file
5
2015/day19/sample_input
Normal file
@@ -0,0 +1,5 @@
|
||||
H => HO
|
||||
H => OH
|
||||
O => HH
|
||||
|
||||
HOH
|
Reference in New Issue
Block a user