AoC 2020: day4

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-12-04 11:38:40 +01:00
parent 12c90115a7
commit 5e8eb10c92
46 changed files with 1174 additions and 0 deletions

41
2019-go/day_01/day_01.go Normal file
View File

@@ -0,0 +1,41 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func requiredFuel(mass int) int {
// Part 1
fuelReq := (mass / 3) - 2
// Part 2
if fuelReq <= 0 {
return 0
}
return fuelReq + requiredFuel(fuelReq)
}
func main() {
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
var fuelReq int = 0
scan := bufio.NewScanner(file)
for scan.Scan() {
mass, err := strconv.Atoi(scan.Text())
if err != nil {
log.Printf("Error converting line to int: %v", err)
} else {
fuelReq += requiredFuel(mass)
}
}
fmt.Println(fuelReq)
}

100
2019-go/day_01/input Normal file
View File

@@ -0,0 +1,100 @@
125050
115884
132344
67441
119823
86204
111093
99489
67860
51288
62815
65263
56540
81380
96101
116351
56330
123123
133969
115050
137851
136900
71254
53458
139976
140218
117085
52241
71251
136110
103784
132893
140216
85568
94327
85200
136753
110917
147197
120161
81684
56987
143452
94728
138355
54577
59898
69123
133769
118418
93530
50297
71543
113383
135203
140129
70977
58566
129593
137456
130100
130915
88872
96014
62746
127048
89522
62021
85363
143611
135995
65836
146022
119911
127381
121007
71577
129637
90271
54640
117213
116151
114022
107683
102079
94388
135676
69019
104056
124799
107998
148696
122793
135417
52981
122890
142491
88137
57609
54921

95
2019-go/day_02/day_02.go Normal file
View File

@@ -0,0 +1,95 @@
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
)
func readValues(fileName string) []int {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
r := csv.NewReader(file)
recordsMatrix, err := r.ReadAll()
if err != nil {
log.Fatal(err)
}
values := make([]int, len(recordsMatrix[0]))
for i, value := range recordsMatrix[0] {
values[i], err = strconv.Atoi(value)
if err != nil {
log.Printf("Error converting line to int: %v", err)
}
}
return values
}
func restore1202(values []int) []int {
restoredValues := append(values[:0:0], values...)
restoredValues[1] = 12
restoredValues[2] = 2
return restoredValues
}
func findNounVerb(values []int) int {
testValues := append(values[:0:0], values...)
for noun := 0; noun < 100; noun++ {
for verb := 0; verb < 100; verb++ {
testValues[1] = noun
testValues[2] = verb
output := runIntcode(testValues)
if output == 19690720 {
return 100*noun + verb
}
}
}
return -1
}
func runIntcode(values []int) int {
processedValues := append(values[:0:0], values...)
loop:
for i := 0; i < len(processedValues); i += 4 {
num1 := processedValues[processedValues[i+1]]
num2 := processedValues[processedValues[i+2]]
pos := processedValues[i+3]
switch processedValues[i] {
case 1:
processedValues[pos] = num1 + num2
case 2:
processedValues[pos] = num1 * num2
case 99:
break loop
default:
log.Printf("Error evaluating opcode %d\n", processedValues[i])
return -1
}
}
return processedValues[0]
}
func main() {
values := readValues("./input")
// Part 1
restoredValues := restore1202(values)
initialValue := runIntcode(restoredValues)
fmt.Printf("Part 1: %d\n", initialValue)
//Part 2
nounVerb := findNounVerb(values)
fmt.Printf("Part 2: %d\n", nounVerb)
}

1
2019-go/day_02/input Normal file
View File

@@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,6,19,1,19,6,23,2,23,6,27,2,6,27,31,2,13,31,35,1,9,35,39,2,10,39,43,1,6,43,47,1,13,47,51,2,6,51,55,2,55,6,59,1,59,5,63,2,9,63,67,1,5,67,71,2,10,71,75,1,6,75,79,1,79,5,83,2,83,10,87,1,9,87,91,1,5,91,95,1,95,6,99,2,10,99,103,1,5,103,107,1,107,6,111,1,5,111,115,2,115,6,119,1,119,6,123,1,123,10,127,1,127,13,131,1,131,2,135,1,135,5,0,99,2,14,0,0

120
2019-go/day_03/day_03.go Normal file
View File

@@ -0,0 +1,120 @@
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
mapset "github.com/deckarep/golang-set"
)
type point struct {
x int
y int
}
func readValues(fileName string) [][]string {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
values := make([][]string, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lineElements := strings.Split(scanner.Text(), ",")
values = append(values, lineElements)
}
return values
}
func wireTrace(cableMoves []string) ([]point, mapset.Set) {
currentPosition := point{0, 0}
cablePath := make([]point, 0)
cableSet := mapset.NewSet()
for _, step := range cableMoves {
direction := step[0]
distance, err := strconv.Atoi(string(step[1:]))
if err != nil {
log.Fatalf("Cannot decode step lenght: %c\n", step[1])
}
for i := 0; i < distance; i++ {
switch direction {
case 'U':
currentPosition.y++
case 'D':
currentPosition.y--
case 'R':
currentPosition.x++
case 'L':
currentPosition.x--
default:
log.Fatalf("Cannot decode step direction: %c\n", direction)
}
cablePath = append(cablePath, currentPosition)
cableSet.Add(currentPosition)
}
}
return cablePath, cableSet
}
func (p1 point) manhattanDistance(p2 point) int {
return int(math.Abs(float64(p1.x-p2.x)) + math.Abs(float64(p1.y-p2.y)))
}
func reachIntersect(cablePath []point, intersect point) int {
for i, pos := range cablePath {
if pos.x == intersect.x && pos.y == intersect.y {
return i + 1
}
}
return -1
}
func shortestDistance(intersectionPoints mapset.Set) int {
origin := point{0, 0}
bestDistance := 0
for p := range intersectionPoints.Iterator().C {
dist := origin.manhattanDistance(p.(point))
if bestDistance == 0 || dist < bestDistance {
bestDistance = dist
}
}
return bestDistance
}
func fewestSteps(cablesPath [][]point, intersectionPoints mapset.Set) int {
bestSteps := 0
for p := range intersectionPoints.Iterator().C {
steps := reachIntersect(cablesPath[0], p.(point)) + reachIntersect(cablesPath[1], p.(point))
if bestSteps == 0 || steps < bestSteps {
bestSteps = steps
}
}
return bestSteps
}
func main() {
cablesMoves := readValues("./input")
cable1Path, cable1Set := wireTrace(cablesMoves[0])
cable2Path, cable2Set := wireTrace(cablesMoves[1])
intersectionPoints := cable1Set.Intersect(cable2Set)
if intersectionPoints.Cardinality() == 0 {
log.Fatalln("No intersection point")
}
// Part 1
dist := shortestDistance(intersectionPoints)
fmt.Printf("Part 1: %d\n", dist)
//Part 2
steps := fewestSteps([][]point{cable1Path, cable2Path}, intersectionPoints)
fmt.Printf("Part 2: %d\n", steps)
}

2
2019-go/day_03/input Normal file
View File

