Re-implement a solution for the first day 1 puzzle, move common code into a library package.

This commit is contained in:
Anna Rose 2018-12-01 22:13:08 -05:00
parent 333321cfd7
commit 01f53ae30c
No known key found for this signature in database
GPG Key ID: 8D9ACA841015C59A
3 changed files with 44 additions and 19 deletions

18
2018/day01-1.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"fmt"
"os"
"internal/calibration"
)
// Provide a filename as input, get the result on stdout
func main() {
x := 0
seen := calibration.Set{} // we don't use this until day01-2, just added here for backwards compatibility
x, _ = calibration.ScanFrequencies(os.Args[1], &seen, x)
fmt.Println("Frequency list scanned, calibration complete: ", x)
}

25
2018/day01-2.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"fmt"
"os"
"internal/calibration"
)
// Provide a filename as input, get the result on stdout
func main() {
x := 0
found := false
seen := calibration.Set{0: struct{}{}}
for {
x, found = calibration.ScanFrequencies(os.Args[1], &seen, x)
if found {
break
}
fmt.Println("All frequencies scanned, repeating...")
}
fmt.Println("Repeat frequency detected, calibration complete: ", x)
}

View File

@ -1,31 +1,13 @@
package main package calibration
import ( import (
"bufio" "bufio"
"fmt"
"os" "os"
"strconv" "strconv"
) )
type Set map[int]struct{} type Set map[int]struct{}
// Provide a filename as input, get the result on stdout
func main() {
x := 0
found := false
seen := Set{0: struct{}{}}
for {
x, found = ScanFrequencies(os.Args[1], &seen, x)
if found {
break
}
fmt.Println("All frequencies scanned, repeating...")
}
fmt.Println("Repeat frequency detected, calibration complete: ", x)
}
// ScanFrequencies iterates over a list of frequencies (provided as an open // ScanFrequencies iterates over a list of frequencies (provided as an open
// file handle), compares against a list of frequencies already seen and a 'current' frequency counter, // file handle), compares against a list of frequencies already seen and a 'current' frequency counter,
// and returns `true` when a duplicate frequency is found. Integer value returned is the new, adjusted frequency // and returns `true` when a duplicate frequency is found. Integer value returned is the new, adjusted frequency