Exemple #1
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)
	guestList := make(map[string]map[string]int)
	table := []string{}

	var guest map[string]int
	var exists bool

	for _, str := range lines {
		name, happiness, nextTo := parse(str)

		if guest, exists = guestList[name]; !exists {
			guest = map[string]int{}
			guestList[name] = guest
			table = append(table, name)
		}

		guest[nextTo] = happiness
	}

	o := optimalSeating(guestList, table)

	// add me
	for _, guest := range guestList {
		guest["me"] = 0
	}
	table = append(table, "me")
	o2 := optimalSeating(guestList, table)

	return o, o2
}
Exemple #2
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)
	lights := []int{}
	width := len(lines[0])

	cnt := 0
	cntstuck := 0

	for _, str := range lines {
		for _, r := range str {
			var state int
			if r == 35 {
				state = ON
			}

			lights = append(lights, state)
		}
	}

	size := len(lights)
	stuck := []int{0, width - 1, size - width, size - 1}

	final := animate(lights, width, []int{}, 100)
	finalStuck := animate(lights, width, stuck, 100)

	for i := 0; i < size; i++ {
		cnt += final[i]
		cntstuck += finalStuck[i]
	}

	return cnt, cntstuck
}
Exemple #3
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)
	var first, second int

	compounds := map[string]int{
		"children":    3,
		"cats":        7,
		"samoyeds":    2,
		"pomeranians": 3,
		"akitas":      0,
		"vizslas":     0,
		"goldfish":    5,
		"trees":       3,
		"cars":        2,
		"perfumes":    1,
	}

	for i, str := range lines {
		if match(parse(str), compounds) {
			first = i + 1
		}
		if _match(parse(str), compounds) {
			second = i + 1
		}
	}

	return first, second
}
Exemple #4
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)

	gridA := new(Grid)
	gridB := new(Grid)

	countA := 0
	countB := 0

	for _, str := range lines {
		cmd := parse(str)

		for x := cmd["xi"]; x <= cmd["xf"]; x++ {
			for y := cmd["yi"]; y <= cmd["yf"]; y++ {
				gridA.light(x, y, cmd["id"])
				gridB._light(x, y, cmd["id"])
			}
		}
	}

	for x := 0; x < 1000; x++ {
		for y := 0; y < 1000; y++ {
			countA += gridA.get(x, y)
			countB += gridB.get(x, y)
		}
	}

	return countA, countB
}
Exemple #5
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)
	circuit := NewCircuit()

	for _, str := range lines {
		// get inputs first
		if isInput, cmd := parse(str); isInput {
			circuit.eval(cmd)
		} else {
			circuit.enqueue(cmd)
		}
	}

	nqueue := circuit.queue

	circuit.consumeQueue()
	a, _ := circuit.get("a")

	// reset circuit to get second star
	circuit.queue = nqueue
	circuit.clearHeap()
	circuit.in("b", a)

	circuit.consumeQueue()
	a2, _ := circuit.get("a")

	return int(a), int(a2)
}
Exemple #6
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)

	shortestTour := int(^uint(0) >> 1)
	longestTour := 0
	m := newMap()

	for _, str := range lines {
		aname, bname, d := parse(str)

		a := m.getsert(aname)
		b := m.getsert(bname)

		a.add(Route{to: b, distance: d})
		b.add(Route{to: a, distance: d})
	}

	for startLocation, _ := range m.locations {
		allVisited, sTour, lTour := m.travel(m.locations[startLocation], 0, 0)

		if allVisited && sTour < shortestTour {
			shortestTour = sTour
		}
		if allVisited && lTour > longestTour {
			longestTour = lTour
		}
	}

	return shortestTour, longestTour
}
Exemple #7
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)

	molecule, reactions := parse(lines)
	plant := newNuclearPlant(reactions)

	return plant.calibrate(molecule), plant.reduce(molecule)
}
Exemple #8
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)

	program := parse(lines)
	incRegAByOne := Instruction{cmd: "inc", reg: "a"}
	program2 := append([]Instruction{incRegAByOne}, program...)

	return int(newMachine().run(program)), int(newMachine().run(program2))
}
Exemple #9
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)
	ingredients := make([]Ingredient, 0)

	for _, str := range lines {
		ingredients = append(ingredients, parse(str))
	}

	return bestCookie(ingredients, false), bestCookie(ingredients, true)
}
Exemple #10
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)
	containers := []int{}

	for _, str := range lines {
		v, _ := strconv.Atoi(str)
		containers = append(containers, v)
	}

	return fill(150, containers), fillMin(150, containers)
}
Exemple #11
0
func Solve(filepath string) (int, int) {
	s := io.Readlines(filepath)[0]
	santa := NewSanta(1)
	santaWithBot := NewSanta(2)

	for _, r := range s {
		santa.move(r)
		santaWithBot.move(r)
	}

	return santa.total, santaWithBot.total
}
Exemple #12
0
func Solve(filepath string) (int, int) {
	lines := io.Readlines(filepath)

	in := []int{}

	for _, s := range lines {
		w, _ := strconv.Atoi(s)
		in = append(in, w)
	}

	return balanceSleigh(in, 3), balanceSleigh(in, 4)
}
Exemple #13
0
func Solve(filepath string) (int, int) {
	strings := io.Readlines(filepath)

	cInString := 0
	cInMemory := 0
	cEncoded := 0

	for _, str := range strings {
		cInString += len(str)
		cInMemory += memorySpace(str)
		cEncoded += len(encode(str))
	}

	return cInString - cInMemory, cEncoded - cInString
}
Exemple #14
0
func Solve(filepath string) (int, int) {
	strings := io.Readlines(filepath)
	count := 0
	_count := 0

	for _, str := range strings {
		if isNice(str) {
			count++
		}
		if _isNice(str) {
			_count++
		}
	}

	return count, _count
}
Exemple #15
0
func Solve(filepath string, timeLimit int) (int, int) {
	lines := io.Readlines(filepath)
	reindeers := make(map[string]*Reindeer)

	for _, str := range lines {
		name, spd, tm, tr := parse(str)

		reindeers[name] = &Reindeer{
			speed:  spd,
			moving: tm,
			rest:   tr,
		}
	}

	return race(reindeers, timeLimit), burstRace(reindeers, timeLimit)
}
Exemple #16
0
func Solve(filepath string) (int, int) {
	presents := io.Readlines(filepath)
	tpaper := 0
	tribbon := 0

	for _, box := range presents {
		l, w, h := getSize(box)
		paper := 2*(l*w+w*h+h*l) + l*w
		ribbon := 2*(l+w) + (l * w * h)

		tpaper += paper
		tribbon += ribbon
	}

	return tpaper, tribbon
}
Exemple #17
0
func Solve(filepath string) (int, int) {
	s := io.Readlines(filepath)[0]
	c := 0
	var ibelowzero int

	for i, r := range s {
		if r == 40 {
			c++
		} else {
			c--
		}

		if ibelowzero == 0 && c < 0 {
			ibelowzero = i + 1
		}
	}

	return c, ibelowzero
}
Exemple #18
0
func Solve(filepath string) (int, int) {
	blob := io.Readlines(filepath)[0]

	return addAll(blob), ignoreRed(blob)
}