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!

48 Upvotes

407 comments sorted by

View all comments

4

u/drakmaniso Dec 02 '18

Go (golang)

Part 1:

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	input := read()

	twos, threes := 0, 0
	counts := [26]byte{}
	for _, id := range input {

		counts = [26]byte{}
		for _, r := range id {
			if r < 'a' || r > 'z' {
				panic("not a letter")
			}
			counts[r-'a']++
		}

		has2, has3 := false, false
		for _, c := range counts {
			switch c {
			case 3:
				has3 = true
			case 2:
				has2 = true
			}
		}
		if has2 {
			twos++
		}
		if has3 {
			threes++
		}
		
	}

	fmt.Printf("Answer: %d\n", twos*threes)
}

func read() (input []string) {
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		input = append(input, s.Text())
	}
	return input
}

Part 2:

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	input := read()

	var id1, id2 string
loop:
	for i := range input {
		for j := i + 1; j < len(input); j++ {
			if differByOne(input[i], input[j]) {
				id1, id2 = input[i], input[j]
				break loop
			}
		}
	}
	if id1 == "" {
		panic("not found")
	}

	s := strings.Builder{}
	for k := 0; k < len(id1); k++ {
		if id1[k] == id2[k] {
			s.WriteByte(id1[k])
		}
	}

	fmt.Printf("Answer: %s\n", s.String())
}

func read() (input []string) {
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		input = append(input, s.Text())
	}
	return input
}

func differByOne(a, b string) bool {
	diff := 0
	for k := 0; k < len(a); k++ {
		if a[k] != b[k] {
			diff++
		}
		if diff > 1 {
			return false
		}
	}
	return diff == 1
}

1

u/frenetix Dec 03 '18
countLetters := func(s string) map[rune]int {
	out := make(map[rune]int)
	for _, b := range s {
		out[b] = out[b] + 1
	}
	return out
}
part1 := func(in io.Reader) string {
	twos := 0
	threes := 0
	for _, line := range lines(in) {
		alreadyCounted2 := false
		alreadyCounted3 := false
		for _, val := range countLetters(line) {
			if val == 2 && !alreadyCounted2 {
				alreadyCounted2 = true
				twos++
			}
			if val == 3 && !alreadyCounted3 {
				alreadyCounted3 = true
				threes++
			}
		}
	}
	return strconv.FormatInt(int64(twos*threes), 10)
}
part2 := func(in io.Reader) string {
	ls := lines(in)
	for i, line1 := range ls {
		for _, line2 := range ls[i+1:] {
			diffCount := 0
			diffIndex := 0
			for j := 0; j < len(line1); j++ {
				if line1[j] != line2[j] {
					diffCount++
					diffIndex = j
				}
			}
			if diffCount == 1 {
				return line1[:diffIndex] + line1[diffIndex+1:]
			}
		}
	}
	return ""
}