chore: organization day19

This commit is contained in:
xuu 2023-12-30 19:46:23 -07:00
parent 39c6f8ed4d
commit 62f8338227
2 changed files with 182 additions and 95 deletions

View File

@ -32,8 +32,34 @@ func run(scan *bufio.Scanner) (*result, error) {
} }
// Is Part // Is Part
if text[0] == '{' { if p, ok := scanPart(text); ok {
parts = append(parts, p)
continue
}
if name, r, ok := scanRule(text); ok {
workflows[name] = r
}
}
var result result
result.valuePT1 = solveWorkflow(parts, workflows)
return &result, nil
}
type part struct {
x, m, a, s int
}
func (p part) String() string {
return fmt.Sprintf("{x:%v m:%v a:%v s:%v}", p.x, p.m, p.a, p.s)
}
func scanPart(text string) (part, bool) {
var p part var p part
// Is Part
if text[0] == '{' {
for _, s := range strings.Split(text[1:], ",") { for _, s := range strings.Split(text[1:], ",") {
a, b, _ := strings.Cut(s, "=") a, b, _ := strings.Cut(s, "=")
i := aoc.Atoi(b) i := aoc.Atoi(b)
@ -48,10 +74,19 @@ func run(scan *bufio.Scanner) (*result, error) {
p.s = i p.s = i
} }
} }
parts = append(parts, p) return p, true
continue
} }
return p, false
}
type rule struct {
match string
op string
value int
queue string
}
func scanRule(text string) (string, []rule, bool) {
name, text, _ := strings.Cut(text, "{") name, text, _ := strings.Cut(text, "{")
var r []rule var r []rule
for _, s := range strings.Split(text, ",") { for _, s := range strings.Split(text, ",") {
@ -81,66 +116,7 @@ func run(scan *bufio.Scanner) (*result, error) {
r = append(r, rule{queue: s}) r = append(r, rule{queue: s})
break break
} }
workflows[name] = r return name, r, len(r) > 0
}
var rejected []part
var accepted []part
for _, p := range parts {
workflow := "in"
nextStep:
for workflow != "" {
for _, r := range workflows[workflow] {
if !r.Match(p) {
continue
}
workflow = r.queue
if workflow == "A" {
accepted = append(accepted, p)
workflow = ""
break nextStep
}
if workflow == "R" {
rejected = append(rejected, p)
workflow = ""
break nextStep
}
continue nextStep
}
}
}
fmt.Println("accepted", accepted)
fmt.Println("rejected", rejected)
var result result
for _, p := range accepted {
result.valuePT1 += p.x
result.valuePT1 += p.m
result.valuePT1 += p.a
result.valuePT1 += p.s
}
return &result, nil
}
type part struct {
x, m, a, s int
}
func (p part) String() string {
return fmt.Sprintf("{x:%v m:%v a:%v s:%v}", p.x,p.m,p.a,p.s)
}
type rule struct {
match string
op string
value int
queue string
} }
func (r rule) Match(p part) bool { func (r rule) Match(p part) bool {
var value int var value int
@ -166,12 +142,114 @@ func (r rule) Match(p part) bool {
return false // no match return false // no match
} }
func solveWorkflow(parts []part, workflows map[string][]rule) int {
var rejected []part
var accepted []part
func in(n string, haystack ...string) bool { for _, p := range parts {
for _, h := range haystack { workflow := "in"
if n == h {
return true nextStep:
for workflow != "" {
for _, r := range workflows[workflow] {
if !r.Match(p) {
continue
}
workflow = r.queue
if workflow == "A" {
accepted = append(accepted, p)
workflow = ""
break nextStep
}
if workflow == "R" {
rejected = append(rejected, p)
workflow = ""
break nextStep
}
continue nextStep
} }
} }
return false }
fmt.Println("accepted", accepted)
fmt.Println("rejected", rejected)
sum := 0
for _, p := range accepted {
sum += p.x
sum += p.m
sum += p.a
sum += p.s
}
return sum
} }
/*
in{s<1351:px, s>=1351:qqz}
--> px{a<2006&&(x<1416||x>2662):A, a<2006&&x>=1416&&x<=2662:R, m>2090:A, a>=2006&&m<=2090&&s<537:R||x>2440:R, a>=2006&&m<=2090&&s<537&x<=2440:A}
--> qqz{s>2770:A, m<1801&&m>838:A, m<1801&&a>1716:R, m<1801&&a<=1716:A, s<=2770&&m>=1801:R}
in [/]
--
s<1351 -> px
s>=1351 -> qqz
px [/]
--
s<1351 -> px
a< 2006 -> qkq
m> 2090 -> A
a>=2006 -> ...
m<=2090 -> rfg
qqz [ ]
--
s>=1351 -> qqz
s> 2770 -> qs
m< 1801 -> hdj
-> R
qkq [ ]
--
s< 1351 ->
a< 2006 ->
x< 1416 -> A
x>=1416 -> crn
rfg [ ]
--
s< 1351 -> px
a>=2006 ->...
m<=2090 -> rfg
s< 537 -> gd
x> 2440 -> R
s>= 537 ->...
x<=2440 -> A
crn [ ]
--
s< 1351 -> px
a< 2006 -> qkq
x>=1416 -> crn
x> 2662 -> A
x<=2662 -> R
A
--
*/

9
set.go
View File

@ -57,3 +57,12 @@ func (m *defaultMap[K, V]) Items() []pair[K, V] {
} }
return items return items
} }
func In[C comparable](n C, haystack ...C) bool {
for _, h := range haystack {
if n == h {
return true
}
}
return false
}