mirror of
https://github.com/RussellChamp/advent-of-code-2021.git
synced 2026-10-27 21:52:32 +00:00
day 16 part 1
This commit is contained in:
1 parent
4737afa383
commit
0c277a8404
11 files changed
+463
-1
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package bits
|
||||
|
||||
import "fmt"
|
||||
|
||||
func GetBitCount(val int) int {
|
||||
count := 0
|
||||
for val != 0 {
|
||||
@@ -52,3 +54,66 @@ func BitwiseAndA(a ...int) int {
|
||||
func ByBitCount(size int) func(int) bool {
|
||||
return func(val int) bool { return GetBitCount(val) == size }
|
||||
}
|
||||
|
||||
func ToInt(bits []bool) int {
|
||||
intVal := 0
|
||||
for idx := 0; idx < len(bits); idx++ {
|
||||
intVal = intVal << 1
|
||||
if bits[idx] {
|
||||
intVal += 1
|
||||
}
|
||||
}
|
||||
return intVal
|
||||
}
|
||||
|
||||
func ToStr(bits []bool) string {
|
||||
str := ""
|
||||
for idx := 0; idx < len(bits); idx++ {
|
||||
if bits[idx] {
|
||||
str += "1"
|
||||
} else {
|
||||
str += "0"
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
func HexByteToBits(b byte) ([]bool, error) {
|
||||
// just do this manually because i can't be arsed
|
||||
switch b {
|
||||
case byte('0'):
|
||||
return []bool{false, false, false, false}, nil
|
||||
case byte('1'):
|
||||
return []bool{false, false, false, true}, nil
|
||||
case byte('2'):
|
||||
return []bool{false, false, true, false}, nil
|
||||
case byte('3'):
|
||||
return []bool{false, false, true, true}, nil
|
||||
case byte('4'):
|
||||
return []bool{false, true, false, false}, nil
|
||||
case byte('5'):
|
||||
return []bool{false, true, false, true}, nil
|
||||
case byte('6'):
|
||||
return []bool{false, true, true, false}, nil
|
||||
case byte('7'):
|
||||
return []bool{false, true, true, true}, nil
|
||||
case byte('8'):
|
||||
return []bool{true, false, false, false}, nil
|
||||
case byte('9'):
|
||||
return []bool{true, false, false, true}, nil
|
||||
case byte('A'):
|
||||
return []bool{true, false, true, false}, nil
|
||||
case byte('B'):
|
||||
return []bool{true, false, true, true}, nil
|
||||
case byte('C'):
|
||||
return []bool{true, true, false, false}, nil
|
||||
case byte('D'):
|
||||
return []bool{true, true, false, true}, nil
|
||||
case byte('E'):
|
||||
return []bool{true, true, true, false}, nil
|
||||
case byte('F'):
|
||||
return []bool{true, true, true, true}, nil
|
||||
default:
|
||||
return []bool{}, fmt.Errorf("read invalid byte")
|
||||
}
|
||||
}
|
||||
@@ -26,3 +26,37 @@ func TestByBitCount(t *testing.T) {
|
||||
twoBitFilter := ByBitCount(2)
|
||||
assert.IsTrue(t, twoBitFilter(17 /*10001*/), "should count bits")
|
||||
}
|
||||
|
||||
func TestHexByteToBits(t *testing.T) {
|
||||
cases := []struct {
|
||||
input rune
|
||||
output string
|
||||
}{
|
||||
{'0', "0000"},
|
||||
{'1', "0001"},
|
||||
{'2', "0010"},
|
||||
{'3', "0011"},
|
||||
{'4', "0100"},
|
||||
{'5', "0101"},
|
||||
{'6', "0110"},
|
||||
{'7', "0111"},
|
||||
{'8', "1000"},
|
||||
{'9', "1001"},
|
||||
{'A', "1010"},
|
||||
{'B', "1011"},
|
||||
{'C', "1100"},
|
||||
{'D', "1101"},
|
||||
{'E', "1110"},
|
||||
{'F', "1111"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
bits, err := HexByteToBits(byte(c.input))
|
||||
if err != nil {
|
||||
t.Logf("Got error from HexByteToBits: %s", err.Error())
|
||||
t.Fail()
|
||||
}
|
||||
bitStr := ToStr(bits)
|
||||
assert.EqualsStr(t, bitStr, c.output, "incorrect value")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func CreateOrReplace(fileName string) (*os.File, error) {
|
||||
var output *os.File
|
||||
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
|
||||
output, err = os.Create(fileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error creating file %s", fileName)
|
||||
}
|
||||
} else {
|
||||
err = os.Truncate(fileName, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error truncating file %s", fileName)
|
||||
}
|
||||
output, err = os.Open(fileName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error opening file %s", fileName)
|
||||
}
|
||||
}
|
||||
return output, nil
|
||||
}
|
||||
+30
-1
@@ -1,12 +1,16 @@
|
||||
package log
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
NONE = 0
|
||||
NORMAL = 1
|
||||
DEBUG = 2
|
||||
DIAGNOSTIC = 3
|
||||
NEVER = 99
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -24,6 +28,31 @@ func SetLogLevel(level int) {
|
||||
}
|
||||
}
|
||||
|
||||
func SetLogLevelFromArgs(argMap map[string]string) {
|
||||
logLevelStr, found := argMap["log"]
|
||||
if found {
|
||||
logLevel, err := NORMAL, error(nil)
|
||||
|
||||
switch logLevelStr {
|
||||
case "NONE", "None", "none":
|
||||
logLevel = NONE
|
||||
case "NORMAL", "Normal", "normal":
|
||||
logLevel = NORMAL
|
||||
case "DEBUG", "Debug", "debug":
|
||||
logLevel = DEBUG
|
||||
case "DIAG", "DIAGNOSTIC", "Diag", "Diagnostic", "diag", "diagnostic":
|
||||
logLevel = DIAGNOSTIC
|
||||
default:
|
||||
logLevel, err = strconv.Atoi(logLevelStr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic("Error when parsing log level")
|
||||
}
|
||||
SetLogLevel(logLevel)
|
||||
}
|
||||
}
|
||||
|
||||
func Print(minLevel int, a ...interface{}) (int, error) {
|
||||
if LOG_LEVEL >= minLevel {
|
||||
return fmt.Print(a...)
|
||||
|
||||
@@ -9,3 +9,21 @@ func Min(i1 int, a ...int) int {
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
func Max(i1 int, a ...int) int {
|
||||
max := i1
|
||||
for _, i := range a {
|
||||
if i > max {
|
||||
max = i
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
func BoolToInt(b bool) int {
|
||||
if b {
|
||||
return 1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user