@@ -0,0 +1,2 @@
R997,U849,R349,U641,R581,D39,R285,U139,R455,D346,L965,D707,R393,D302,L263,U58,R950,U731,R858,D748,R302,U211,R588,D441,L153,D417,R861,U775,R777,U204,R929,U868,L62,U163,R841,D214,L648,U626,R501,D751,L641,D961,L23,D430,L73,D692,R49,U334,L601,U996,R444,D658,R633,D30,L861,D811,R10,D394,R9,U227,L848,U420,L378,D622,L501,U397,R855,U369,R615,U591,L674,D166,L181,U61,L224,U463,L203,U594,R93,U614,L959,U198,L689,D229,L674,U255,R843,D382,R538,U923,L960,D775,L879,U97,R137,U665,L340,D941,L775,D57,R852,D167,R980,U704,L843,U989,L611,D32,L724,D790,L32,U984,L39,U671,L994,U399,R475,D85,L322,D936,R117,D261,R705,D696,L523,D433,L239,U477,L247,D465,R560,D902,L589,U682,R645,U376,L989,D121,L215,U514,R519,U407,L218,D444,R704,D436,L680,U759,R937,U400,R533,D860,R782,D233,R840,D549,L508,U380,L992,U406,L213,D403,L413,D532,L429,U186,R262,U313,L913,U873,L838,D882,R851,U70,R185,D131,R945,D595,L330,U446,R88,D243,L561,D952,R982,D395,L708,U459,L82,D885,L996,U955,L406,U697,L183,U266,L878,D839,R843,D891,R118,U772,R590,D376,L500,U370,R607,D12,L310,D436,L602,D365,R886,U239,L471,D418,L122,U18,R879,D693,R856,U848,L657,D911,L63,U431,R41,U752,R919,U323,L61,D263,L370,D85,R929,D213,R350,U818,R458,D912,R509,U394,L734,U49,R810,D87,L870,U658,R499,U550,L402,U244,L112,U859,R836,U951,R222,D944,L691,D731,R742,D52,R984,D453,L514,U692,R812,U35,L844,D177,L110,D22,R61,U253,R618,D51,R163,U835,R704,U148,R766,U297,R457,D170,L104,D441,R330,D330,R989,D538,R668,D811,R62,D67,L470,D526,R788,U376,R708,U3,R961
L1009,D381,R970,U429,L230,D909,R516,D957,R981,U609,L480,D139,L861,U168,L48,U620,R531,D466,L726,D380,R977,D454,L318,D397,R994,U402,L77,U93,L359,D72,R968,D956,L174,D22,R218,U619,R593,U32,L154,U55,L169,U415,L171,U666,R617,U109,L265,U773,R541,D808,L797,U478,R731,U379,R311,D137,L806,U298,R362,D458,L254,D539,R700,U853,R246,D588,L28,U203,L432,U946,R663,D408,R974,U59,L683,D36,L139,U738,L780,U414,L401,D93,R212,D973,L710,U892,L357,D177,R823,D4,R46,D924,L235,D898,R67,U220,L592,U87,R94,U584,R979,D843,L299,D648,L491,U360,R824,D245,L944,D24,R616,U975,L4,U42,L984,U181,R902,D835,L687,D413,L767,U632,L754,U270,R413,U51,L825,D377,L596,U960,L378,U706,L859,D708,L156,D991,L814,U351,R923,D749,L16,D651,R20,D86,R801,U811,L228,U161,L871,U129,R215,U235,L784,U896,R94,U145,R822,U494,R248,D98,R494,U156,L495,U311,R66,D873,L294,D620,L885,U395,R778,D227,R966,U756,L694,D707,R983,D950,R706,D730,R415,U886,L465,D622,L13,D938,R324,D464,R723,U804,R942,D635,L729,D317,L522,U469,R550,D141,R302,U999,L642,U509,R431,D380,R18,D676,R449,D759,L495,U901,R1,D745,L655,U449,L439,D818,R55,D541,R420,U764,L426,D176,L520,U3,L663,D221,L80,D449,L987,U349,L71,U632,L887,D231,R655,D208,R698,D639,R804,U616,R532,U846,R363,D141,R659,U470,L798,U144,L675,U483,L944,U380,L329,U72,L894,D130,R53,U109,R610,U770,R778,U493,R972,D340,L866,U980,L305,D812,R130,D954,R253,D33,L912,U950,L438,D680,R891,U725,R171,D587,R549,D367,L4,U313,R522,D128,L711,D405,L769,D496,L527,U373,R725,D261,L268,D939,L902,D58,L858,D190,L442

67
2019-go/day_04/day_04.go Normal file
View File

@@ -0,0 +1,67 @@
package main
import "fmt"
const (
min = 130254
max = 678275
)
func intToSlice(n int) []int {
s := make([]int, 0)
for n != 0 {
r := n % 10
n = n / 10
s = append([]int{r}, s...)
}
return s
}
func twoEqualAdjacentDigits(pwd []int) bool {
l := len(pwd)
for i := 0; i < l-1; i++ {
if pwd[i] == pwd[i+1] {
return true
}
}
return false
}
func twoExclusiveAdjacentDigits(pwd []int) bool {
l := len(pwd)
for i := 0; i < l-1; i++ {
if pwd[i] == pwd[i+1] && (i == 0 || pwd[i] != pwd[i-1]) && (i == l-2 || pwd[i] != pwd[i+2]) {
return true
}
}
return false
}
func increasingDigits(pwd []int) bool {
for i, digit := range pwd {
if i < len(pwd)-1 && digit > pwd[i+1] {
return false
}
}
return true
}
func main() {
// Part 1:
count1, count2 := 0, 0
for i := min; i < max; i++ {
pwd := intToSlice(i)
two := twoEqualAdjacentDigits(pwd)
onlyTwo := twoExclusiveAdjacentDigits(pwd)
if increasingDigits(pwd) {
if two {
count1++
}
if onlyTwo {
count2++
}
}
}
fmt.Printf("Part 1: %d\n", count1)
fmt.Printf("Part 2: %d\n", count2)
}

121
2019-go/day_05/day_05.go Normal file
View File

@@ -0,0 +1,121 @@
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
const (
opcodeAdd = 1
opcodeMultiply = 2
opcodeInput = 3
opcodeOutput = 4
opcodeJumpIfTrue = 5
opcodeJumpIfFalse = 6
opcodeLessThan = 7
opcodeEquals = 8
opcodeHalt = 99
paramModePosition = 0
paramModeImmediate = 1
)
func readValues(fileName string) []int {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var values []int
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lineElements := strings.Split(scanner.Text(), ",")
for _, elem := range lineElements {
n, err := strconv.Atoi(elem)
if err != nil {
log.Fatalf("Cannot convert to int: %s\n", elem)
}
values = append(values, n)
}
}
return values
}
func runIntcode(values []int) {
instructionPointer := 0
for values[instructionPointer] != opcodeHalt {
instruction := values[instructionPointer]
opCode := instruction % 100
paramAddress := func(n int) int {
mode := (instruction % int(math.Pow10(n+2))) / int(math.Pow10(n+1))
switch mode {
case paramModeImmediate:
return instructionPointer + n
default:
return values[instructionPointer+n]
}
}
switch opCode {
case opcodeInput:
var input int
fmt.Print("Input: ")
_, err := fmt.Scanf("%d", &input)
if err != nil {
log.Fatalf("Cannot read integer from stdin: %s", err)
}
values[paramAddress(1)] = input
instructionPointer += 2
case opcodeOutput:
fmt.Printf("Output: %d\n", values[paramAddress(1)])
instructionPointer += 2
case opcodeAdd:
values[paramAddress(3)] = values[paramAddress(1)] + values[paramAddress(2)]
instructionPointer += 4
case opcodeMultiply:
values[paramAddress(3)] = values[paramAddress(1)] * values[paramAddress(2)]
instructionPointer += 4
case opcodeJumpIfTrue:
if values[paramAddress(1)] != 0 {
instructionPointer = values[paramAddress(2)]
} else {
instructionPointer += 3
}
case opcodeJumpIfFalse:
if values[paramAddress(1)] == 0 {
instructionPointer = values[paramAddress(2)]
} else {
instructionPointer += 3
}
case opcodeLessThan:
if values[paramAddress(1)] < values[paramAddress(2)] {
values[paramAddress(3)] = 1
} else {
values[paramAddress(3)] = 0
}
instructionPointer += 4
case opcodeEquals:
if values[paramAddress(1)] == values[paramAddress(2)] {
values[paramAddress(3)] = 1
} else {
values[paramAddress(3)] = 0
}
instructionPointer += 4
default:
log.Fatalf("Unknown opCode: %d\n", opCode)
}
}
}
func main() {
values := readValues("./input")
runIntcode(values)
}

1
2019-go/day_05/input Normal file
View File

