diff --git a/learning/Go.md b/learning/Go.md new file mode 100644 index 0000000..be70791 --- /dev/null +++ b/learning/Go.md @@ -0,0 +1,13 @@ +# Go Learning + +So this will be my first time at writing some go. +Please excuse me for the ranting 😅 + +## WTFs + +- Why is there no bitwise OR for bools?! Please what? XOR == `a != b`, but still... +- `math.Max()` only works with floats. Of course. + +## Syntax + +- we constantly need to write `:=` which is annoying as hell diff --git a/solutions/1/go/main.go b/solutions/1/go/main.go new file mode 100644 index 0000000..afb16e8 --- /dev/null +++ b/solutions/1/go/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "io/ioutil" + "strconv" + "strings" +) + +const target uint64 = 2020 + +func findTwo(list []uint64) { + for _, a := range list { + for _, b := range list { + if a+b == target { + fmt.Printf("The numbers: %v and %v.\tSolution: %v\n", a, b, a*b) + return + } + } + + } +} + +func findThree(list []uint64) { + for _, a := range list { + for _, b := range list { + for _, c := range list { + if a+b+c == target { + fmt.Printf("The numbers: %v, %v and %v.\tSolution: %v\n", a, b, c, a*b*c) + return + } + } + } + } +} + +func main() { + data, err := ioutil.ReadFile("./solutions/1/data.txt") + if err != nil { + fmt.Println("File reading error", err) + return + } + + lines := strings.Split(strings.TrimSpace(string(data)), "\n") + intLines := []uint64{} + for _, i := range lines { + num, _ := strconv.ParseUint(i, 10, 64) + intLines = append(intLines, num) + } + + // fmt.Println("Result: ", findTwo(intLines)) + // fmt.Println("Result: ", findThree(intLines)) + findTwo(intLines) + findThree(intLines) +} diff --git a/solutions/1/main.py b/solutions/1/python/main.py similarity index 91% rename from solutions/1/main.py rename to solutions/1/python/main.py index 59186a3..e6169ff 100644 --- a/solutions/1/main.py +++ b/solutions/1/python/main.py @@ -3,7 +3,7 @@ from itertools import product from os.path import join, dirname target = 2020 -data = join(dirname(__file__), 'data.txt') +data = join(dirname(__file__), '../data.txt') with open(data) as f: numbers: List[int] = list(map(int, f.readlines())) for a, b in product(numbers, numbers): diff --git a/solutions/2/go/main.go b/solutions/2/go/main.go new file mode 100644 index 0000000..13c05a0 --- /dev/null +++ b/solutions/2/go/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "io/ioutil" + "strconv" + "strings" +) + +type sRow struct { + min, max int + char, password string +} + +func parse(data []byte) []sRow { + parsed := []sRow{} + for _, row := range strings.Split(strings.TrimSpace(string(data)), "\n") { + s0 := strings.Split(row, ":") + rule := strings.TrimSpace(s0[0]) + password := strings.TrimSpace(s0[1]) + s1 := strings.Split(rule, " ") + minMax := strings.TrimSpace(s1[0]) + char := strings.TrimSpace(s1[1]) + s2 := strings.Split(minMax, "-") + min, _ := strconv.Atoi(strings.TrimSpace(s2[0])) + max, _ := strconv.Atoi(strings.TrimSpace(s2[1])) + + r := sRow{ + min: min, + max: max, + char: char, + password: password, + } + parsed = append(parsed, r) + } + return parsed +} + +func validSimple(rows []sRow) int { + valid := 0 + for _, row := range rows { + count := strings.Count(row.password, row.char) + if row.min <= count && count <= row.max { + valid++ + } + } + return valid +} + +func validComplex(rows []sRow) int { + valid := 0 + for _, row := range rows { + l := len(row.password) + min := row.min - 1 + max := row.max - 1 + if min >= l || max >= l { + continue + } + r := []rune(row.password) + a := string(r[min]) == row.char + b := string(r[max]) == row.char + if a != b { + valid++ + } + } + + return valid +} + +func main() { + data, _ := ioutil.ReadFile("./solutions/2/data.txt") + rows := parse(data) + simple := validSimple(rows) + fmt.Println(simple) + complex := validComplex(rows) + fmt.Println(complex) +} diff --git a/solutions/2/main.py b/solutions/2/python/main.py similarity index 94% rename from solutions/2/main.py rename to solutions/2/python/main.py index 47604f0..20b1a96 100644 --- a/solutions/2/main.py +++ b/solutions/2/python/main.py @@ -12,7 +12,7 @@ def checkRow(row: str, alternative=False) -> bool: return minimum <= occurrences <= maximum -data = join(dirname(__file__), 'data.txt') +data = join(dirname(__file__), '../data.txt') with open(data) as f: valid = 0 valid_alt = 0