Created
December 2, 2020 17:20
-
-
Save spellgen/efd91d877b72ed54b8687bfd433e50aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"github.com/spellgen/scan" | |
"os" | |
) | |
type passLine struct { | |
s int | |
e int | |
ch string | |
list string | |
} | |
func (p passLine) Parse(in string) (scan.LineScanner, bool) { | |
n, err := fmt.Sscanf(in, "%d-%d %s%s", &p.s, &p.e, &p.ch, &p.list) | |
return p, n == 4 && err == nil | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
panic("no args") | |
} | |
file := os.Args[1] | |
fh, err := os.Open(file) | |
if err != nil { | |
panic(err) | |
} | |
data, err := scan.ReadAll(fh, passLine{}) | |
if err != nil { | |
panic(err) | |
} | |
t1sum := 0 | |
t2sum := 0 | |
for _, p := range data { | |
if test1(p.(passLine)) { | |
t1sum++ | |
} | |
if test2(p.(passLine)) { | |
t2sum++ | |
} | |
} | |
fmt.Printf("test1=%d\n", t1sum) | |
fmt.Printf("test2=%d\n", t2sum) | |
} | |
func test1(p passLine) bool { | |
ch := p.ch[0] | |
var n = 0 | |
for k := range p.list { | |
if p.list[k]==ch { | |
n++ | |
} | |
} | |
return n>=p.s && n<=p.e | |
} | |
func test2(p passLine) bool { | |
ch := p.ch[0] | |
count := 0 | |
if p.list[p.s-1]==ch { | |
count++ | |
} | |
if p.list[p.e-1]==ch { | |
count++ | |
} | |
return count==1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment