-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (41 loc) · 1.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Package day08 - Solution for Advent of Code 2020/08
// Problem Link: http://adventofcode.com/2020/day/08
package day08
import (
_ "embed"
"fmt"
"strings"
)
//go:embed input.txt
var input string
// Run prints out the result of the solution.
func Run() {
fmt.Println(solve(input))
}
func solve(input string) (int, int) {
instructions := parse(input)
return solvePart1(instructions), solvePart2(instructions)
}
func solvePart1(instructions []instruction) int {
c := newConsole(instructions)
c.run()
return c.accumulator
}
func solvePart2(instructions []instruction) (acc int) {
for idx := range instructions {
instructions[idx].swap()
c := newConsole(instructions)
c.run()
if c.completed {
return c.accumulator
}
instructions[idx].swap() // Swap back to original
}
panic("[logical error] unreachable zone")
}
func parse(input string) (instructions []instruction) {
for _, line := range strings.Split(input, "\n") {
instructions = append(instructions, newInstruction(line))
}
return instructions
}