Skip to content

Commit

Permalink
Learning Go
Browse files Browse the repository at this point in the history
2015 Day 19
  • Loading branch information
pmwals09 committed Jul 23, 2023
1 parent 405c2ba commit 185b9c0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 2015/day-19/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.DEFAULT_GOAL := all
NAME = day-19-medicine-for-rudolph

all: time-node time-go

run-node:
node ./$(NAME).js

time-node:
@echo "Timing Node...\n"
time -l node ./$(NAME).js
@echo ""

run-go:
go run ./$(NAME).go

go.out:
go build -o go.out ./$(NAME).go

time-go: go.out
@echo "Timing Go...\n"
time -l ./go.out
@echo ""

clean:
rm -f ./*.out
rm -f ./go.mod
55 changes: 55 additions & 0 deletions 2015/day-19/day-19-medicine-for-rudolph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)

func main() {
f, _ := os.Open("./day-19-data.txt")
s := bufio.NewScanner(f)
rawInstructions := []string{}
for s.Scan() {
rawInstructions = append(rawInstructions, s.Text())
}

replacements, molecule := rawInstructions[:len(rawInstructions)-2], rawInstructions[len(rawInstructions)-1]

replacementMap := make(map[string][]string)
for _, r := range replacements {
from, to, _ := strings.Cut(r, " => ")
_, exists := replacementMap[from]
if exists {
replacementMap[from] = append(replacementMap[from], to)
} else {
replacementMap[from] = []string{to}
}
}

moleculeSet := make(map[string]struct{})
for k, v := range replacementMap {
for i := 0; i < len(molecule)-(len(k)-1); i++ {
sub := molecule[i : i+len(k)]
if sub == k {
for _, val := range v {
replaced := molecule[:i] + val + molecule[i+len(k):]
moleculeSet[replaced] = struct{}{}
}
}
}
}

fmt.Println("Part one:", len(moleculeSet))

r := regexp.MustCompile("[A-Z]")
numberOfMolecules := len(r.Split(molecule, -1)) - 1
r = regexp.MustCompile("Ar|Rn")
exteriorCantReplace := len(r.Split(molecule, -1)) - 1
r = regexp.MustCompile("Y")
interiorCantReplace := len(r.Split(molecule, -1)) - 1

fmt.Println("Part two:", numberOfMolecules-exteriorCantReplace-(2*interiorCantReplace)-1)
}

0 comments on commit 185b9c0

Please sign in to comment.