diff --git a/src/2022/day03/input.txt b/src/2022/day03/input.txt new file mode 100644 index 0000000..2d758fe --- /dev/null +++ b/src/2022/day03/input.txt @@ -0,0 +1,300 @@ +CjhshBJCSrTTsLwqwqwb +GtmnFHlDfcpHbLZjtTTRLWwb +fDfNHHjVFNvvrvVBJJdS +PPWvWQjPhrPQwlMWJJdMDGbJTdCJ +rsqsStgNNggBNBZHSrJGdJdCFRRZCFbGbTdJ +qgBqqHzzggBpzSnBNqNSSSgcfhrVlVmwPljQVLVwVvQmmzVl +bBBGBfmGvBTnGtGJBtGpcJbZrrddjqrZhDldwdcqrjrjDr +HWPSQMsPHFsMWPVVMVSHCwDCDwwZZvwjwQZZwjdd +vVHPgHHFRLfpfJTLLtJL +LWLZhDBHhWWHjBwHwBjCTrBnnrQTQCJTJpTQBQ +vvdcqbRmvwSFmRqPFSqwdvtQnrpCQJpNNVnrptVCtCCP +wRSffqlFgvvdgdlzhLWWDzLljZhhGL +LNbTbPdTNgnShgSjmHcqtQGCtrctCPcQqc +vzWZDZZBlDwlzvDRZfFBRzVBtMMGHCcpjrqBGMtQQppqHtCq +DfRvFZjZRsRjlVWsjFlWVWvdSdbhsSmnSngTdTSTsJdSLm +nnZRbfZRTZfRsbZTFPRfpbRJdqqQNPwNqvvwvvvSwdQqdS +WjDzjMWMHpQwcSQWcJSS +hrHhHDgpphmjtMrGLDmGgmnbsflbfZCsnsClltsVsfRR +QFngsFnGdLGFGVRgLLqWPQPWvBrMDppCPrDB +NfcHZZzbHthSZtNtNfhHWhClDvCqlrqBvrCpWMWB +fSMSHjbTMVgVVngVgT +llnMffwbvCnffHvJJPJpPBNVVZDJDmmS +QssGGHhRgcqjRRTWGWRGDZcVFZBpNBmPFBDSmZNP +hzsRHWQshzgRjjsgQRTsbwzCtnrrlnrwzMCtvMff +HdddHHmtDMDTqHHSftmqdmfdssGzrsjVGtrllWlGZGsnlnnp +BQNPNPgPhBBhCJQhbCgCnrrnbZnlWznzpGssllVW +LFNZRvCPCFZmH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BtQJtclBrtJwBzJz +wtMNCNwNqwtMMRnVcTlFtlcnFlsl +vrvrjzjZDDDwmwwVFT +jzrHjrrHjjLBPfQPjZBZzvpSMwCNRpRwSdRCNLqSShSR +BDgnhMDCDDpjDhBDJDfMSsLSZzCFTTLzTFLzTFZS +lrqrlmqbvtvWwVRtwlmrrqNvZlZSsLTTFTFZSPzZFzzTBFzz +HRRRbVmmwqhHHHhGJBcD +MvnmMvNjvvvmNnRcvzHgzMGtzhffHwHtwt +ZBBsFPPrTgCpSSBwHrLtwbHbLLtzLf +ZBZZssBdWRJgmgJdNn +TTLChzhDnjQLTDhTQJrzSbbJHsGrGrGFGb +BfvvpflfWVlVsFFvJHcFJFrJrt +ZwMBwwZPWMMpffflqlZMRnRNQLCNhPhDDNssnRQD \ No newline at end of file diff --git a/src/2022/day03/main.go b/src/2022/day03/main.go new file mode 100644 index 0000000..2f556a9 --- /dev/null +++ b/src/2022/day03/main.go @@ -0,0 +1,137 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "sort" + "strings" +) + +func parseInput(input *os.File) ([]string, error) { + scanner := bufio.NewScanner(input) + rucksacks := make([]string, 0) + for scanner.Scan() { + rucksacks = append(rucksacks, scanner.Text()) + } + return rucksacks, nil +} + +func Day03Of2022PartA(rucksacks []string) (int, error) { + priorities := 0 + for _, rucksack := range rucksacks { + items := strings.Split(rucksack, "") + itemsA, itemsB := halves(items) + sort.Slice(itemsA, func(i, j int) bool { + return itemsA[i] < itemsA[j] + }) + sort.Slice(itemsB, func(i, j int) bool { + return itemsB[i] < itemsB[j] + }) + for _, item := range itemsB { + if search(itemsA, item) { + runeItem := []rune(item)[0] + priorities += charToNumber(runeItem) + break + } + } + } + return priorities, nil +} + +func Day03Of2022PartB(rucksacks []string) (int, error) { + var groups [][]string + priorities := 0 + for i := 0; i < len(rucksacks); i += 3 { + end := i + 3 + if end > len(rucksacks) { + end = len(rucksacks) + } + groups = append(groups, rucksacks[i:end]) + } + for _, group := range groups { + itemsA, itemsB, itemsC := group[0], group[1], group[2] + arrA, arrB, arrC := strings.Split(itemsA, ""), strings.Split(itemsB, ""), strings.Split(itemsC, "") + sort.Slice(arrA, func(i, j int) bool { + return arrA[i] < arrA[j] + }) + sort.Slice(arrB, func(i, j int) bool { + return arrB[i] < arrB[j] + }) + sort.Slice(arrC, func(i, j int) bool { + return arrC[i] < arrC[j] + }) + shortest, second, third := shortest(arrA, arrB, arrC) + for _, item := range shortest { + if search(second, item) && search(third, item) { + priorities += charToNumber([]rune(item)[0]) + break + } + } + } + return priorities, nil +} + +func shortest(arr1, arr2, arr3 []string) ([]string, []string, []string) { + if len(arr1) <= len(arr2) && len(arr1) <= len(arr3) { + return arr1, arr2, arr3 + } + if len(arr2) <= len(arr1) && len(arr2) <= len(arr3) { + return arr2, arr1, arr3 + } + return arr3, arr1, arr2 +} + +func charToNumber(ch rune) int { + if ch >= 'a' && ch <= 'z' { + return int(ch - 'a' + 1) + } else if ch >= 'A' && ch <= 'Z' { + return int(ch - 'A' + 27) + } + return -1 +} + +func search(items []string, target string) bool { + left, right := 0, len(items)-1 + for left <= right { + mid := left + (right-left)/2 + if items[mid] == target { + return true + } + if items[mid] < target { + left = mid + 1 + } else { + right = mid - 1 + } + } + return false +} + +func halves(items []string) ([]string, []string) { + mid := len(items) / 2 + return items[:mid], items[mid:] +} + +func main() { + input, err := os.Open("input.txt") + if err != nil { + log.Fatal(err) + } + defer input.Close() + rucksacks, err := parseInput(input) + if err != nil { + log.Fatalf(err.Error()) + } + priorities, err := Day03Of2022PartA(rucksacks) + if err != nil { + log.Fatalf(err.Error()) + } + input.Seek(0, 0) + prioritiesPartB, err := Day03Of2022PartB(rucksacks) + if err != nil { + log.Fatalf(err.Error()) + } + fmt.Println(priorities) + fmt.Println(prioritiesPartB) +} diff --git a/src/2022/day03/main_test.go b/src/2022/day03/main_test.go new file mode 100644 index 0000000..f815c2a --- /dev/null +++ b/src/2022/day03/main_test.go @@ -0,0 +1,50 @@ +package main + +import ( + "os" + "testing" +) + +func TestDay03Of2022(t *testing.T) { + tests := []struct { + name string + input string + expectedA int + expectedB int + }{ + { + name: "test case from AOC", + input: "test.txt", + expectedA: 157, + expectedB: 70, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + file, err := os.Open(test.input) + if err != nil { + t.Fatalf(err.Error()) + } + defer file.Close() + rucksacks, err := parseInput(file) + if err != nil { + t.Fatalf(err.Error()) + } + priorities, err := Day03Of2022PartA(rucksacks) + if err != nil { + t.Fatalf(err.Error()) + } + if priorities != test.expectedA { + t.Errorf("Expected %d, got %d", test.expectedA, priorities) + } + file.Seek(0, 0) + prioritiesPartB, err := Day03Of2022PartB(rucksacks) + if err != nil { + t.Fatalf(err.Error()) + } + if prioritiesPartB != test.expectedB { + t.Errorf("Expected %d, got %d", test.expectedB, prioritiesPartB) + } + }) + } +} diff --git a/src/2022/day03/test.txt b/src/2022/day03/test.txt new file mode 100644 index 0000000..9919ffa --- /dev/null +++ b/src/2022/day03/test.txt @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw \ No newline at end of file