-
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
1 parent
d6c20e8
commit ab6db6b
Showing
81 changed files
with
215 additions
and
55 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,121 @@ | ||
package day19 | ||
|
||
import ( | ||
"bufio" | ||
"log" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type Part struct { | ||
x int | ||
m int | ||
a int | ||
s int | ||
} | ||
|
||
func (p *Part) getVal(field string) int { | ||
switch field { | ||
case "x": | ||
return (*p).x | ||
case "m": | ||
return (*p).m | ||
case "a": | ||
return (*p).a | ||
case "s": | ||
return (*p).s | ||
} | ||
|
||
log.Fatalf("Part.getVal() unknown field %s\n", field) | ||
return -1 | ||
} | ||
|
||
type RuleSet struct { | ||
name string | ||
rawInput string | ||
workflowSteps []func() | ||
} | ||
|
||
func (rs *RuleSet) init() { | ||
// parse `rawInput` and populate `workflowSteps` | ||
} | ||
|
||
func Solution1(filepath string) int { | ||
return -1 | ||
} | ||
|
||
func Solution2(filepath string) int { | ||
return -1 | ||
} | ||
|
||
func parseInput(filepath string) ([]RuleSet, []Part) { | ||
var ruleSets []RuleSet | ||
var parts []Part | ||
|
||
file, err := os.Open(filepath) | ||
if err != nil { | ||
log.Fatalf("Failed to open the input file: %v\n", err.Error()) | ||
} | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
currentLine := scanner.Text() | ||
|
||
if strings.HasPrefix(currentLine, "{") { | ||
parts = append(parts, parsePart(currentLine)) | ||
} else { | ||
ruleSets = append(ruleSets, parseRuleSet(currentLine)) | ||
} | ||
} | ||
if err = scanner.Err(); err != nil { | ||
log.Fatalf("Error during input file read: %v\n", err.Error()) | ||
} | ||
|
||
return ruleSets, parts | ||
} | ||
|
||
func parseRuleSet(rawInput string) RuleSet { | ||
// name := strings.Split(rawInput, "{")[0] | ||
// ruleSet := RuleSet{ | ||
// name: name, | ||
// rawInput: rawInput, | ||
// } | ||
// ruleSegments := strings.Split(rawInput, ",") | ||
// var rules []func() | ||
// for _, ruleSeg := range ruleSegments { | ||
// samples: | ||
// a<10:gd - conditinal transition | ||
// A - unconditional transition | ||
|
||
// rule func signature func(p Part) (applied bool, transitTo string) | ||
// } | ||
|
||
// return ruleSet | ||
return RuleSet{} | ||
} | ||
|
||
func parsePart(rawInput string) Part { | ||
numRegex := regexp.MustCompile(`\d+`) | ||
matches := numRegex.FindAllString(rawInput, -1) | ||
if len(matches) != 4 { | ||
log.Fatalf("Failed to parse Part properly: input=%v, matches=%v\n", rawInput, matches) | ||
} | ||
|
||
// fields are always specified in the same order in the input | ||
return Part{ | ||
x: parseInt(matches[0]), | ||
m: parseInt(matches[1]), | ||
a: parseInt(matches[2]), | ||
s: parseInt(matches[3]), | ||
} | ||
} | ||
|
||
func parseInt(strNum string) int { | ||
num, err := strconv.ParseInt(strNum, 10, 64) | ||
if err != nil { | ||
log.Fatalf("Failed to parse number %v with %v\n", strNum, err.Error()) | ||
} | ||
|
||
return int(num) | ||
} |
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,18 @@ | ||
package day19 | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSolution1(t *testing.T) { | ||
cases := map[string]int{ | ||
"test_input11": 19114, | ||
} | ||
|
||
for input, expectedResult := range cases { | ||
result := Solution1(input) | ||
|
||
assert.Equal(t, expectedResult, result, "Solution1()") | ||
} | ||
} |
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,17 @@ | ||
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} |
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