Browse Source

choose with day to run from command line

master
Alexander Avery 3 weeks ago
parent
commit
970b090886
  1. 16
      2024/day01.go
  2. 6
      2024/puzzle_test.go
  3. 32
      main.go

16
2024/day01.go

@ -2,14 +2,14 @@ package adventoc2024
import ( import (
"bufio" "bufio"
"bytes"
"fmt" "fmt"
"io" "io"
"os"
"sort" "sort"
) )
func part1(r io.Reader) string { type Day1 struct{}
func (d Day1) Part1(r io.Reader) string {
s := bufio.NewScanner(r) s := bufio.NewScanner(r)
var sum int var sum int
var left, right []int var left, right []int
@ -30,7 +30,7 @@ func part1(r io.Reader) string {
return printi(sum) return printi(sum)
} }
func part2(r io.Reader) string { func (d Day1) Part2(r io.Reader) string {
s := bufio.NewScanner(r) s := bufio.NewScanner(r)
var sum int var sum int
var left []int var left []int
@ -51,11 +51,3 @@ func part2(r io.Reader) string {
return printi(sum) return printi(sum)
} }
func main() {
buf := new(bytes.Buffer)
tee := io.TeeReader(os.Stdin, buf)
fmt.Println(part1(tee))
fmt.Println(part2(buf))
}

6
2024/puzzle_test.go

@ -19,12 +19,13 @@ func TestDay01(t *testing.T) {
3 3` 3 3`
p1, p2 := dup(input) 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) 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) t.Errorf("day01 part 2 = %s; expected %s", got, expected)
} }
} }
@ -38,7 +39,6 @@ func TestDay02(t *testing.T) {
1 3 6 7 9` 1 3 6 7 9`
p1, p2 := dup(input) p1, p2 := dup(input)
solver := Day2{} solver := Day2{}
if got, expected := solver.Part1(p1), "2"; got != expected { if got, expected := solver.Part1(p1), "2"; got != expected {

32
main.go

@ -1,18 +1,46 @@
package main package main
import ( import (
"adventoc/2024" adventoc2024 "adventoc/2024"
"bytes" "bytes"
"flag"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"strconv"
) )
type puzzle interface {
Part1(io.Reader) string
Part2(io.Reader) string
}
var puzzles = []puzzle{
adventoc2024.Day1{},
adventoc2024.Day2{},
}
func main() { 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) buf := new(bytes.Buffer)
tee := io.TeeReader(os.Stdin, buf) tee := io.TeeReader(os.Stdin, buf)
puzzle := adventoc2024.Day2{} puzzle := puzzles[pi]
fmt.Println("Part 1:", puzzle.Part1(tee)) fmt.Println("Part 1:", puzzle.Part1(tee))
fmt.Println("Part 2:", puzzle.Part2(buf)) fmt.Println("Part 2:", puzzle.Part2(buf))

Loading…
Cancel
Save