diff --git a/2024/day01.go b/2024/day01.go index 7a24926..90913fb 100644 --- a/2024/day01.go +++ b/2024/day01.go @@ -2,14 +2,14 @@ package adventoc2024 import ( "bufio" - "bytes" "fmt" "io" - "os" "sort" ) -func part1(r io.Reader) string { +type Day1 struct{} + +func (d Day1) Part1(r io.Reader) string { s := bufio.NewScanner(r) var sum int var left, right []int @@ -30,7 +30,7 @@ func part1(r io.Reader) string { return printi(sum) } -func part2(r io.Reader) string { +func (d Day1) Part2(r io.Reader) string { s := bufio.NewScanner(r) var sum int var left []int @@ -51,11 +51,3 @@ func part2(r io.Reader) string { return printi(sum) } - -func main() { - buf := new(bytes.Buffer) - tee := io.TeeReader(os.Stdin, buf) - - fmt.Println(part1(tee)) - fmt.Println(part2(buf)) -} diff --git a/2024/puzzle_test.go b/2024/puzzle_test.go index 9379376..5081671 100644 --- a/2024/puzzle_test.go +++ b/2024/puzzle_test.go @@ -19,12 +19,13 @@ func TestDay01(t *testing.T) { 3 3` p1, p2 := dup(input) + solver := Day1{} - if got, expected := part1(p1), "11"; got != expected { + if got, expected := solver.Part1(p1), "11"; got != expected { t.Errorf("day01 part 1 = %s; expected %s", got, expected) } - if got, expected := part2(p2), "31"; got != expected { + if got, expected := solver.Part2(p2), "31"; got != expected { t.Errorf("day01 part 2 = %s; expected %s", got, expected) } } @@ -38,7 +39,6 @@ func TestDay02(t *testing.T) { 1 3 6 7 9` p1, p2 := dup(input) - solver := Day2{} if got, expected := solver.Part1(p1), "2"; got != expected { diff --git a/main.go b/main.go index 4b01527..7e9875f 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,46 @@ package main import ( - "adventoc/2024" + adventoc2024 "adventoc/2024" "bytes" + "flag" "fmt" "io" + "log" "os" + "strconv" ) +type puzzle interface { + Part1(io.Reader) string + Part2(io.Reader) string +} + +var puzzles = []puzzle{ + adventoc2024.Day1{}, + adventoc2024.Day2{}, +} + func main() { + flag.Parse() + pi := 0 + if i := flag.Arg(0); i != "" { + var err error + pi, err = strconv.Atoi(i) + if err != nil { + log.Fatalf("invalid puzzle day: %s", i) + } + pi-- + + if pi < 0 || pi > len(puzzles) { + log.Fatalf("puzzle index %d is out of range", pi) + } + } + buf := new(bytes.Buffer) tee := io.TeeReader(os.Stdin, buf) - puzzle := adventoc2024.Day2{} + puzzle := puzzles[pi] fmt.Println("Part 1:", puzzle.Part1(tee)) fmt.Println("Part 2:", puzzle.Part2(buf))