Skip to content

Commit

Permalink
add sanitizer
Browse files Browse the repository at this point in the history
  • Loading branch information
metalim committed Dec 24, 2024
1 parent 473e589 commit d0a1c46
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
170 changes: 170 additions & 0 deletions cmd/sanitize/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package main

import (
"bytes"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/zeebo/xxh3"
)

var Verbose bool

func Verbosef(format string, a ...any) {
if Verbose {
fmt.Printf(format, a...)
}
}

// Sanitize the repository by removing all the tasks and input files to make little lizard happy
func main() {
flag.BoolVar(&Verbose, "v", false, "Verbose output")
flag.Parse()
sanitizeInputsAndTasks()
}

var actions = []string{
"Angry Alligator annihilated",
"Bold Bear buried",
"Clever Chameleon cleared",
"Daring Dragonfly deleted",
"Eager Eagle erased",
"Fierce Falcon finished",
"Gentle Giraffe gutted",
"Happy Hedgehog hid",
"Inquisitive Iguana invalidated",
"Jumpy Jackal junked",
"Keen Kangaroo kicked out",
"Lazy Lizard lost",
"Mellow Monkey moved away",
"Naughty Newt nixed",
"Obnoxious Owl obliterated",
"Proud Panther purged",
"Quick Quokka quashed",
"Rowdy Raccoon removed",
"Sneaky Snake shredded",
"Tiny Turtle trashed",
"Unique Unicorn undid",
"Valiant Vulture vanished",
"Witty Wolf wiped",
"Xenophobic Xerus x-ed out",
"Young Yak yielded nothing",
"Zany Zebra zeroed out",
}

func sanitizeInputsAndTasks() {
var inputsSanitized int
var tasksSanitized int
// Walk all txt files and remove inputs and task descriptions
filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
if strings.HasSuffix(path, ".git") {
return filepath.SkipDir
}
if d.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".txt") {
return nil
}

if strings.HasPrefix(filepath.Base(path), "input") {
if sanitizeInput(path) {
inputsSanitized++
}
} else {
if sanitizeTask(path) {
tasksSanitized++
}
}
// if inputsSanitized > 1 || tasksSanitized > 1 {
// return filepath.SkipAll
// }
return nil
})
fmt.Printf("Sanitized %d inputs and %d tasks\n", inputsSanitized, tasksSanitized)
}

const fmtReplace = "<%s the content, and left this: %x>\n"

var reSanitized = regexp.MustCompile(`^<[\w\s\-]+ the content, and left this: \w+>`)
var reInputAOC = regexp.MustCompile(`^input\d?\.txt$`)

func sanitizeInput(path string) bool {
Verbosef("Checking input %s\n", path)
// ignore custom inputs, like input_123.txt
name := filepath.Base(path)
if !reInputAOC.MatchString(name) {
Verbosef("Ignoring custom input %s\n", path)
return false
}

content, err := os.ReadFile(path)
catch(err)

if reSanitized.Match(content) {
Verbosef("Input already sanitized in %s\n", path)
return false
}

fmt.Printf("Sanitizing input %s\n", path)
action, hash := getActionHash(content)
replace := fmt.Sprintf(fmtReplace, action, hash)
err = os.WriteFile(path, []byte(replace), 0644)
catch(err)
return true
}

var reTask = regexp.MustCompile(`(?s)(--- Day \d+: [^\n]*---\s*)\n([^\n]*)\n(.*)`)

const fmtDayReplace = "$1\n" + fmtReplace

func sanitizeTask(path string) bool {
Verbosef("Checking %s\n", path)
content, err := os.ReadFile(path)
catch(err)

m := reTask.FindSubmatch(content)
if m == nil {
Verbosef("No task found in %s\n", path)
return false
}

task := m[0]
// day := m[1]
trigger := m[2]
// theRest := m[3]

if reSanitized.Match(trigger) {
Verbosef("Task already sanitized in %s\n", path)
return false
}

fmt.Printf("Sanitizing task %s\n", path)
action, hash := getActionHash(task)
replace := fmt.Sprintf(fmtDayReplace, action, hash)
content = reTask.ReplaceAll(content, []byte(replace))
catch(os.WriteFile(path, content, 0644))
return true
}

func getActionHash(content []byte) (string, uint64) {
content = bytes.TrimSpace(content)
hash := xxh3.Hash(content)
i := hash % uint64(len(actions))
return actions[i], hash
}

func catch(err error) {
if err != nil {
panic(err)
}
}

func Fatalf(format string, a ...any) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ go 1.23.3
require (
github.com/fatih/color v1.18.0
github.com/stretchr/testify v1.10.0
github.com/zeebo/xxh3 v1.0.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
Expand All @@ -11,6 +13,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
Expand Down

0 comments on commit d0a1c46

Please sign in to comment.