Add day 2 solution.
This commit is contained in:
parent
16b30b2a75
commit
3ca1a1068b
42
2018/day02-1.go
Normal file
42
2018/day02-1.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"internal/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
lines := util.ReadInput()
|
||||||
|
|
||||||
|
twos := 0
|
||||||
|
threes := 0
|
||||||
|
for _, id := range lines {
|
||||||
|
// here we build up a map of letters and counts
|
||||||
|
letters := make(map[byte]int)
|
||||||
|
for i := 0; i < len(id); i++ {
|
||||||
|
letters[id[i]] += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// these are the values to add to the 'twos' and 'threes'
|
||||||
|
// count, respectively. They will only ever be 0 or 1.
|
||||||
|
add2 := 0
|
||||||
|
add3 := 0
|
||||||
|
|
||||||
|
// now we iterate over the map of letter->count, and set
|
||||||
|
// add2 and add3 if we find any values equal to 2 or 3
|
||||||
|
for _, v := range letters {
|
||||||
|
if v == 2 {
|
||||||
|
add2 = 1
|
||||||
|
}
|
||||||
|
if v == 3 {
|
||||||
|
add3 = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
twos += add2
|
||||||
|
threes += add3
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(twos * threes)
|
||||||
|
}
|
32
2018/day02-2.go
Normal file
32
2018/day02-2.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"internal/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ids := util.ReadInput()
|
||||||
|
|
||||||
|
for _, box1 := range ids {
|
||||||
|
for _, box2 := range ids {
|
||||||
|
diffIndex := -1 // diffIndex doubles as the letter to remove and a 'found' flag
|
||||||
|
for i := range box1 {
|
||||||
|
if box1[i] != box2[i] {
|
||||||
|
if diffIndex != -1 {
|
||||||
|
// If we reach here, we've detected more than one error, so we set
|
||||||
|
// diffIndex back to -1 here to avoid the conditional below this loop.
|
||||||
|
diffIndex = -1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
diffIndex = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if diffIndex != -1 {
|
||||||
|
fmt.Printf("%s%s\n", box1[0:diffIndex], box1[diffIndex+1:])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
2018/internal/util/input.go
Normal file
24
2018/internal/util/input.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ReadInput isn't here to make friends. It is highly specific to this domain.
|
||||||
|
// It assumes the first argument on the command-line is a file with input. It
|
||||||
|
// returns an array of strings containing the lines of that file.
|
||||||
|
func ReadInput() []string {
|
||||||
|
file, err := os.Open(os.Args[1])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := []string{}
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user