r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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.


Advent of Code: The Party Game!

Click here for rules

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

51 Upvotes

407 comments sorted by

View all comments

3

u/muffa Dec 02 '18

My solution, written in golang.

func stringDiffer(str1 string, str2 string) (int, string) {
	diffCounter := 0
	solution := ""
	for i := range str1 {
		if str1[i] == str2[i] {
			solution += string(str1[i])
		} else {
			diffCounter++
		}
	}
	return diffCounter, solution
}

func main() {
	file, _ := ioutil.ReadFile("input_day2.txt")
	answer2 := 0
	answer3 := 0
	// Solution to first part
	for _, line := range strings.Split(string(file), "\n") {
		abcCounter := make(map[rune]int)
		val2 := false
		val3 := false
		for _, char := range line {
			if _, exists := abcCounter[char]; exists {
				abcCounter[char]++
			} else {
				abcCounter[char] = 1
			}
		}
		for _, value := range abcCounter {
			if value == 2 && val2 == false {
				answer2++
				val2 = true
			} else if value == 3 && val3 == false {
				answer3++
			val3 = true
		}
	}
}
fmt.Println(answer2 * answer3)
// Solution to first part
var abc []string
diff := 0
solution := ""
for _, line := range strings.Split(string(file), "\n") {
	abc = append(abc, line)
}
for i := range abc {
	for j := range abc {
		diff, solution = stringDiffer(abc[i], abc[j])
		if diff == 1 {
			fmt.Println(solution)
		}
	}
}

}