Day 2 part 2.
This commit is contained in:
parent
8dc6e4e5f6
commit
86a286a2b1
37
2017/2/2.go
37
2017/2/2.go
@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func checksum(spreadsheet string) int {
|
||||
func checksum1(spreadsheet string) int {
|
||||
sum := 0
|
||||
for _, row := range strings.Split(spreadsheet, "\n") {
|
||||
min := math.MaxInt64
|
||||
@ -29,6 +29,38 @@ func checksum(spreadsheet string) int {
|
||||
return sum
|
||||
}
|
||||
|
||||
func checksum2(spreadsheet string) int {
|
||||
sum := 0
|
||||
for _, row := range strings.Split(spreadsheet, "\n") {
|
||||
var cols []int
|
||||
for _, col := range strings.Fields(row) {
|
||||
v, err := strconv.Atoi(col)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Failed to parse %q", col))
|
||||
}
|
||||
cols = append(cols, v)
|
||||
}
|
||||
for i, a1 := range cols[:len(cols)-1] {
|
||||
for _, a2 := range cols[i+1:] {
|
||||
var n, d int
|
||||
if a2 > a1 {
|
||||
n = a2
|
||||
d = a1
|
||||
} else {
|
||||
n = a1
|
||||
d = a2
|
||||
}
|
||||
v := n / d
|
||||
r := n - v*d
|
||||
if r == 0 {
|
||||
sum += v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func main() {
|
||||
spreadsheet := `414 382 1515 319 83 1327 116 391 101 749 1388 1046 1427 105 1341 1590
|
||||
960 930 192 147 932 621 1139 198 865 820 597 165 232 417 19 183
|
||||
@ -46,5 +78,6 @@ func main() {
|
||||
334 275 395 128 347 118 353 281 430 156 312 386 160 194 63 141
|
||||
146 1116 153 815 2212 2070 599 3018 2640 47 125 2292 165 2348 2694 184
|
||||
1704 2194 1753 146 2063 1668 1280 615 163 190 2269 1856 150 158 2250 2459`
|
||||
fmt.Println(checksum(spreadsheet))
|
||||
fmt.Println(checksum1(spreadsheet))
|
||||
fmt.Println(checksum2(spreadsheet))
|
||||
}
|
||||
|
||||
@ -2,12 +2,22 @@ package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestChecksum(t *testing.T) {
|
||||
func TestChecksum1(t *testing.T) {
|
||||
var spreadsheet = `5 1 9 5
|
||||
7 5 3
|
||||
2 4 6 8`
|
||||
want := 18
|
||||
if got := checksum(spreadsheet); got != want {
|
||||
t.Errorf("checksum() = %d; want %d", got, want)
|
||||
if got := checksum1(spreadsheet); got != want {
|
||||
t.Errorf("checksum1() = %d; want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChecksum2(t *testing.T) {
|
||||
var spreadsheet = `5 9 2 8
|
||||
9 4 7 3
|
||||
3 8 6 5`
|
||||
want := 9
|
||||
if got := checksum2(spreadsheet); got != want {
|
||||
t.Errorf("checksum2() = %d; want %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,3 +15,24 @@ For example, given the following spreadsheet:
|
||||
* The second row's largest and smallest values are 7 and 3, and their difference is 4.
|
||||
* The third row's difference is 6.
|
||||
In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
|
||||
|
||||
# Part 2
|
||||
|
||||
"Great work; looks like we're on the right track after all. Here's a star for your effort." However, the program seems a little worried. Can programs be worried?
|
||||
|
||||
"Based on what we're seeing, it looks like all the User wanted is some information about the evenly divisible values in the spreadsheet. Unfortunately, none of us are equipped for that kind of calculation - most of us specialize in bitwise operations."
|
||||
|
||||
It sounds like the goal is to find the only two numbers in each row where one evenly divides the other - that is, where the result of the division operation is a whole number. They would like you to find those numbers on each line, divide them, and add up each line's result.
|
||||
|
||||
For example, given the following spreadsheet:
|
||||
|
||||
```
|
||||
5 9 2 8
|
||||
9 4 7 3
|
||||
3 8 6 5
|
||||
```
|
||||
|
||||
* In the first row, the only two numbers that evenly divide are 8 and 2; the result of this division is 4.
|
||||
* In the second row, the two numbers are 9 and 3; the result is 3.
|
||||
* In the third row, the result is 2.
|
||||
* In this example, the sum of the results would be 4 + 3 + 2 = 9.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user