2017 Day 02 Completed in Go

This commit is contained in:
Brian Buller 2017-12-01 23:34:33 -06:00
parent b3860dfb28
commit dc2725defe
3 changed files with 199 additions and 0 deletions

95
2017/day02/day02.go Normal file
View File

@ -0,0 +1,95 @@
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
func main() {
inp := StdinToStrings()
part1(inp)
part2(inp)
}
func part1(inp []string) {
fmt.Println("= Part 1 =")
var chk int
for i := range inp {
cells := strings.Split(inp[i], "\t")
max, min := -1, -1
for j := range cells {
tst := Atoi(cells[j])
if tst > max {
max = tst
}
if min < 0 || tst < min {
min = tst
}
}
chk += (max - min)
}
fmt.Println("Checksum: ", chk)
fmt.Println("")
}
func part2(inp []string) {
fmt.Println("= Part 2 =")
var chk int
for i := range inp {
cells := strings.Split(inp[i], "\t")
var foundIt bool
for j := range cells {
for k := j; k < len(cells); k++ {
jVal, kVal := Atoi(cells[j]), Atoi(cells[k])
if isThePair(jVal, kVal) {
foundIt = true
chk += getResult(jVal, kVal)
break
}
}
if foundIt {
break
}
}
}
fmt.Println("Checksum: ", chk)
fmt.Println("")
}
func isThePair(k, j int) bool {
if k == j {
return false
}
jVal, kVal := float64(j), float64(k)
return math.Mod(jVal, kVal) == 0 || math.Mod(kVal, jVal) == 0
}
func getResult(k, j int) int {
if k > j {
return k / j
}
return j / k
}
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}

16
2017/day02/input Normal file
View File

@ -0,0 +1,16 @@
790 99 345 1080 32 143 1085 984 553 98 123 97 197 886 125 947
302 463 59 58 55 87 508 54 472 63 469 419 424 331 337 72
899 962 77 1127 62 530 78 880 129 1014 93 148 239 288 357 424
2417 2755 254 3886 5336 3655 5798 3273 5016 178 270 6511 223 5391 1342 2377
68 3002 3307 166 275 1989 1611 364 157 144 3771 1267 3188 3149 156 3454
1088 1261 21 1063 1173 278 1164 207 237 1230 1185 431 232 660 195 1246
49 1100 136 1491 647 1486 112 1278 53 1564 1147 1068 809 1638 138 117
158 3216 1972 2646 3181 785 2937 365 611 1977 1199 2972 201 2432 186 160
244 86 61 38 58 71 243 52 245 264 209 265 308 80 126 129
1317 792 74 111 1721 252 1082 1881 1349 94 891 1458 331 1691 89 1724
3798 202 3140 3468 1486 2073 3872 3190 3481 3760 2876 182 2772 226 3753 188
2272 6876 6759 218 272 4095 4712 6244 4889 2037 234 223 6858 3499 2358 439
792 230 886 824 762 895 99 799 94 110 747 635 91 406 89 157
2074 237 1668 1961 170 2292 2079 1371 1909 221 2039 1022 193 2195 1395 2123
8447 203 1806 6777 278 2850 1232 6369 398 235 212 992 7520 7304 7852 520
3928 107 3406 123 2111 2749 223 125 134 146 3875 1357 508 1534 4002 4417

88
2017/day02/problem Normal file
View File

@ -0,0 +1,88 @@
Advent of Code
--- Day 2: Corruption Checksum ---
As you walk through the door, a glowing humanoid shape yells in your
direction. "You there! Your state appears to be idle. Come help us repair the
corruption in this spreadsheet - if we take another millisecond, we'll have
to display an hourglass cursor!"
The spreadsheet consists of rows of apparently-random numbers. To make sure
the recovery process is on the right track, they need you to calculate the
spreadsheet's checksum. For each row, determine the difference between the
largest value and the smallest value; the checksum is the sum of all of these
differences.
For example, given the following spreadsheet:
5 1 9 5
7 5 3
2 4 6 8
* The first row's largest and smallest values are 9 and 1, and their
difference is 8.
* 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.
What is the checksum for the spreadsheet in your puzzle input?
Your puzzle answer was ____.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
"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.
What is the sum of each row's result in your puzzle input?
Although it hasn't changed, you can still get your puzzle input.
Answer: _____________________
You can also [Shareon Twitter Google+ Reddit] this puzzle.
References
Visible links
. http://adventofcode.com/
. http://adventofcode.com/2017/about
. http://adventofcode.com/2017/support
. http://adventofcode.com/2017/events
. http://adventofcode.com/2017/settings
. http://adventofcode.com/2017/auth/logout
. http://adventofcode.com/2017
. http://adventofcode.com/2017
. http://adventofcode.com/2017/leaderboard
. http://adventofcode.com/2017/stats
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/sponsors
. http://adventofcode.com/2017/day/2/input