@@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1102,89,49,225,1102,35,88,224,101,-3080,224,224,4,224,102,8,223,223,1001,224,3,224,1,223,224,223,1101,25,33,224,1001,224,-58,224,4,224,102,8,223,223,101,5,224,224,1,223,224,223,1102,78,23,225,1,165,169,224,101,-80,224,224,4,224,102,8,223,223,101,7,224,224,1,224,223,223,101,55,173,224,1001,224,-65,224,4,224,1002,223,8,223,1001,224,1,224,1,223,224,223,2,161,14,224,101,-3528,224,224,4,224,1002,223,8,223,1001,224,7,224,1,224,223,223,1002,61,54,224,1001,224,-4212,224,4,224,102,8,223,223,1001,224,1,224,1,223,224,223,1101,14,71,225,1101,85,17,225,1102,72,50,225,1102,9,69,225,1102,71,53,225,1101,10,27,225,1001,158,34,224,101,-51,224,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,102,9,154,224,101,-639,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,108,226,226,224,102,2,223,223,1006,224,329,101,1,223,223,1007,677,677,224,1002,223,2,223,1005,224,344,1001,223,1,223,8,226,677,224,1002,223,2,223,1006,224,359,1001,223,1,223,108,226,677,224,1002,223,2,223,1005,224,374,1001,223,1,223,107,226,677,224,102,2,223,223,1006,224,389,101,1,223,223,1107,226,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,1107,677,226,224,102,2,223,223,1005,224,419,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,434,1001,223,1,223,1108,677,226,224,1002,223,2,223,1005,224,449,101,1,223,223,1008,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,7,226,677,224,102,2,223,223,1006,224,479,101,1,223,223,1008,226,677,224,1002,223,2,223,1006,224,494,101,1,223,223,1107,226,677,224,1002,223,2,223,1005,224,509,1001,223,1,223,1108,226,226,224,1002,223,2,223,1006,224,524,101,1,223,223,7,226,226,224,102,2,223,223,1006,224,539,1001,223,1,223,107,226,226,224,102,2,223,223,1006,224,554,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,569,101,1,223,223,1008,677,677,224,1002,223,2,223,1006,224,584,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,599,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,614,101,1,223,223,108,677,677,224,102,2,223,223,1005,224,629,1001,223,1,223,8,677,677,224,1002,223,2,223,1005,224,644,1001,223,1,223,7,677,226,224,102,2,223,223,1006,224,659,1001,223,1,223,1007,226,677,224,102,2,223,223,1005,224,674,101,1,223,223,4,223,99,226

81
2019-go/day_06/day_06.go Normal file
View File

@@ -0,0 +1,81 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func readValues(fileName string) map[string]string {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
orbits := make(map[string]string)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
value := scanner.Text()
planets := strings.Split(value, ")")
orbits[planets[1]] = planets[0]
}
return orbits
}
func getOrbitsNum(planet string, orbits map[string]string) int {
orbitsNum := 0
key := planet
orbit, hasOrbit := orbits[key]
for hasOrbit {
orbitsNum++
key = orbit
orbit, hasOrbit = orbits[key]
}
return orbitsNum
}
func getMinOrbitalTransfers(from string, to string, orbits map[string]string) int {
path := make(map[string]int)
orbit, hasOrbit := orbits[from]
distance := 0
for hasOrbit {
distance++
path[orbit] = distance
orbit, hasOrbit = orbits[orbit]
}
orbit, hasOrbit = orbits[to]
distance = 0
for hasOrbit {
dist, isInPath := path[orbit]
if isInPath {
distance += dist
return distance - 1
}
distance++
orbit, hasOrbit = orbits[orbit]
}
return -1
}
func main() {
orbits := readValues("./input")
orbitsNum := 0
for planet := range orbits {
orbitsNum += getOrbitsNum(planet, orbits)
}
fmt.Printf("Part 1: %d\n", orbitsNum)
minDist := getMinOrbitalTransfers("YOU", "SAN", orbits)
fmt.Printf("Part 2: %d\n", minDist)
}

1151
2019-go/day_06/input Normal file

File diff suppressed because it is too large Load Diff

161
2019-go/day_07/day_07.go Normal file
View File

