generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
616 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package day19p1 | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strconv" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
workflows := make(workflowMap) | ||
partList := []part{} | ||
|
||
startParts := false | ||
for _, ln := range lines { | ||
if ln == "" { | ||
startParts = true | ||
continue | ||
} | ||
|
||
if startParts { | ||
p := parsePart(ln) | ||
partList = append(partList, p) | ||
} else { | ||
name, instructions := parseWorkflow(ln) | ||
workflows[name] = instructions | ||
} | ||
} | ||
|
||
var sum int64 | ||
for _, p := range partList { | ||
accepted := workflows.accept(p, "in") | ||
if utils.Verbose { | ||
fmt.Printf("%#v %t\n", p, accepted) | ||
} | ||
if accepted { | ||
sum += p.x + p.m + p.a + p.s | ||
} | ||
} | ||
return sum | ||
} | ||
|
||
type operation int | ||
|
||
const ( | ||
none operation = iota | ||
lessthan | ||
greaterthan | ||
) | ||
|
||
type part struct { | ||
x int64 | ||
m int64 | ||
a int64 | ||
s int64 | ||
} | ||
|
||
func (p part) getAttribute(a string) int64 { | ||
switch a { | ||
case "x": | ||
return p.x | ||
case "m": | ||
return p.m | ||
case "a": | ||
return p.a | ||
case "s": | ||
return p.s | ||
default: | ||
return 0 | ||
} | ||
} | ||
|
||
type rule struct { | ||
op operation | ||
attribute string | ||
argument int64 | ||
result string | ||
} | ||
|
||
func (r rule) evaluate(p part) string { | ||
input := p.getAttribute(r.attribute) | ||
switch r.op { | ||
case none: | ||
return r.result | ||
case lessthan: | ||
if input < r.argument { | ||
return r.result | ||
} | ||
case greaterthan: | ||
if input > r.argument { | ||
return r.result | ||
} | ||
default: | ||
panic("unknown operation") | ||
} | ||
|
||
return "" | ||
} | ||
|
||
type workflowMap map[string][]rule | ||
|
||
// Check if part is accepted starting with workflow start | ||
func (w workflowMap) accept(p part, start string) bool { | ||
instructions := w[start] | ||
|
||
for _, i := range instructions { | ||
result := i.evaluate(p) | ||
switch result { | ||
case "A": | ||
return true | ||
case "R": | ||
return false | ||
case "": | ||
// do nothing and move on | ||
default: | ||
return w.accept(p, result) | ||
} | ||
} | ||
|
||
panic("Finished workflow with no result") | ||
} | ||
|
||
func parseWorkflow(ln string) (string, []rule) { | ||
rules := []rule{} | ||
|
||
ln = strings.TrimSuffix(ln, "}") | ||
parts := strings.Split(ln, "{") | ||
|
||
name := parts[0] | ||
|
||
ruleStatements := strings.Split(parts[1], ",") | ||
|
||
for _, s := range ruleStatements { | ||
ruleComponents := strings.Split(s, ":") | ||
|
||
if len(ruleComponents) == 1 { | ||
// This is a non rule | ||
r := rule{none, "", 0, ruleComponents[0]} | ||
rules = append(rules, r) | ||
} else { | ||
opRune := ruleComponents[0][1] | ||
var op operation | ||
switch opRune { | ||
case '>': | ||
op = greaterthan | ||
case '<': | ||
op = lessthan | ||
default: | ||
panic("unknown operation in parse") | ||
} | ||
|
||
arg, err := strconv.ParseInt(ruleComponents[0][2:], 10, 64) | ||
utils.Check(err, "Unable to parse %s to integer", ruleComponents[0][2:]) | ||
r := rule{op, ruleComponents[0][:1], arg, ruleComponents[1]} | ||
rules = append(rules, r) | ||
} | ||
} | ||
|
||
return name, rules | ||
} | ||
|
||
func parsePart(ln string) part { | ||
ln = strings.TrimSuffix(ln, "}") | ||
ln = strings.TrimPrefix(ln, "{") | ||
|
||
parts := strings.Split(ln, ",") | ||
|
||
ret := part{} | ||
|
||
for _, p := range parts { | ||
sides := strings.Split(p, "=") | ||
value, err := strconv.ParseInt(sides[1], 10, 64) | ||
utils.Check(err, "unable to parse %s to integer", sides[1]) | ||
|
||
switch sides[0] { | ||
case "x": | ||
ret.x = value | ||
case "m": | ||
ret.m = value | ||
case "a": | ||
ret.a = value | ||
case "s": | ||
ret.s = value | ||
} | ||
} | ||
|
||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package day19p1 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `px{a<2006:qkq,m>2090:A,rfg} | ||
pv{a>1716:R,A} | ||
lnx{m>1548:A,A} | ||
rfg{s<537:gd,x>2440:R,A} | ||
qs{s>3448:A,lnx} | ||
qkq{x<1416:A,crn} | ||
crn{x>2662:A,R} | ||
in{s<1351:px,qqz} | ||
qqz{s>2770:qs,m<1801:hdj,R} | ||
gd{a>3333:R,R} | ||
hdj{m>838:A,pv} | ||
{x=787,m=2655,a=1222,s=2876} | ||
{x=1679,m=44,a=2067,s=496} | ||
{x=2036,m=264,a=79,s=2244} | ||
{x=2461,m=1339,a=466,s=291} | ||
{x=2127,m=1623,a=2188,s=1013}` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer int64 | ||
}{ | ||
{testInput, 19114}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(int64) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
Oops, something went wrong.