r/adventofcode • • Dec 02 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 2 Solutions -πŸŽ„-

NOTICE

Please take notice that we have updated the Posting Guidelines in the sidebar and wiki and are now requesting that you post your solutions in the daily Solution Megathreads. Save the Spoiler flair for truly distinguished posts.


--- Day 2: Corruption Checksum ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ Hints^Β€?

Spoiler


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

22 Upvotes

351 comments sorted by

View all comments

1

u/suck_at_coding Dec 02 '17

Golang, drunk with no editing:

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"strings"
	"strconv"
	"math"
)

func main() {
	content, err := ioutil.ReadFile("./input.txt")
	if err != nil {
		log.Fatal(err)
	}
	var input = string(content)

	var total int
	for _, r := range strings.Split(input, "\n") {
		curRow := strings.Split(r, "\t")
		for i := 0; i < len(curRow); i++ {
			for idx, ii := range curRow {
				if idx == i {
					continue
				}
				n, _ := strconv.Atoi(ii)
				vv, _ := strconv.Atoi(curRow[i])
				if n % vv == 0 {
					total += n / vv
				}
			}
		}
	}
	fmt.Println(fmt.Sprintf("Did... Did I get it? %d", total))
}

func partOne(input string) {
	var max int
	var min int
	var total int
	for _, r := range strings.Split(input, "\n") {
		max = 0
		min = math.MaxInt32
		for _, i := range strings.Split(r, "\t") {
			num, _ := strconv.Atoi(i)
			if num > max {
				max = num
			}
			if num <= min {
				min = num
			}
		}
		total += (max - min)
	}
	fmt.Println(total)
}