@@ -0,0 +1,161 @@
package main
import (
"bufio"
"log"
"math"
"os"
"strconv"
"strings"
"sync"
)
const (
opcodeAdd = 1
opcodeMultiply = 2
opcodeInput = 3
opcodeOutput = 4
opcodeJumpIfTrue = 5
opcodeJumpIfFalse = 6
opcodeLessThan = 7
opcodeEquals = 8
opcodeHalt = 99
paramModePosition = 0
paramModeImmediate = 1
)
func readValues(fileName string) []int {
file, err := os.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var values []int
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lineElements := strings.Split(scanner.Text(), ",")
for _, elem := range lineElements {
n, err := strconv.Atoi(elem)
if err != nil {
log.Fatalf("Cannot convert to int: %s\n", elem)
}
values = append(values, n)
}
}
return values
}
func runIntcode(program []int, input <-chan int, output chan<- int, wg *sync.WaitGroup) {
values := append([]int(nil), program...)
instructionPointer := 0
for values[instructionPointer] != opcodeHalt {
instruction := values[instructionPointer]
opCode := instruction % 100
paramAddress := func(n int) int {
mode := (instruction % int(math.Pow10(n+2))) / int(math.Pow10(n+1))
switch mode {
case paramModeImmediate:
return instructionPointer + n
default:
return values[instructionPointer+n]
}
}
switch opCode {
case opcodeInput:
values[paramAddress(1)] = <-input
instructionPointer += 2
case opcodeOutput:
output <- values[paramAddress(1)]
instructionPointer += 2
case opcodeAdd:
values[paramAddress(3)] = values[paramAddress(1)] + values[paramAddress(2)]
instructionPointer += 4
case opcodeMultiply:
values[paramAddress(3)] = values[paramAddress(1)] * values[paramAddress(2)]
instructionPointer += 4
case opcodeJumpIfTrue:
if values[paramAddress(1)] != 0 {
instructionPointer = values[paramAddress(2)]
} else {
instructionPointer += 3
}
case opcodeJumpIfFalse:
if values[paramAddress(1)] == 0 {
instructionPointer = values[paramAddress(2)]
} else {
instructionPointer += 3
}
case opcodeLessThan:
if values[paramAddress(1)] < values[paramAddress(2)] {
values[paramAddress(3)] = 1
} else {
values[paramAddress(3)] = 0
}
instructionPointer += 4
case opcodeEquals:
if values[paramAddress(1)] == values[paramAddress(2)] {
values[paramAddress(3)] = 1
} else {
values[paramAddress(3)] = 0
}
instructionPointer += 4
default:
log.Fatalf("Unknown opCode: %d\n", opCode)
}
}
wg.Done()
}
func emulateAmplifiers(program []int, phases []int) int {
var wg sync.WaitGroup
fromEtoA := make(chan int, 1)
fromAtoB := make(chan int)
fromBtoC := make(chan int)
fromCtoD := make(chan int)
fromDtoE := make(chan int)
fromAtoB <- phases[0]
fromBtoC <- phases[1]
fromCtoD <- phases[2]
fromDtoE <- phases[3]
fromEtoA <- phases[4]
fromEtoA <- 0
wg.Add(5)
go runIntcode(program, fromEtoA, fromAtoB, &wg)
go runIntcode(program, fromAtoB, fromBtoC, &wg)
go runIntcode(program, fromBtoC, fromCtoD, &wg)
go runIntcode(program, fromCtoD, fromDtoE, &wg)
go runIntcode(program, fromDtoE, fromEtoA, &wg)
fromAtoB <- phases[0]
fromBtoC <- phases[1]
fromCtoD <- phases[2]
fromDtoE <- phases[3]
fromEtoA <- phases[4]
fromEtoA <- 0
wg.Wait()
return <-fromEtoA
}
func main() {
//values := readValues("./input")
}

1
2019-go/day_07/input Normal file
View File

@@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,34,43,64,85,98,179,260,341,422,99999,3,9,1001,9,3,9,102,3,9,9,4,9,99,3,9,102,5,9,9,4,9,99,3,9,1001,9,2,9,1002,9,4,9,1001,9,3,9,1002,9,4,9,4,9,99,3,9,1001,9,3,9,102,3,9,9,101,4,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,99