-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
478 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func AOC202203Priority(input rune) int { | ||
// a->z => 1->26 | ||
// A->Z => 27->52 | ||
if 'a' <= input && input <= 'z' { | ||
return int(input - 'a' + 1) | ||
} | ||
|
||
return int(input - 'A' + 26 + 1) | ||
} | ||
|
||
func AOC202203Item(line string) rune { | ||
partlen := len(line) / 2 | ||
|
||
// O(n^2), yeah! | ||
for _, lRune := range line[:partlen] { | ||
for _, rRune := range line[partlen:] { | ||
if lRune == rRune { | ||
return lRune | ||
} | ||
} | ||
} | ||
|
||
return ' ' | ||
} | ||
|
||
func AOC202203Multiitem(first, second, third string) rune { | ||
// O(n^3), yeah! | ||
for _, lRune := range first { | ||
for _, mRune := range second { | ||
for _, rRune := range third { | ||
if lRune == mRune && mRune == rRune { | ||
return lRune | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ' ' | ||
} | ||
|
||
func AOC202203Round(line string) int { | ||
item := AOC202203Item(line) | ||
prio := AOC202203Priority(item) | ||
return prio | ||
} | ||
|
||
func AOC2022031(input string) (int, error) { | ||
sum := 0 | ||
for _, line := range strings.Split(input, "\n") { | ||
prio := AOC202203Round(line) | ||
sum += prio | ||
} | ||
|
||
return sum, nil | ||
} | ||
|
||
func AOC2022032(input string) (int, error) { | ||
sum := 0 | ||
lines := strings.Split(input, "\n") | ||
rounds := len(lines) / 3 | ||
for round := 0; round < rounds; round++ { | ||
item := AOC202203Multiitem(lines[round*3], lines[round*3+1], lines[round*3+2]) | ||
prio := AOC202203Priority(item) | ||
sum += prio | ||
} | ||
|
||
return sum, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestAOC2022031(t *testing.T) { | ||
var tests = []struct { | ||
input string | ||
item rune | ||
priority int | ||
}{ | ||
{ | ||
input: "vJrwpWtwJgWrhcsFMMfFFhFp", | ||
item: 'p', | ||
priority: 16, | ||
}, | ||
{ | ||
input: "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL", | ||
item: 'L', | ||
priority: 38, | ||
}, | ||
{ | ||
input: "PmmdzqPrVvPwwTWBwg", | ||
item: 'P', | ||
priority: 42, | ||
}, | ||
{ | ||
input: "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn", | ||
item: 'v', | ||
priority: 22, | ||
}, | ||
{ | ||
input: "ttgJtRGJQctTZtZT", | ||
item: 't', | ||
priority: 20, | ||
}, | ||
{ | ||
input: "CrZsJsPPZsGzwwsLwLmpwMDw", | ||
item: 's', | ||
priority: 19, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
got := AOC202203Item(test.input) | ||
if got != test.item { | ||
t.Errorf("AOC202203Round(%s) missmatch:\nwant: %v\ngot: %v", test.input, test.item, got) | ||
} | ||
prio := AOC202203Priority(test.item) | ||
if prio != test.priority { | ||
t.Errorf("AOC202203Priority(%v) missmatch:\nwant: %d\ngot: %d", test.item, test.priority, prio) | ||
} | ||
} | ||
} | ||
|
||
func TestAOC2022032(t *testing.T) { | ||
var tests = []struct { | ||
input []string | ||
output rune | ||
}{ | ||
{ | ||
|
||
input: []string{ | ||
"vJrwpWtwJgWrhcsFMMfFFhFp", | ||
"jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL", | ||
"PmmdzqPrVvPwwTWBwg", | ||
}, | ||
output: 'r', | ||
}, | ||
{ | ||
input: []string{ | ||
"wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn", | ||
"ttgJtRGJQctTZtZT", | ||
"CrZsJsPPZsGzwwsLwLmpwMDw", | ||
}, | ||
output: 'Z', | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
out := AOC202203Multiitem(test.input[0], test.input[1], test.input[2]) | ||
if out != test.output { | ||
t.Errorf("AOC202203Item3() missmatch:\nwant: %v\ngot: %v", test.output, out) | ||
} | ||
} | ||
} |
Oops, something went wrong.