mirror of
https://github.com/RussellChamp/advent-of-code-2021.git
synced 2026-10-27 21:52:32 +00:00
day 8 work and jumbled together
This commit is contained in:
1 parent
e76cb1a355
commit
c7ea0afedc
18 files changed
+457
-21
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package arrays
|
||||
|
||||
func FindFirst(list []string, filter func(string) bool) string {
|
||||
for _, item := range list {
|
||||
if filter(item) {
|
||||
return item
|
||||
}
|
||||
}
|
||||
panic("No item found that matches criteria")
|
||||
}
|
||||
|
||||
func FindFirstInt(list []int, filter func(int) bool) int {
|
||||
for _, item := range list {
|
||||
if filter(item) {
|
||||
return item
|
||||
}
|
||||
}
|
||||
panic("No item found that matches criteria")
|
||||
}
|
||||
|
||||
func FindAll(list []string, filter func(string) bool) []string {
|
||||
var filteredList []string
|
||||
for _, item := range list {
|
||||
if filter(item) {
|
||||
filteredList = append(filteredList, item)
|
||||
}
|
||||
}
|
||||
|
||||
return filteredList
|
||||
}
|
||||
|
||||
func FindAllInt(list []int, filter func(int) bool) []int {
|
||||
var filteredList []int
|
||||
for _, item := range list {
|
||||
if filter(item) {
|
||||
filteredList = append(filteredList, item)
|
||||
}
|
||||
}
|
||||
|
||||
return filteredList
|
||||
}
|
||||
|
||||
func MapStrToInt(input []string, fn func(string) int) []int {
|
||||
var retVals []int
|
||||
for _, s := range input {
|
||||
retVals = append(retVals, fn(s))
|
||||
}
|
||||
return retVals
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package arrays
|
||||
|
||||
import (
|
||||
"AoC2021/utils/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindFirst(t *testing.T) {
|
||||
list := []string{"foo", "bar", "baz"}
|
||||
filter := func(s string) bool { return s == "baz" }
|
||||
item := FindFirst(list, filter)
|
||||
|
||||
assert.EqualsStr(t, item, "baz", "should find item baz")
|
||||
}
|
||||
|
||||
func TestFindFirstInt(t *testing.T) {
|
||||
list := []int{1, 3, 4, 5}
|
||||
filter := func(i int) bool { return i%2 == 0 }
|
||||
item := FindFirstInt(list, filter)
|
||||
|
||||
assert.EqualsInt(t, item, 4, "should find even number")
|
||||
}
|
||||
|
||||
func TestFindAll(t *testing.T) {
|
||||
list := []string{"hello", "world", "foo"}
|
||||
filter := func(s string) bool { return len(s) > 3 }
|
||||
items := FindAll(list, filter)
|
||||
|
||||
assert.EqualsInt(t, len(items), 2, "should filter lists of strings")
|
||||
}
|
||||
|
||||
func TestFindAllInt(t *testing.T) {
|
||||
list := []int{1, 3, 99, 200, 9000}
|
||||
filter := func(i int) bool { return i > 50 }
|
||||
items := FindAllInt(list, filter)
|
||||
|
||||
assert.EqualsInt(t, len(items), 3, "should filter lists of ints")
|
||||
}
|
||||
|
||||
func TestMapStrToInt(t *testing.T) {
|
||||
list := []string{"alice", "bob", "carol"}
|
||||
mapFn := func(s string) int { return int(s[0]) }
|
||||
|
||||
items := MapStrToInt(list, mapFn)
|
||||
|
||||
assert.EqualsInt(t, items[0], int('a'), "should run map function across all strings")
|
||||
}
|
||||
@@ -9,9 +9,16 @@ func IsTrue(t *testing.T, expr bool, message string) {
|
||||
}
|
||||
}
|
||||
|
||||
func StrEquals(t *testing.T, actual, expected, message string) {
|
||||
func EqualsStr(t *testing.T, actual, expected, message string) {
|
||||
if actual != expected {
|
||||
t.Logf("Expected value '%s' but got '%s': %s", expected, actual, message)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func EqualsInt(t *testing.T, actual, expected int, message string) {
|
||||
if actual != expected {
|
||||
t.Logf("Expected value '%d' but got '%d': %s", expected, actual, message)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package bits
|
||||
|
||||
func GetBitCount(val int) int {
|
||||
count := 0
|
||||
for val != 0 {
|
||||
count += val & 1
|
||||
val >>= 1
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// returns the number of matching bits in the two value's binary expression
|
||||
func MatchingOnBits(v1 int, v2 int) int {
|
||||
count := 0
|
||||
for v1 != 0 && v2 != 0 {
|
||||
if v1&1 == 1 && v2&1 == 1 {
|
||||
count += 1
|
||||
}
|
||||
v1 >>= 1
|
||||
v2 >>= 1
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
func BitwiseAnd(v1 int, v2 int) int {
|
||||
val := 0
|
||||
for block := 1; v1 != 0 || v2 != 0; block <<= 1 {
|
||||
if v1&1 == 1 || v2&1 == 1 {
|
||||
val += block
|
||||
}
|
||||
if v1 > 0 {
|
||||
v1 >>= 1
|
||||
}
|
||||
if v2 > 0 {
|
||||
v2 >>= 1
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func BitwiseAndA(a ...int) int {
|
||||
total := 0
|
||||
for _, i := range a {
|
||||
total = BitwiseAnd(total, i)
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func ByBitCount(size int) func(int) bool {
|
||||
return func(val int) bool { return GetBitCount(val) == size }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package bits
|
||||
|
||||
import (
|
||||
"AoC2021/utils/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetBitCount(t *testing.T) {
|
||||
assert.EqualsInt(t, GetBitCount(0), 0, "should count zero bits")
|
||||
assert.EqualsInt(t, GetBitCount(15), 4, "should count bits")
|
||||
assert.EqualsInt(t, GetBitCount(16), 1, "should not count empty bits")
|
||||
}
|
||||
func TestMatchingOnBits(t *testing.T) {
|
||||
assert.EqualsInt(t, MatchingOnBits(5 /*101*/, 13 /*1101*/), 2, "should match 2 bits")
|
||||
}
|
||||
|
||||
func TestBitwiseAnd(t *testing.T) {
|
||||
assert.EqualsInt(t, BitwiseAnd(7 /*111*/, 18 /*10010*/), 23 /*10111*/, "should bitwise add")
|
||||
}
|
||||
|
||||
func TestBitwiseAndA(t *testing.T) {
|
||||
assert.EqualsInt(t, BitwiseAndA(1, 4, 8, 12), 13 /*1101*/, "should bitwise add lists")
|
||||
}
|
||||
|
||||
func TestByBitCount(t *testing.T) {
|
||||
twoBitFilter := ByBitCount(2)
|
||||
assert.IsTrue(t, twoBitFilter(17 /*10001*/), "should count bits")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package filters
|
||||
|
||||
func ByLength(size int) func(string) bool {
|
||||
return func(val string) bool { return len(val) == size }
|
||||
}
|
||||
+4
-3
@@ -3,9 +3,10 @@ package log
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
NONE = 0
|
||||
NORMAL = 1
|
||||
DEBUG = 2
|
||||
NONE = 0
|
||||
NORMAL = 1
|
||||
DEBUG = 2
|
||||
DIAGNOSTIC = 3
|
||||
)
|
||||
|
||||
var LOG_LEVEL = NORMAL
|
||||
|
||||
Reference in new issue
Block a user