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

959
2018-go/day_01/input Normal file
View File

@@ -0,0 +1,959 @@
+12
-10
-4
-8
+18
-1
-13
+10
-13
+5
+7
+6
+14
-9
-8
+7
+18
-11
+13
-1
+20
+11
+13
-2
+3
-8
-16
+4
-6
-3
-9
-12
+16
+17
-5
-11
+2
-20
-15
+5
+3
+11
-13
-18
-13
-6
-19
+6
-15
-11
+16
+1
+15
+5
-18
+14
-5
-15
-3
+11
-17
+19
+12
+5
+8
-2
+10
+13
+8
+19
+8
+17
-6
+8
+4
-9
+19
+13
+11
-2
+20
+3
-16
-18
+3
-5
-15
+18
+18
+9
-18
-19
+17
-16
+15
-12
+2
-20
+7
-3
-9
+10
+4
+20
-13
-17
-23
+7
+18
+21
+16
+17
+5
-14
+19
-18
-10
+7
+1
-13
+16
-21
+12
+19
+11
-5
+14
+11
-9
+19
-2
-7
+3
-20
+9
-16
+9
+10
+17
-18
-12
-11
-11
+20
-18
-12
-5
-7
+15
+20
+18
+23
+17
-7
+5
-7
+8
-2
-15
+5
-17
+1
-5
-8
+7
+15
+5
+1
+15
-11
-1
+17
+15
+18
+7
-3
-16
-7
+18
-6
+17
-2
+20
-2
-7
-17
+20
-1
+8
-16
-12
-6
-3
+19
+19
+18
-7
+17
+5
+19
-15
-13
+2
+14
+2
+1
+13
-7
+2
-10
-10
-15
+9
+9
+14
+7
+4
-15
-5
-17
-5
-9
+13
+6
+2
-15
-11
+17
-19
+14
-13
+3
+1
-18
-15
-17
-12
+13
-17
+5
-9
+2
+5
-8
-8
-19
+11
+20
+11
+16
-7
+20
+3
-21
-8
-11
-9
+8
-15
-20
-20
-17
+24
+17
-12
-20
-12
-3
+14
-18
+13
-74
-19
-13
+2
+23
-13
+24
-17
-5
+14
-10
-2
-10
-7
-6
-23
-10
-3
+9
-11
+8
+16
-8
-28
-10
+17
-9
-10
-9
-8
+2
+5
-19
+11
-16
+8
-14
-5
+8
+4
-3
+8
-7
+4
+1
-2
-7
+15
+3
+10
+4
+1
-14
-18
+15
-21
+3
+14
-15
-13
-14
-6
+15
+1
+11
+26
+19
-16
+5
-2
-9
+12
+19
-11
+23
-3
+15
-13
+14
-9
-26
+17
+63
-4
-20
-10
-39
-55
-6
-15
-9
-16
-17
-14
-12
+21
+1
-2
-16
-13
-21
+26
+27
+17
+6
+2
-18
+1
+18
+25
-3
+19
-35
-26
+4
-48
-3
+1
+20
+8
-27
-25
-8
-10
-24
+3
-61
+64
+17
+21
+84
+49
+4
-233
-2
+535
+55053
-1
-12
+2
-19
+13
+10
+10
+15
-12
+13
+15
+8
-15
-13
+10
-7
+3
-10
+16
+12
-6
+4
+3
+19
+14
+2
+10
+4
+16
-2
-8
+18
-14
-1
+4
-1
+15
+10
-6
+17
+17
+15
+8
+15
+6
+9
+19
-2
+13
-19
-15
-6
-15
-4
-10
-1
-4
-18
+15
-2
+3
+13
+5
+14
+15
-19
+7
+13
-14
-11
+10
+8
+13
-16
+5
-13
-16
+10
-5
-9
-1
+2
+15
-4
-19
-7
+19
+12
+24
+10
+10
+18
+15
+12
-5
-11
+5
+10
-12
-7
-14
+12
-17
-13
-20
+14
-13
+11
-16
+15
+15
+18
-8
+10
+17
+1
-10
+19
+8
+7
+4
+11
+11
-15
-13
+18
-17
+19
+5
-6
-9
+6
+18
-10
+9
-18
-17
-2
-14
-19
-1
-8
-10
-7
-18
+3
+14
+6
+14
-17
+18
+10
+23
+4
-14
+12
-4
-19
+15
+20
-5
+15
+16
-21
+14
-15
-17
-15
+7
+10
+18
+21
+14
+6
+4
+6
+17
-18
-14
-18
-8
+24
+10
+8
-13
+18
+3
-18
-2
-3
-8
-1
-1
-16
+9
-4
+10
+20
+18
+9
-1
+17
-2
+16
+16
-17
+13
+5
-6
-15
-19
+11
-17
-8
+10
-1
+10
-22
-15
+11
+20
-14
+2
+11
+17
+17
+18
-8
+7
+8
-14
+19
-6
+2
+10
+6
+5
+10
+3
-8
-4
-13
-15
-16
-5
-19
+16
-15
+5
-27
-18
+3
-7
+21
-8
-15
-4
-5
-8
+3
+22
+6
+8
+12
-27
+32
-4
+2
+4
-5
+26
-5
-3
-4
+21
+34
+4
-6
+17
-6
+7
+7
+10
-19
+20
+2
+14
+14
+17
+5
+16
-17
+11
-13
-5
+15
+7
-18
-9
-13
-2
-14
-2
-4
-3
-7
+12
+14
-9
+20
+7
+7
+15
+12
-3
-14
-1
+4
-16
+14
-3
-16
-8
+14
+18
+14
-5
-6
+7
+14
+16
-22
-16
-13
-9
-13
-1
-15
-9
-6
+20
+17
+1
-3
+44
+17
+17
+21
+9
+20
-17
+11
+10
-15
+10
+3
+4
+16
+8
-7
+17
+8
+15
-9
+4
+28
+8
-1
-6
-10
-65
+4
-42
-14
-14
-24
-15
+6
+13
-72
-1
-17
-5
+7
-27
+19
-21
-2
+20
-2
+3
-34
+30
+23
-30
-5
+20
-27
+11
-27
-7
-23
+39
-225
+15
+60
+472
+54569
+1
+15
+13
+6
-10
-13
-2
-6
+13
+2
+5
-10
-5
-11
-1
+18
-12
-15
-7
-1
+13
-18
-1
-5
-2
-14
-5
-4
-16
-10
+8
-1
+20
+11
+15
-3
-17
+8
+11
-6
-9
-8
-11
-1
-13
-3
-8
+12
+18
-11
-17
-12
+6
-7
-3
+15
+9
+13
-14
+18
+20
-6
-12
+2
+20
-12
-23
-17
-7
+1
-24
-17
-7
-18
+3
-13
+14
+2
-12
-16
+2
-35
-3
+2
-18
-14
-13
+10
-3
-17
+4
+23
+16
-7
-30
+29
-19
+18
-3
+20
-7
-3
+77
+1
+42
+18
+9
-1
+23
+57
+8
-12
+1
+5
+12
+3
+10
-1
+14
+20
-7
-3
-14
-12
+9
+16
+16
+12
+14
+11
+11
+2
+1
+19
-10
+18
-17
-9
-11
-8
+10
-1
-110292

62
2018-go/day_01/main.go Normal file
View File

@@ -0,0 +1,62 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func main() {
//count to maintain the total sum of the frequence changes
count := 0
//current to maintain the partial frequence at a stage
current := 0
//freqChange to store the individual frequence changes
var freqChange []int
//freqList to store all the stage frequences
freqList := map[int]bool{0: true}
//We open the input file
file, err := os.Open("./input")
if err != nil {
//If there is an error we print it in the stderr and exit(1)
log.Fatal(err)
}
//We defer the closure of the file
defer file.Close()
//We create a scanner to the file
scan := bufio.NewScanner(file)
//We iterate throught the file line
for scan.Scan() {
//We convert each line into an int
num, err := strconv.Atoi(scan.Text())
if err != nil {
//If the conversion generate an error we print it to stderr
log.Printf("Error converting line to int: %v", err)
} else {
//Otherwise we add the current freq change to count
count += num
//And we append it to freqChange
freqChange = append(freqChange, num)
}
}
fmt.Printf("Part One: %v\n", count)
for {
for _, freqMod := range freqChange {
current += freqMod
_, found := freqList[current]
if found {
fmt.Printf("Part Two: %v\n", current)
os.Exit(0)
} else {
freqList[current] = true
}
}
}
}

250
2018-go/day_02/input Normal file
View File

@@ -0,0 +1,250 @@
xrecqmdonskvzupalfkwhjctdb
xrlgqmavnskvzupalfiwhjctdb
xregqmyonskvzupalfiwhjpmdj
areyqmyonskvzupalfiwhjcidb
xregqpyonskvzuaalfiwhjctdy
xwegumyonskvzuphlfiwhjctdb
xregumymnskvzupalfiwhjctib
xregqmyonjkvzupalfvwijctdb
xrmgqmyonsdvzupalfiwhjcthb
xrpgqmyonskvzupalfiwhcitdb
xregvmysnsjvzupalfiwhjctdb
xregqsyonskvzupgqfiwhjctdb
qreuqmyonskvzupalfiwyjctdb
xrecqmyenskvzupalyiwhjctdb
xmegqmyonskvzhpalfcwhjctdb
xiegqmyonskvzupalfkwhjjtdb
xreaqmyofskfzupalfiwhjctdb
xregqmypnskvzupalmiwhjltdb
xretqmmonskvzupalfiwhwcfdb
xrexqmkonskvzupalfiwjjctdb
xrigqmyonskvgupplfiwhjctdb
xregqmyotskvzupalfywhjctdm
xcegmmyonsvvzupalfiwhjctdb
xrezqmypnskvznpalfiwhjctdb
xragqmyonskvzupblfiwajctdb
xregqmyonskvzwpapfiwhjctqb
xoegqmyoyskvzupaufiwhjctdb
xrcgqmyjnskvzupalfcwhjctdb
xregqmyonskvzudalfipajctdb
xsegqmyonsklzupalwiwhjctdb
xregqmyocskvduaalfiwhjctdb
xhegqmyfnskvzupalflwhjctdb
xregqmymnykvzupalfiwhjctdm
xregqmybnskvzupacfiwdjctdb
xaegqmlonskvzfpalfiwhjctdb
xoegtmyonskvzupalfiwhwctdb
xregqmyohskvzupaqfiwhjccdb
xoegqmyonstvzupalfiwhjctbb
mregnmyonskszupalfiwhjctdb
xreoqmycnskvzupalfiphjctdb
xregqmyocskvdupacfiwhjctdb
xregqmyonskvzupajqiahjctdb
xregqmyonslvwupalfiwhjcfdb
xregqmyonskvzapalfiwhqcthb
xrerqmyonskwzupalfiwhjctdt
xrefqmfonskvzupalfiwcjctdb
xregqmyonskvzupadfiwhjxedb
iregqhyonskvzupaliiwhjctdb
iregqmyotskvzucalfiwhjctdb
xrbgqmaonrkvzupalfiwhjctdb
xregqmyonskvzupalfixhdctdf
xrehqmyonskvzupalfiwijctdd
xvegqmyonskvzupaleuwhjctdb
xregqmyiyskvzupalfiwqjctdb
hregqmyonskvzupaxfiwhjptdb
xregamyznskbzupalfiwhjctdb
xreyqmyonskvgupalziwhjctdb
xregqmysnskvzupalfiwhgctdu
xojgqmyonskvzupalfiwbjctdb
xrkgqmyonskvlupalfiwhjcwdb
xregqmyonwkvxupalfiwajctdb
xregqmyonsuvzupalfjwhjcxdb
xregqmyonskgzucalfiwhjstdb
xaegqmyonfkvzupalfiwhjcttb
xlegqmyonskvzupazfiwhjctqb
xrejqmyonskvzqpaldiwhjctdb
xreguryonskvzupalfiwhjctdz
xregqsyoeskvzupalfiwhjctdt
xregqmyonskvzubalfirhjctdp
xrepqmymnskvzupadfiwhjctdb
xregqayonskvzuoalfichjctdb
xreqqmyonskvzunalfiwojctdb
xregqmyonsivzufalciwhjctdb
xregqqeonskvzupanfiwhjctdb
xoegqmyunskvzppalfiwhjctdb
xregqmyonskvzupalfqwhnftdb
xregqmyonskvzuralkiwhjcudb
xrwgqmymnskvzupalfiwhjcgdb
xvrgqmyonskvzupalfiwhjcthb
xregemyonskkzupalfiwhjctbb
xregqmyonsevzupalfiwhjjtdl
xregqmyonckvcupajfiwhjctdb
xregqmysnskvzunalfnwhjctdb
xreowmyonskvkupalfiwhjctdb
xregqmyonskvjupalfiwhjytdr
xregqmyonskyzupaffiwhmctdb
xrsgqmyonszvzupmlfiwhjctdb
xzegqmyonskvnupalfiwfjctdb
qregqmyonskvzupalfiwhrctjb
xpegqmyonsivzupqlfiwhjctdb
xregqmyoyskrzupalfiwhjctdx
xregqmyonsqvzupalfiwhjdndb
xregjmyonskvzppalfiwhjcgdb
xregqmyziskvzupalfiwhjctib
xregqmyonmkvbupalfiwhjckdb
xtegamyonskvzupalniwhjctdb
xregqpyonskvzhpwlfiwhjctdb
xvegqmfonskvzupalfiwhjcadb
xregqmyonskvzupaysiwhjctxb
xrejqmyonudvzupalfiwhjctdb
llegqmyonskvzbpalfiwhjctdb
tcegqmbonskvzupalfiwhjctdb
lregqmyohskvzupalfiwhjcttb
xrngqmcfnskvzupalfiwhjctdb
xregqmyonspvzuuplfiwhjctdb
xrxgqmyonslvzupalfiwhjctdo
xregqmyonskvzulalfuwhjdtdb
xregqmnonskvzupalfvwhjckdb
xregqbyfnskvzupaltiwhjctdb
xregqmyodsovzwpalfiwhjctdb
xregomyonskvhrpalfiwhjctdb
xregqmfdnskvzupalliwhjctdb
xregqmyonskvzupaabithjctdb
xrngamyonskvzupalfiwhjcttb
xrhgqmyonskvzupaldifhjctdb
xrygzmyonskvzupatfiwhjctdb
xregqmyonskvzupiqtiwhjctdb
xregqmyonfkvzupalfiwxjcsdb
xregqsyunskvzupalfiwhjctde
xrzgqmyolskvzupasfiwhjctdb
xgegqmyoyskvzupalfiwfjctdb
xrvgqlyohskvzupalfiwhjctdb
xregcmyonskvzuprlyiwhjctdb
xregqmyonskvwjpalfiwsjctdb
xrfgqmyonskvzupalfidhactdb
xcegqmyonwkvzdpalfiwhjctdb
nregqmyrnskvzupalciwhjctdb
xcegqmyonskvzvpalfiwhjctdj
xregqmyonskvzupqssiwhjctdb
xregcmyonskvzupalfinhjutdb
xregqmyonskvzupzlfiwcjctnb
xnegqmyozskvzbpalfiwhjctdb
xregvmponskvzupalfiwhsctdb
xregqmyonskvpupalqichjctdb
xreqqmyonskvzupauuiwhjctdb
xregqryonskvzupatfiwhjctyb
hregqmyonokvzupalfiwhmctdb
xreuqmionckvzupalfiwhjctdb
xregqmyoiskvzupanfiwhjntdb
xrdgqmronskvzupaluiwhjctdb
xadgqmyunskvzupalfiwhjctdb
eregqmzonskvzupakfiwhjctdb
xiegqmyonskvnupblfiwhjctdb
yregqmzonskvzupalfiwhjotdb
xregqmyonskvjupalfiwhjhtvb
wregqmyonskvzzprlfiwhjctdb
xregqmyovskvzupalgiuhjctdb
xregqmyonskjzupelfuwhjctdb
xregqmysuskvpupalfiwhjctdb
xrebqkyonskvzupalfiwpjctdb
xregcmyonskvzipalfiwhjcttb
xregqmyonskdyupalfiwgjctdb
xregcmyonskvzupalfiwijctnb
xregqmyonsovdupalfrwhjctdb
xregqmaonskvzupalnkwhjctdb
xregqmysnfkvzupalfiwhictdb
xregqmyonswvzupalfiyhjctdf
xreoqmyrnskvzupalfihhjctdb
tregqmydnskvzupalfizhjctdb
xregxmyonykvzupalfnwhjctdb
xzegqnyonskuzupalfiwhjctdb
xregqmfonszvvupalfiwhjctdb
xrerqmyjnskvzupalfiwhpctdb
xregqmyanskvzupalffphjctdb
rregqmyogskvzupalfiehjctdb
xrpgqmyonspvzupalfiwgjctdb
xuegqmppnskvzupalfiwhjctdb
xregqmyonskvzqpalsiwhjhtdb
xregqzyonskvzkpalfiwujctdb
xrdgqmyonskvzupglfiwhjctdu
xregqmyonskqzupahciwhjctdb
treqqmyonskvzupalfiwhjcqdb
vlegqmyonskvzupalfiwhjwtdb
xregjmyonskviupglfiwhjctdb
xreggmyanskvzupalfiwhjcydb
xregqmybnskvzuprlfiwhjmtdb
xrsgqmyonskizupagfiwhjctdb
xregqmyenskvzupalfvwhjctib
lrygqmyonsrvzupalfiwhjctdb
xregqmjonskvqupalfiwhjctdu
xregqmyonsknzmpzlfiwhjctdb
xregqmyonhkvzupllfiwhjctdz
xregqmronskvdumalfiwhjctdb
xrpgqmyonskvzupalfhwhjhtdb
xfegqmeonskvzupasfiwhjctdb
xregqqyonskvzrpalfiwijctdb
xretqmmonskvzupalfiwhjcfdb
xregqmyonskvznpalniwhjztdb
xregqmyqnskvzuoalfiwhhctdb
xregqmyonsbvzupalviwhjxtdb
xregqmyonskvzupazmiwhhctdb
xregqmyosskvzupalflwhjctdw
xtegqmyonskvzupamciwhjctdb
xregamyonskvzbpalfiwhqctdb
xregqmgonskvzupalfiwhictxb
xregqmyonskvjupvlfnwhjctdb
xrthqmyonskvzupalfiwhjctub
xrexqmyoyskvzupalfiwhjcadb
xvegqmyonskvxupalfiwhjztdb
xregqmyonskgzupalhiwhjptdb
xregqmysnskvzufalpiwhjctdb
xregqmyonskvbipalfighjctdb
xregqmyonskvzupylfiwhjwvdb
gregqmyonskvzupalfikhjctdt
ujegqmyonskvzupalfiwhjctlb
nreqqmyonskjzupalfiwhjctdb
xregqmyonskvzupanfbwhjchdb
xregqyyoeskwzupalfiwhjctdb
xregqmyokskvzgpalfiwhnctdb
lregqmyonskvzupalfawsjctdb
xtegqmyonskvzmpalfiwhjctmb
xtegqvyonskvzupalfiwhjdtdb
xpegqpyonekvzupalfiwhjctdb
qregqmyonskvzupalfiwmjctdn
xregqnyosskvzupalfibhjctdb
xregqmyonsknzupalflwhjctfb
xregqmxoyskvzuealfiwhjctdb
xregdmyoeskvzupalfiwhfctdb
xremmmyonskvzupalfiwhxctdb
xregqmconskvzupylfuwhjctdb
xregqmyonskvzupawiiwhictdb
xlegsmyonskvzupalfiwhbctdb
xregqmyonsavzopalyiwhjctdb
xregqmyonskczupalfibhvctdb
xregqmyonskvzvpalfiunjctdb
xregqmyonskvdupalfiwhjczdp
xregqmyonskvzupklfswhhctdb
xrelqmyonskvzupalyiwhjctdi
xrcgqmyonskvzupalfieqjctdb
xregqmnonskvzupacfewhjctdb
xrwgqmyonskvzuealfiwhcctdb
xregqiyonsevzmpalfiwhjctdb
xregqmyonjyvzupalfiwhjckdb
xregqmyonyklzupadfiwhjctdb
xregqmyanskvzupolfiwhjctpb
xdbgqmyonskvzupslfiwhjctdb
xregqmhonykvzupalfawhjctdb
xregqmqonsivzupalfifhjctdb
xregqgyonsrvzupalfiwhjctib
xregqmyofskvzupalfiwlfctdb
xregqmyovskvzupllftwhjctdb
xregqmyonskvzupaciiwhuctdb
xregqmyonsdvzuhalfiwhjhtdb
xreiqmyonskvzupalfiwhncldb
xregqmyongkvzugalfiwhjctxb
xregqsyonskvzrpmlfiwhjctdb
xrogqmyonskvzxpalfiwhbctdb
xregqmkonskvzuqalfiwhjptdb
xregqmyonskvvxpalfiwhactdb
xregqmyonskvzupsliiwhwctdb

84
2018-go/day_02/main.go Normal file
View File

@@ -0,0 +1,84 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
func checkDuplicates(s string) (bool, bool) {
two := false
three := false
charsInString := map[string]int{}
chars := strings.Split(s, "")
for _, c := range chars {
charsInString[c]++
}
for _, c := range charsInString {
if c == 2 {
two = true
} else if c == 3 {
three = true
}
}
return two, three
}
func compareStrings(s1, s2 string) (string, bool) {
diffCount := 0
diffString := ""
for i := 0; i < len(s1) && diffCount < 2; i++ {
if s1[i] == s2[i] {
diffString += string(s1[i])
} else {
diffCount++
}
}
if diffCount >= 2 {
return "", false
}
return diffString, true
}
func main() {
twos := 0
threes := 0
var stringStore []string
//We open the input file
file, err := os.Open("./input")
if err != nil {
//If there is an error we print it in the stderr and exit(1)
log.Fatal(err)
}
//We defer the closure of the file
defer file.Close()
//We create a scanner to the file
scan := bufio.NewScanner(file)
//We iterate throught the file line
for scan.Scan() {
currentString := scan.Text()
stringStore = append(stringStore, currentString)
two, three := checkDuplicates(currentString)
if two {
twos++
}
if three {
threes++
}
}
fmt.Printf("Part One: the checksum is %v\n", twos*threes)
for i, str := range stringStore {
for j := i + 1; j < len(stringStore); j++ {
ret, valid := compareStrings(str, stringStore[j])
if valid {
fmt.Printf("Part Two: the common letters are %v\n", ret)
os.Exit(0)
}
}
}
}

1411
2018-go/day_03/input Normal file

File diff suppressed because it is too large Load Diff

71
2018-go/day_03/main.go Normal file
View File

@@ -0,0 +1,71 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type claim struct {
x int
y int
len int
height int
overlapping bool
}
func main() {
var fabric [1000][1000]int
claims := make(map[int]*claim)
inchesCount := 0
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
for scan.Scan() {
line := scan.Text()
line = strings.Join(strings.Fields(line), "")
info := strings.Split(line, "#")[1]
id, _ := strconv.Atoi(strings.Split(info, "@")[0])
measures := strings.Split(info, "@")[1]
xy := strings.Split(strings.Split(measures, ":")[0], ",")
dim := strings.Split(strings.Split(measures, ":")[1], "x")
x, _ := strconv.Atoi(xy[0])
y, _ := strconv.Atoi(xy[1])
len, _ := strconv.Atoi(dim[0])
height, _ := strconv.Atoi(dim[1])
newClaim := claim{x, y, len, height, false}
for i := x; i < x+len; i++ {
for j := y; j < y+height; j++ {
if fabric[i][j] == 0 {
fabric[i][j] = id
} else if fabric[i][j] != -1 {
claims[fabric[i][j]].overlapping = true
fabric[i][j] = -1
inchesCount++
}
if fabric[i][j] == -1 {
newClaim.overlapping = true
}
}
}
claims[id] = &newClaim
}
fmt.Printf("Part One: square inches of fabric within two or more claims %v\n", inchesCount)
for id, c := range claims {
if !c.overlapping {
fmt.Printf("Part Two: the id of the claim that doesn't overlap is %v\n", id)
}
}
}

1134
2018-go/day_04/input Normal file

File diff suppressed because it is too large Load Diff

115
2018-go/day_04/main.go Normal file
View File

@@ -0,0 +1,115 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
)
type guard struct {
timeCard [60]int
}
func printSchedule(guardsSchedule map[int]guard) {
fmt.Print("ID\t")
for i := 0; i < 60; i++ {
fmt.Printf("%v ", i)
}
fmt.Print("\tSUM\n")
for i, g := range guardsSchedule {
sum := 0
fmt.Printf("%v\t", i)
for i, t := range g.timeCard {
sum += t
if i < 59 && i > 8 && g.timeCard[i+1] < 10 {
fmt.Printf("%v ", t)
} else {
fmt.Printf("%v ", t)
}
}
fmt.Printf("\t%v\n", sum)
}
}
func main() {
var lines []string
guardsSchedule := make(map[int]guard)
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
for scan.Scan() {
lines = append(lines, scan.Text())
}
sort.Strings(lines)
currentGuardID := -1
sleepTime := 0
wakeTime := 0
for _, line := range lines {
if strings.Contains(line, "Guard") {
currentGuardID, _ = strconv.Atoi(strings.Split(strings.Split(line, "#")[1], " ")[0])
_, exists := guardsSchedule[currentGuardID]
if !exists {
guardsSchedule[currentGuardID] = *new(guard)
}
} else if strings.Contains(line, "falls asleep") {
st, _ := strconv.Atoi(strings.Split(strings.Split(strings.Split(line, "]")[0], " ")[1], ":")[1])
sleepTime = st
} else if strings.Contains(line, "wakes up") {
wt, _ := strconv.Atoi(strings.Split(strings.Split(strings.Split(line, "]")[0], " ")[1], ":")[1])
wakeTime = wt
timeSchedule := guardsSchedule[currentGuardID]
for j := sleepTime; j < wakeTime; j++ {
timeSchedule.timeCard[j]++
}
guardsSchedule[currentGuardID] = timeSchedule
}
}
mostAsleepTime := -1
mostAsleepID := -1
for i, g := range guardsSchedule {
sum := 0
for _, t := range g.timeCard {
sum += t
}
if sum > mostAsleepTime {
mostAsleepTime = sum
mostAsleepID = i
}
}
mostAsleepMinute := -1
max := -1
for i, t := range guardsSchedule[mostAsleepID].timeCard {
if t > max {
max = t
mostAsleepMinute = i
}
}
fmt.Printf("Part One: %v\n", mostAsleepMinute*mostAsleepID)
mostAsleepMinute = -1
max = -1
mostAsleepID = -1
for i, g := range guardsSchedule {
for j, t := range g.timeCard {
if t > max {
max = t
mostAsleepMinute = j
mostAsleepID = i
}
}
}
fmt.Printf("Part Two: %v\n", mostAsleepMinute*mostAsleepID)
}

1
2018-go/day_05/input Normal file

File diff suppressed because one or more lines are too long

56
2018-go/day_05/main.go Normal file
View File

@@ -0,0 +1,56 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
var alphabet string
func react(s string) string {
firstS := s
for i := 0; i < len(alphabet); i++ {
letter := alphabet[i : i+1]
s = strings.Replace(s, letter+strings.ToUpper(letter), "", -1)
s = strings.Replace(s, strings.ToUpper(letter)+letter, "", -1)
}
if firstS == s {
return s
} else {
return react(s)
}
}
func main() {
var input string
alphabet = "abcdefghijklmnopqrstuvwxyz"
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
for scan.Scan() {
input = scan.Text()
}
fmt.Printf("Part One: %v\n", len(react(input)))
min := len(input)
for i := 0; i < len(alphabet); i++ {
letter := alphabet[i : i+1]
try := input
try = strings.Replace(try, letter, "", -1)
try = strings.Replace(try, strings.ToUpper(letter), "", -1)
size := len(react(try))
if size < min {
min = size
}
}
fmt.Printf("Part Two: %v\n", min)
}

50
2018-go/day_06/input Normal file
View File

@@ -0,0 +1,50 @@
81, 46
330, 289
171, 261
248, 97
142, 265
139, 293
309, 208
315, 92
72, 206
59, 288
95, 314
126, 215
240, 177
78, 64
162, 168
75, 81
271, 258
317, 223
210, 43
47, 150
352, 116
316, 256
269, 47
227, 343
125, 290
245, 310
355, 301
251, 282
353, 107
254, 298
212, 128
60, 168
318, 254
310, 303
176, 345
110, 109
217, 338
344, 330
231, 349
259, 208
201, 57
200, 327
354, 111
166, 214
232, 85
96, 316
151, 288
217, 339
62, 221
307, 68

133
2018-go/day_06/main.go Normal file
View File

@@ -0,0 +1,133 @@
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
)
type point struct {
x int
y int
id int
}
type grid struct {
points [][]int
coordinates []point
currentID int
invalidIDs map[int]bool
maxX int
maxY int
minX int
minY int
maxArea int
}
func (p1 point) manhattanDistance(p2 point) int {
return int(math.Abs(float64(p1.x-p2.x)) + math.Abs(float64(p1.y-p2.y)))
}
func main() {
var currentGrid grid
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
for scan.Scan() {
var p point
line := scan.Text()
fmt.Sscanf(line, "%d, %d", &p.x, &p.y)
p.id = currentGrid.currentID
currentGrid.currentID++
currentGrid.coordinates = append(currentGrid.coordinates, p)
if p.x > currentGrid.maxX {
currentGrid.maxX = p.x
}
if p.y > currentGrid.maxY {
currentGrid.maxY = p.y
}
if p.x < currentGrid.minX || currentGrid.minX == 0 {
currentGrid.minX = p.x
}
if p.y < currentGrid.minY || currentGrid.minY == 0 {
currentGrid.minY = p.y
}
}
currentGrid.points = make([][]int, currentGrid.maxX)
for i := range currentGrid.points {
currentGrid.points[i] = make([]int, currentGrid.maxY)
}
for x := 0; x < currentGrid.maxX; x++ {
for y := 0; y < currentGrid.maxY; y++ {
minDist := -1
minDistID := -1
for i, c := range currentGrid.coordinates {
dist := c.manhattanDistance(point{x: x, y: y})
if i == 0 {
minDist = dist
minDistID = c.id
} else if dist < minDist {
minDist = dist
minDistID = c.id
} else if dist == minDist {
minDistID = -1
}
}
currentGrid.points[x][y] = minDistID
}
}
currentGrid.invalidIDs = make(map[int]bool)
for x := 0; x < currentGrid.maxX; x++ {
currentGrid.invalidIDs[currentGrid.points[x][0]] = true
currentGrid.invalidIDs[currentGrid.points[x][currentGrid.maxY-1]] = true
}
for y := 0; y < currentGrid.maxY; y++ {
currentGrid.invalidIDs[currentGrid.points[0][y]] = true
currentGrid.invalidIDs[currentGrid.points[currentGrid.maxX-1][y]] = true
}
for id := 0; id < currentGrid.currentID; id++ {
count := 0
for x := 0; x < currentGrid.maxX; x++ {
for y := 0; y < currentGrid.maxY; y++ {
if currentGrid.points[x][y] == id {
count++
}
}
}
if count > currentGrid.maxArea {
_, invalidID := currentGrid.invalidIDs[id]
if !invalidID {
currentGrid.maxArea = count
}
}
}
fmt.Printf("Part One: %v\n", currentGrid.maxArea)
count := 0
for x := 0; x < currentGrid.maxX; x++ {
for y := 0; y < currentGrid.maxY; y++ {
sum := 0
for _, c := range currentGrid.coordinates {
sum += c.manhattanDistance(point{x: x, y: y})
}
if sum < 10000 {
count++
}
}
}
fmt.Printf("Part Two: %v\n", count)
}

101
2018-go/day_07/input Normal file
View File

@@ -0,0 +1,101 @@
Step Q must be finished before step I can begin.
Step B must be finished before step M can begin.
Step R must be finished before step F can begin.
Step G must be finished before step S can begin.
Step M must be finished before step A can begin.
Step Z must be finished before step W can begin.
Step J must be finished before step C can begin.
Step K must be finished before step O can begin.
Step C must be finished before step I can begin.
Step Y must be finished before step L can begin.
Step N must be finished before step P can begin.
Step S must be finished before step X can begin.
Step E must be finished before step U can begin.
Step U must be finished before step V can begin.
Step D must be finished before step F can begin.
Step W must be finished before step H can begin.
Step T must be finished before step I can begin.
Step H must be finished before step V can begin.
Step L must be finished before step O can begin.
Step P must be finished before step A can begin.
Step A must be finished before step I can begin.
Step F must be finished before step O can begin.
Step V must be finished before step X can begin.
Step I must be finished before step O can begin.
Step X must be finished before step O can begin.
Step F must be finished before step V can begin.
Step L must be finished before step P can begin.
Step Y must be finished before step P can begin.
Step Y must be finished before step X can begin.
Step Y must be finished before step O can begin.
Step D must be finished before step A can begin.
Step T must be finished before step F can begin.
Step W must be finished before step X can begin.
Step R must be finished before step A can begin.
Step E must be finished before step F can begin.
Step H must be finished before step I can begin.
Step K must be finished before step Y can begin.
Step W must be finished before step P can begin.
Step V must be finished before step O can begin.
Step N must be finished before step E can begin.
Step L must be finished before step I can begin.
Step B must be finished before step G can begin.
Step D must be finished before step T can begin.
Step J must be finished before step L can begin.
Step M must be finished before step Y can begin.
Step T must be finished before step A can begin.
Step K must be finished before step D can begin.
Step H must be finished before step P can begin.
Step P must be finished before step I can begin.
Step T must be finished before step L can begin.
Step J must be finished before step N can begin.
Step U must be finished before step F can begin.
Step U must be finished before step I can begin.
Step A must be finished before step F can begin.
Step U must be finished before step P can begin.
Step R must be finished before step H can begin.
Step G must be finished before step V can begin.
Step P must be finished before step F can begin.
Step B must be finished before step D can begin.
Step U must be finished before step X can begin.
Step K must be finished before step A can begin.
Step G must be finished before step D can begin.
Step N must be finished before step U can begin.
Step U must be finished before step L can begin.
Step M must be finished before step J can begin.
Step I must be finished before step X can begin.
Step H must be finished before step L can begin.
Step M must be finished before step S can begin.
Step E must be finished before step O can begin.
Step Q must be finished before step F can begin.
Step A must be finished before step O can begin.
Step T must be finished before step P can begin.
Step F must be finished before step X can begin.
Step D must be finished before step P can begin.
Step A must be finished before step X can begin.
Step G must be finished before step Z can begin.
Step W must be finished before step F can begin.
Step Q must be finished before step X can begin.
Step C must be finished before step V can begin.
Step L must be finished before step V can begin.
Step E must be finished before step L can begin.
Step B must be finished before step X can begin.
Step M must be finished before step V can begin.
Step F must be finished before step I can begin.
Step P must be finished before step X can begin.
Step C must be finished before step A can begin.
Step Z must be finished before step H can begin.
Step Q must be finished before step S can begin.
Step G must be finished before step X can begin.
Step T must be finished before step O can begin.
Step P must be finished before step O can begin.
Step T must be finished before step V can begin.
Step N must be finished before step V can begin.
Step Z must be finished before step X can begin.
Step L must be finished before step X can begin.
Step Z must be finished before step Y can begin.
Step N must be finished before step T can begin.
Step S must be finished before step T can begin.
Step G must be finished before step K can begin.
Step T must be finished before step X can begin.
Step R must be finished before step X can begin.

View File

@@ -0,0 +1,7 @@
Step C must be finished before step A can begin.
Step C must be finished before step F can begin.
Step A must be finished before step B can begin.
Step A must be finished before step D can begin.
Step B must be finished before step E can begin.
Step D must be finished before step E can begin.
Step F must be finished before step E can begin.

176
2018-go/day_07/main.go Normal file
View File

@@ -0,0 +1,176 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strings"
)
type step struct {
ID string
time int
next []*step
prev []*step
}
func alreadyExecuted(executed []string, steps []*step) bool {
for _, s := range steps {
found := false
for _, e := range executed {
if e == s.ID {
found = true
}
}
if !found {
return false
}
}
return true
}
func alreadyInQueue(queue []string, s *step) bool {
for _, q := range queue {
if s.ID == q {
return true
}
}
return false
}
func main() {
var queue []string
var executionOrder []string
stepAddr := make(map[string]*step)
file, err := os.Open("./input_less")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
for scan.Scan() {
line := scan.Text()
splittedLine := strings.Split(line, " ")
currentID := splittedLine[1]
nextID := splittedLine[7]
currentStep, csExists := stepAddr[currentID]
nextStep, nsExists := stepAddr[nextID]
if !csExists {
cs := step{ID: currentID, time: int(currentID[0]) - 64}
stepAddr[currentID] = &cs
currentStep = &cs
}
if !nsExists {
ns := step{ID: nextID, time: int(nextID[0]) - 64}
stepAddr[nextID] = &ns
nextStep = &ns
}
currentStep.next = append(currentStep.next, nextStep)
nextStep.prev = append(nextStep.prev, currentStep)
}
for _, s := range stepAddr {
if len(s.prev) == 0 {
queue = append(queue, s.ID)
}
}
sort.Strings(queue)
i := 0
for i < len(queue) {
sPrev := stepAddr[queue[i]].prev
if len(sPrev) == 0 || alreadyExecuted(executionOrder, sPrev) {
executionOrder = append(executionOrder, queue[i])
for _, n := range stepAddr[queue[i]].next {
if !alreadyInQueue(queue, n) {
queue = append(queue, n.ID)
}
}
queue = append(queue[:i], queue[i+1:]...)
sort.Strings(queue)
i = 0
} else {
i++
}
}
fmt.Printf("Part One: %v\n", strings.Join(executionOrder, ""))
for _, s := range stepAddr {
if len(s.prev) == 0 {
queue = append(queue, s.ID)
fmt.Printf("Added in queue: %v\n", s.ID)
}
}
sort.Strings(queue)
currentTime := 0
var times [2]int
var workers [2]string
var availableSteps []*step
working := false
executionOrder = make([]string, 0)
for len(queue) > 0 || len(availableSteps) > 0 || working {
if len(queue) > 0 {
for i := 0; i < len(queue); i++ {
fmt.Printf("Analyzing from queue step %v\t i:%v\n", queue[i], i)
sPrev := stepAddr[queue[i]].prev
if len(sPrev) == 0 || alreadyExecuted(executionOrder, sPrev) {
availableSteps = append(availableSteps, stepAddr[queue[i]])
for _, n := range stepAddr[queue[i]].next {
if !alreadyInQueue(queue, n) {
queue = append(queue, n.ID)
fmt.Printf("Added in queue: %v\n", n.ID)
}
}
fmt.Printf("Added to availableSteps: %v\n", queue[i])
fmt.Printf("Removed from queue: %v\n", queue[i])
queue = append(queue[:i], queue[i+1:]...)
i = -1
}
}
}
sort.Slice(availableSteps, func(i, j int) bool {
return availableSteps[i].ID < availableSteps[j].ID
})
if len(availableSteps) > 0 || working {
for w := 0; w < len(workers); w++ {
if workers[w] == "" && len(availableSteps) > 0 {
workers[w] = availableSteps[0].ID
times[w] = availableSteps[0].time
working = true
fmt.Printf("Assigned to worker %v step %v\n", w, availableSteps[0].ID)
fmt.Printf("Removed from availableSteps: %v\n", availableSteps[0].ID)
availableSteps = availableSteps[1:]
} else if workers[w] != "" {
times[w]--
fmt.Printf("Decreased time of worker %v to %v\n", w, times[w])
if times[w] == 0 {
executionOrder = append(executionOrder, workers[w])
fmt.Printf("Worker %v has completed step %v\n", w, workers[w])
workers[w] = ""
times[w] = 0
//if times[0]+times[1]+times[2]+times[3]+times[4] == 0 {
if times[0]+times[1] == 0 {
working = false
}
}
}
}
if working {
currentTime++
fmt.Printf("Increaset currentTime to %v\n", currentTime)
}
}
}
fmt.Printf("Part Two: %v\t%v\n", currentTime, executionOrder)
}

170
2018-go/day_07/prova.go Normal file
View File

@@ -0,0 +1,170 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
)
type graph map[string][]string
type task struct {
time int
task string
}
func popMinimumTask(tasks []task) (int, string, []task) {
var minTime = 2147483647
var minTasks = make([]task, 0)
for _, t := range tasks {
if t.time < minTime {
minTasks = []task{t}
minTime = t.time
} else if t.time == minTime {
minTasks = append(minTasks, t)
}
}
var minTask task
var minID = string("Z")
for _, t := range minTasks {
if t.task <= minID {
minID = t.task
minTask = t
}
}
var removed = make([]task, 0)
for _, val := range tasks {
if !(val.task == minTask.task && val.time == minTask.time) {
removed = append(removed, val)
}
}
return minTask.time, minTask.task, removed
}
func popMinimumString(strs []string) (string, []string) {
if len(strs) == 0 {
return "", nil
}
min := strs[0]
for _, str := range strs {
if str < min {
min = str
}
}
var removed = make([]string, 0)
for _, str := range strs {
if str != min {
removed = append(removed, str)
}
}
return min, removed
}
func main() {
var gr = make(graph)
var incoming = make(map[string]int)
var incomingSaved = make(map[string]int) // for Part 2
file, err := os.Open("input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
var start, end string
fmt.Sscanf(line, "Step %s must be finished before step %s can begin.", &start, &end)
if _, ok := gr[start]; ok {
gr[start] = append(gr[start], end)
} else {
gr[start] = []string{end}
}
if _, ok := incoming[end]; ok {
incoming[end]++
incomingSaved[end]++
} else {
incoming[end] = 1
incomingSaved[end] = 1
}
}
var queue, taskQueue = make([]string, 0), make([]string, 0)
for key, value := range gr {
sort.Strings(value)
gr[key] = value
if incoming[key] == 0 {
queue = append(queue, key)
}
}
taskQueue = append(taskQueue, queue...)
var result string
for len(queue) != 0 {
sort.Strings(queue)
init := queue[0]
var temp []string
for _, v := range queue {
if v != init {
temp = append(temp, v)
}
}
queue = temp
result += init
for _, key := range gr[init] {
incoming[key]--
if incoming[key] == 0 {
queue = append(queue, key)
}
}
}
fmt.Printf("Part 1: %s\n", result)
// Part 2
var currentTime int
var currentTask string
var tasks = make([]task, 0)
for len(tasks) < 5 && len(taskQueue) > 0 {
var next string
if nxt, q := popMinimumString(taskQueue); q != nil {
next = nxt
taskQueue = q
tasks = append(tasks, task{time: currentTime + int([]byte(next)[0]) - 4, task: next})
}
}
for len(tasks) > 0 || len(taskQueue) > 0 {
currentTime, currentTask, tasks = popMinimumTask(tasks)
for _, key := range gr[currentTask] {
incomingSaved[key]--
if incomingSaved[key] == 0 {
taskQueue = append(taskQueue, key)
}
}
for len(tasks) < 5 && len(taskQueue) > 0 {
var next string
if nxt, q := popMinimumString(taskQueue); q != nil {
next = nxt
taskQueue = q
tasks = append(tasks, task{time: currentTime + int([]byte(next)[0]) - 4, task: next})
}
}
}
fmt.Printf("Part 2: %v\n", currentTime)
}

1
2018-go/day_08/input Normal file

File diff suppressed because one or more lines are too long

81
2018-go/day_08/main.go Normal file
View File

@@ -0,0 +1,81 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type node struct {
children []*node
metadata []int
}
func treeRecursiveBuild(info *[]int) (*node, int) {
n := &node{}
metaSum := 0
childNum := (*info)[0]
metaNum := (*info)[1]
*info = (*info)[2:]
for i := 0; i < childNum; i++ {
children, s := treeRecursiveBuild(info)
metaSum += s
n.children = append(n.children, children)
}
for i := 0; i < metaNum; i++ {
metaSum += (*info)[0]
n.metadata = append(n.metadata, (*info)[0])
if len(*info) > 0 {
*info = (*info)[1:]
}
}
return n, metaSum
}
func (n *node) value() int {
value := 0
if len(n.children) == 0 {
for _, m := range n.metadata {
value += m
}
} else {
for _, m := range n.metadata {
if m <= len(n.children) {
value += n.children[m-1].value()
}
}
}
return value
}
func main() {
var info []int
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
scan.Scan()
line := scan.Text()
for _, n := range strings.Split(line, " ") {
num, _ := strconv.Atoi(n)
info = append(info, num)
}
root, metaSum := treeRecursiveBuild(&info)
fmt.Printf("Part One: %v\n", metaSum)
fmt.Printf("Part Two: %v\n", root.value())
}

1
2018-go/day_09/input Normal file
View File

@@ -0,0 +1 @@
405 players; last marble is worth 70953 points

93
2018-go/day_09/main.go Normal file
View File

@@ -0,0 +1,93 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
)
type marble struct {
left *marble
right *marble
value uint64
}
func (m *marble) placeMarble(newMarbleValue uint64) *marble {
var newMarble = marble{left: m.right, right: m.right.right, value: newMarbleValue}
m.right.right.left = &newMarble
m.right.right = &newMarble
return &newMarble
}
func (m *marble) removeCurrentMarble() (*marble, uint64) {
val := m.value
m.left.right = m.right
m.right.left = m.left
return m.right, val
}
func (m *marble) changeCurrentMarble(clockwiseMove int) *marble {
var newCurrentMarble = m
if clockwiseMove > 0 {
for i := 0; i < clockwiseMove; i++ {
newCurrentMarble = newCurrentMarble.right
}
} else {
for i := 0; i > clockwiseMove; i-- {
newCurrentMarble = newCurrentMarble.left
}
}
return newCurrentMarble
}
func main() {
var elfs, currentElf int
var currentMarbleValue, lastMarbleValue, maxPoint uint64
var currentMarble = &marble{left: nil, right: nil, value: 0}
elfsPoint := make(map[int]uint64)
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
scan.Scan()
line := scan.Text()
fmt.Sscanf(line, "%d players; last marble is worth %d points", &elfs, &lastMarbleValue)
//Part Two: uncomment next line
//lastMarbleValue *= 100
for i := 0; i < elfs; i++ {
elfsPoint[i] = 0
}
currentMarble.left = currentMarble
currentMarble.right = currentMarble
for currentMarbleValue < lastMarbleValue {
currentMarbleValue++
if currentMarbleValue%23 == 0 {
var val uint64
currentMarble = currentMarble.changeCurrentMarble(-7)
currentMarble, val = currentMarble.removeCurrentMarble()
elfsPoint[currentElf] += (currentMarbleValue + val)
if elfsPoint[currentElf] > maxPoint {
maxPoint = elfsPoint[currentElf]
}
} else {
currentMarble = currentMarble.placeMarble(currentMarbleValue)
}
currentElf = (currentElf + 1) % elfs
}
fmt.Printf("Part One/Two: %v\n", maxPoint)
}

367
2018-go/day_10/input Normal file
View File

@@ -0,0 +1,367 @@
position=< 52484, -20780> velocity=<-5, 2>
position=<-52068, 31483> velocity=< 5, -3>
position=< 21120, 52398> velocity=<-2, -5>
position=<-10264, -31236> velocity=< 1, 3>
position=< 52501, 52392> velocity=<-5, -5>
position=<-31144, 52394> velocity=< 3, -5>
position=<-41603, 21031> velocity=< 4, -2>
position=<-41630, -31239> velocity=< 4, 3>
position=< 21126, -20780> velocity=<-2, 2>
position=<-31150, -10326> velocity=< 3, 1>
position=<-41598, 21033> velocity=< 4, -2>
position=<-10252, -20787> velocity=< 1, 2>
position=<-20677, -52149> velocity=< 2, 5>
position=< 21102, -31236> velocity=<-2, 3>
position=<-20734, 41942> velocity=< 2, -4>
position=< 42018, -41696> velocity=<-4, 4>
position=<-52071, -41691> velocity=< 5, 4>
position=<-20720, -41693> velocity=< 2, 4>
position=<-20710, 10574> velocity=< 2, -1>
position=< 31540, 10578> velocity=<-3, -1>
position=< 31581, -41693> velocity=<-3, 4>
position=< 31540, 10576> velocity=<-3, -1>
position=<-31184, 31490> velocity=< 3, -3>
position=<-31168, -41689> velocity=< 3, 4>
position=<-41641, 10575> velocity=< 4, -1>
position=<-41590, 41940> velocity=< 4, -4>
position=<-31179, -41695> velocity=< 3, 4>
position=<-10232, 31481> velocity=< 1, -3>
position=< 42012, 31481> velocity=<-4, -3>
position=<-31192, 31486> velocity=< 3, -3>
position=< 10685, 21029> velocity=<-1, -2>
position=< 31535, -41692> velocity=<-3, 4>
position=< 31561, 41942> velocity=<-3, -4>
position=<-20719, 31481> velocity=< 2, -3>
position=< 42007, 10581> velocity=<-4, -1>
position=<-20726, 21027> velocity=< 2, -2>
position=<-31136, 21034> velocity=< 3, -2>
position=< 10680, -31241> velocity=<-1, 3>
position=< 31559, 21027> velocity=<-3, -2>
position=< 21088, -20780> velocity=<-2, 2>
position=< 52472, 41939> velocity=<-5, -4>
position=< 21126, -41692> velocity=<-2, 4>
position=<-10268, 41937> velocity=< 1, -4>
position=< 31551, 52393> velocity=<-3, -5>
position=<-52097, 10578> velocity=< 5, -1>
position=<-52087, -52145> velocity=< 5, 5>
position=<-10223, 21034> velocity=< 1, -2>
position=< 52483, -20785> velocity=<-5, 2>
position=< 10660, -20780> velocity=<-1, 2>
position=< 10648, -41694> velocity=<-1, 4>
position=< 41988, -31239> velocity=<-4, 3>
position=< 42010, -52149> velocity=<-4, 5>
position=< 52440, -10327> velocity=<-5, 1>
position=< 41991, 41943> velocity=<-4, -4>
position=< 10636, -52142> velocity=<-1, 5>
position=<-52071, -52146> velocity=< 5, 5>
position=< 10672, -31237> velocity=<-1, 3>
position=<-52095, 31484> velocity=< 5, -3>
position=< 31568, -41688> velocity=<-3, 4>
position=<-10236, -10331> velocity=< 1, 1>
position=<-52095, 21031> velocity=< 5, -2>
position=<-31187, 21027> velocity=< 3, -2>
position=< 31577, 21036> velocity=<-3, -2>
position=<-10284, -20784> velocity=< 1, 2>
position=<-10271, -20783> velocity=< 1, 2>
position=< 10650, -10335> velocity=<-1, 1>
position=<-20706, -41694> velocity=< 2, 4>
position=< 10636, 31490> velocity=<-1, -3>
position=<-41590, 21028> velocity=< 4, -2>
position=< 52480, -52142> velocity=<-5, 5>
position=<-52050, -31239> velocity=< 5, 3>
position=<-31183, -10326> velocity=< 3, 1>
position=< 42038, 52393> velocity=<-4, -5>
position=<-31176, 31486> velocity=< 3, -3>
position=<-10276, 10581> velocity=< 1, -1>
position=<-31168, -31236> velocity=< 3, 3>
position=< 41986, -52151> velocity=<-4, 5>
position=< 10656, 52398> velocity=<-1, -5>
position=< 42046, 52393> velocity=<-4, -5>
position=<-31139, 41936> velocity=< 3, -4>
position=< 41991, -20786> velocity=<-4, 2>
position=<-10243, 21033> velocity=< 1, -2>
position=<-41641, 31488> velocity=< 4, -3>
position=<-52095, 21032> velocity=< 5, -2>
position=<-20728, 31490> velocity=< 2, -3>
position=<-31163, -31237> velocity=< 3, 3>
position=< 41999, 52392> velocity=<-4, -5>
position=<-10279, 31485> velocity=< 1, -3>
position=<-10275, 31481> velocity=< 1, -3>
position=< 52484, 21030> velocity=<-5, -2>
position=< 31537, -20785> velocity=<-3, 2>
position=< 52493, -52148> velocity=<-5, 5>
position=< 21129, -52147> velocity=<-2, 5>
position=<-10241, 52398> velocity=< 1, -5>
position=<-10279, 31489> velocity=< 1, -3>
position=< 52496, -10330> velocity=<-5, 1>
position=< 10651, -41692> velocity=<-1, 4>
position=< 52480, -10326> velocity=<-5, 1>
position=<-41596, 41939> velocity=< 4, -4>
position=<-31174, -52151> velocity=< 3, 5>
position=< 42014, -41692> velocity=<-4, 4>
position=< 41986, -10334> velocity=<-4, 1>
position=< 10656, -20781> velocity=<-1, 2>
position=< 31564, 41935> velocity=<-3, -4>
position=<-20717, -20787> velocity=< 2, 2>
position=< 31540, -20789> velocity=<-3, 2>
position=<-10265, -41692> velocity=< 1, 4>
position=<-10260, 41940> velocity=< 1, -4>
position=<-10247, -10326> velocity=< 1, 1>
position=< 21094, -10328> velocity=<-2, 1>
position=<-41646, -52142> velocity=< 4, 5>
position=<-10284, 52395> velocity=< 1, -5>
position=< 21086, 52394> velocity=<-2, -5>
position=< 52500, -31239> velocity=<-5, 3>
position=<-20689, -41697> velocity=< 2, 4>
position=<-41636, 52389> velocity=< 4, -5>
position=< 42026, 41943> velocity=<-4, -4>
position=< 31549, -31239> velocity=<-3, 3>
position=< 52488, 52398> velocity=<-5, -5>
position=< 21086, -52145> velocity=<-2, 5>
position=< 31573, -31237> velocity=<-3, 3>
position=< 21139, 10573> velocity=<-2, -1>
position=<-31188, -20781> velocity=< 3, 2>
position=< 52488, 21035> velocity=<-5, -2>
position=<-10273, 52398> velocity=< 1, -5>
position=<-41642, 10581> velocity=< 4, -1>
position=< 42034, -10328> velocity=<-4, 1>
position=<-31187, 31489> velocity=< 3, -3>
position=< 41994, -10328> velocity=<-4, 1>
position=< 21110, -41692> velocity=<-2, 4>
position=< 41994, -31236> velocity=<-4, 3>
position=< 31532, -41692> velocity=<-3, 4>
position=<-41645, -41696> velocity=< 4, 4>
position=<-41617, -52148> velocity=< 4, 5>
position=< 42027, 21036> velocity=<-4, -2>
position=<-10268, 21029> velocity=< 1, -2>
position=<-41614, -20785> velocity=< 4, 2>
position=< 21097, -31238> velocity=<-2, 3>
position=< 10645, 10575> velocity=<-1, -1>
position=<-10243, 41944> velocity=< 1, -4>
position=< 21118, 21034> velocity=<-2, -2>
position=< 41994, -41691> velocity=<-4, 4>
position=< 21083, -31240> velocity=<-2, 3>
position=<-41596, 52389> velocity=< 4, -5>
position=<-31172, -10328> velocity=< 3, 1>
position=< 52464, 41944> velocity=<-5, -4>
position=<-31176, 52398> velocity=< 3, -5>
position=< 10669, -10326> velocity=<-1, 1>
position=<-31172, -10328> velocity=< 3, 1>
position=< 10653, 41940> velocity=<-1, -4>
position=<-10242, -20784> velocity=< 1, 2>
position=<-10279, -31234> velocity=< 1, 3>
position=< 21134, 10573> velocity=<-2, -1>
position=<-31160, -31239> velocity=< 3, 3>
position=< 21126, 41935> velocity=<-2, -4>
position=<-41617, 31485> velocity=< 4, -3>
position=<-10279, 10574> velocity=< 1, -1>
position=< 10672, 21027> velocity=<-1, -2>
position=< 42022, -20780> velocity=<-4, 2>
position=<-52100, 10573> velocity=< 5, -1>
position=<-52052, -20788> velocity=< 5, 2>
position=< 52461, 31489> velocity=<-5, -3>
position=< 10672, 10577> velocity=<-1, -1>
position=< 21094, -10334> velocity=<-2, 1>
position=<-52074, -31238> velocity=< 5, 3>
position=<-10249, 10582> velocity=< 1, -1>
position=< 52480, -31235> velocity=<-5, 3>
position=< 42042, 10582> velocity=<-4, -1>
position=< 52496, 31481> velocity=<-5, -3>
position=<-31136, 10578> velocity=< 3, -1>
position=<-52095, -41695> velocity=< 5, 4>
position=< 52460, 21027> velocity=<-5, -2>
position=< 21107, -41695> velocity=<-2, 4>
position=<-52051, -52147> velocity=< 5, 5>
position=< 31564, 31487> velocity=<-3, -3>
position=< 52481, -31237> velocity=<-5, 3>
position=<-20722, -31241> velocity=< 2, 3>
position=<-20735, -41692> velocity=< 2, 4>
position=<-52079, 52390> velocity=< 5, -5>
position=< 41994, -52142> velocity=<-4, 5>
position=< 52497, -20785> velocity=<-5, 2>
position=<-41638, 10577> velocity=< 4, -1>
position=<-31175, -20789> velocity=< 3, 2>
position=<-20719, 21031> velocity=< 2, -2>
position=< 52460, 52389> velocity=<-5, -5>
position=<-31136, -31237> velocity=< 3, 3>
position=< 21086, 52397> velocity=<-2, -5>
position=<-52084, 21027> velocity=< 5, -2>
position=< 41994, -41694> velocity=<-4, 4>
position=<-20706, 31486> velocity=< 2, -3>
position=<-20682, -52147> velocity=< 2, 5>
position=< 41999, 41942> velocity=<-4, -4>
position=< 41994, -52151> velocity=<-4, 5>
position=< 41986, 41943> velocity=<-4, -4>
position=<-20738, -20788> velocity=< 2, 2>
position=< 52464, -20780> velocity=<-5, 2>
position=< 31543, 31485> velocity=<-3, -3>
position=< 52448, -10334> velocity=<-5, 1>
position=<-20713, 31486> velocity=< 2, -3>
position=< 21099, 10576> velocity=<-2, -1>
position=< 52440, 31490> velocity=<-5, -3>
position=< 52441, -31241> velocity=<-5, 3>
position=< 42002, -52147> velocity=<-4, 5>
position=< 52469, 31483> velocity=<-5, -3>
position=< 10658, -41688> velocity=<-1, 4>
position=<-10273, 21031> velocity=< 1, -2>
position=<-52083, -10331> velocity=< 5, 1>
position=<-52044, -31234> velocity=< 5, 3>
position=< 21110, 31482> velocity=<-2, -3>
position=<-41590, -52145> velocity=< 4, 5>
position=<-20689, 10577> velocity=< 2, -1>
position=< 31593, -41697> velocity=<-3, 4>
position=< 10673, 41935> velocity=<-1, -4>
position=<-20677, -10331> velocity=< 2, 1>
position=< 52469, 41940> velocity=<-5, -4>
position=< 31593, -31242> velocity=<-3, 3>
position=< 52467, -41697> velocity=<-5, 4>
position=< 42007, 10582> velocity=<-4, -1>
position=<-31173, -41692> velocity=< 3, 4>
position=< 21083, 21032> velocity=<-2, -2>
position=<-20730, 41939> velocity=< 2, -4>
position=< 31574, -10335> velocity=<-3, 1>
position=< 21139, -52143> velocity=<-2, 5>
position=<-52099, -52150> velocity=< 5, 5>
position=<-10252, 52398> velocity=< 1, -5>
position=< 42047, 21029> velocity=<-4, -2>
position=< 42030, 31484> velocity=<-4, -3>
position=<-10268, -41697> velocity=< 1, 4>
position=< 21080, 31484> velocity=<-2, -3>
position=< 41997, 41935> velocity=<-4, -4>
position=< 10653, -41693> velocity=<-1, 4>
position=< 21089, -52142> velocity=<-2, 5>
position=< 10652, 31486> velocity=<-1, -3>
position=< 21107, -52145> velocity=<-2, 5>
position=<-20718, -10335> velocity=< 2, 1>
position=< 21094, 41941> velocity=<-2, -4>
position=< 21097, 41935> velocity=<-2, -4>
position=< 10624, -20785> velocity=<-1, 2>
position=< 21078, -10328> velocity=<-2, 1>
position=< 52464, 41938> velocity=<-5, -4>
position=< 42006, 21031> velocity=<-4, -2>
position=< 10675, -20789> velocity=<-1, 2>
position=<-20677, 52396> velocity=< 2, -5>
position=<-52095, 52396> velocity=< 5, -5>
position=< 21079, -10333> velocity=<-2, 1>
position=<-41589, 10577> velocity=< 4, -1>
position=< 31556, -41691> velocity=<-3, 4>
position=< 31545, 31482> velocity=<-3, -3>
position=< 10644, 31487> velocity=<-1, -3>
position=<-20706, -41690> velocity=< 2, 4>
position=<-10266, 31481> velocity=< 1, -3>
position=< 21087, -41693> velocity=<-2, 4>
position=<-10239, -41695> velocity=< 1, 4>
position=<-52090, 21031> velocity=< 5, -2>
position=<-10236, 10578> velocity=< 1, -1>
position=<-10236, 41942> velocity=< 1, -4>
position=< 21086, 31483> velocity=<-2, -3>
position=<-31176, -10330> velocity=< 3, 1>
position=< 31574, -10330> velocity=<-3, 1>
position=<-41598, 31482> velocity=< 4, -3>
position=<-52087, -52144> velocity=< 5, 5>
position=< 21090, -31243> velocity=<-2, 3>
position=<-20695, -41693> velocity=< 2, 4>
position=< 31574, -41692> velocity=<-3, 4>
position=<-10240, -52148> velocity=< 1, 5>
position=< 42042, 31481> velocity=<-4, -3>
position=< 41986, 31481> velocity=<-4, -3>
position=< 52484, -10326> velocity=<-5, 1>
position=< 31593, -10330> velocity=<-3, 1>
position=< 31548, 52398> velocity=<-3, -5>
position=<-20719, 21027> velocity=< 2, -2>
position=<-20697, -31243> velocity=< 2, 3>
position=< 10680, -20781> velocity=<-1, 2>
position=<-52088, 21031> velocity=< 5, -2>
position=<-31176, 41943> velocity=< 3, -4>
position=< 41996, -52147> velocity=<-4, 5>
position=<-31176, 41944> velocity=< 3, -4>
position=<-52056, -41697> velocity=< 5, 4>
position=< 31593, 41944> velocity=<-3, -4>
position=< 21099, -41695> velocity=<-2, 4>
position=<-20697, -31243> velocity=< 2, 3>
position=< 31588, 41938> velocity=<-3, -4>
position=<-20728, 21031> velocity=< 2, -2>
position=<-31149, 21027> velocity=< 3, -2>
position=<-10223, -10329> velocity=< 1, 1>
position=<-10236, 21034> velocity=< 1, -2>
position=< 10632, -10333> velocity=<-1, 1>
position=< 10680, 10581> velocity=<-1, -1>
position=<-41641, -41688> velocity=< 4, 4>
position=< 31545, -41696> velocity=<-3, 4>
position=<-31139, 41937> velocity=< 3, -4>
position=<-31189, 52395> velocity=< 3, -5>
position=< 42036, -10335> velocity=<-4, 1>
position=< 41998, 31485> velocity=<-4, -3>
position=<-41637, -10326> velocity=< 4, 1>
position=<-20682, -20787> velocity=< 2, 2>
position=<-10241, 31490> velocity=< 1, -3>
position=< 21135, -31239> velocity=<-2, 3>
position=< 31561, -31240> velocity=<-3, 3>
position=<-31192, -20786> velocity=< 3, 2>
position=<-10236, 41944> velocity=< 1, -4>
position=<-52059, -41697> velocity=< 5, 4>
position=<-20733, -20787> velocity=< 2, 2>
position=< 31540, -20781> velocity=<-3, 2>
position=<-52055, 10573> velocity=< 5, -1>
position=<-31152, -31236> velocity=< 3, 3>
position=< 52496, -31240> velocity=<-5, 3>
position=<-52052, 31484> velocity=< 5, -3>
position=< 52453, 41940> velocity=<-5, -4>
position=<-41646, -10333> velocity=< 4, 1>
position=< 42042, 41941> velocity=<-4, -4>
position=<-31131, 21036> velocity=< 3, -2>
position=<-10281, -52145> velocity=< 1, 5>
position=<-41598, 31485> velocity=< 4, -3>
position=<-31179, -10327> velocity=< 3, 1>
position=< 41988, -10332> velocity=<-4, 1>
position=< 52456, 41936> velocity=<-5, -4>
position=<-41595, -10335> velocity=< 4, 1>
position=< 52445, 52390> velocity=<-5, -5>
position=<-20693, 31483> velocity=< 2, -3>
position=<-41630, 31484> velocity=< 4, -3>
position=< 41986, 10581> velocity=<-4, -1>
position=<-20734, 21035> velocity=< 2, -2>
position=<-10223, 52395> velocity=< 1, -5>
position=< 42012, -31238> velocity=<-4, 3>
position=< 52445, -31237> velocity=<-5, 3>
position=< 21110, -20784> velocity=<-2, 2>
position=<-31179, -10328> velocity=< 3, 1>
position=<-10251, 41944> velocity=< 1, -4>
position=< 31593, 21028> velocity=<-3, -2>
position=<-31167, -52150> velocity=< 3, 5>
position=< 42003, -52151> velocity=<-4, 5>
position=< 52485, -10334> velocity=<-5, 1>
position=<-20690, -20786> velocity=< 2, 2>
position=< 10625, -31242> velocity=<-1, 3>
position=<-52087, -10332> velocity=< 5, 1>
position=< 10672, -20783> velocity=<-1, 2>
position=< 42026, 31489> velocity=<-4, -3>
position=< 10672, 41937> velocity=<-1, -4>
position=< 41995, -41697> velocity=<-4, 4>
position=< 31572, 31481> velocity=<-3, -3>
position=<-20688, 52389> velocity=< 2, -5>
position=<-20725, 10574> velocity=< 2, -1>
position=<-10260, -10328> velocity=< 1, 1>
position=< 10685, 21034> velocity=<-1, -2>
position=< 52499, 10577> velocity=<-5, -1>
position=< 31536, 31488> velocity=<-3, -3>
position=<-10260, 41941> velocity=< 1, -4>
position=<-52089, -20789> velocity=< 5, 2>
position=< 41994, 21034> velocity=<-4, -2>
position=< 31549, -52147> velocity=<-3, 5>
position=< 31564, -10328> velocity=<-3, 1>
position=< 10645, 41943> velocity=<-1, -4>
position=<-31168, -52147> velocity=< 3, 5>
position=<-31187, 21034> velocity=< 3, -2>
position=<-41598, -31241> velocity=< 4, 3>
position=< 10653, 21035> velocity=<-1, -2>
position=< 31576, 52398> velocity=<-3, -5>
position=<-10249, 10582> velocity=< 1, -1>
position=<-31136, -31240> velocity=< 3, 3>
position=<-20712, -52151> velocity=< 2, 5>
position=<-10279, 10579> velocity=< 1, -1>
position=< 52469, -10326> velocity=<-5, 1>
position=< 21103, -20784> velocity=<-2, 2>
position=< 21136, -20785> velocity=<-2, 2>
position=<-41605, -52142> velocity=< 4, 5>
position=< 31551, -52147> velocity=<-3, 5>

33
2018-go/day_10/main.go Normal file
View File

@@ -0,0 +1,33 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
)
type point struct {
x, y int
Vx, Vy int
}
func main() {
var points []*point
file, err := os.Open("./input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scan := bufio.NewScanner(file)
for scan.Scan() {
var p *point
line := scan.Text()
fmt.Sscanf(line, "position=<%d, %d> velocity=<%d, %d>", &p.x, &p.y, &p.Vx, &p.Vy)
points = append(points, p)
}
}

110
2018-go/day_10/prova.go Normal file
View File

@@ -0,0 +1,110 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
)
type point struct {
x, y int
vx, vy int
}
func print(points []*point, xrange, yrange, x, y int) {
var res = make([][]string, yrange+1)
for i := range res {
res[i] = make([]string, xrange+1)
}
for i := 0; i < xrange+1; i++ {
for j := 0; j < yrange+1; j++ {
res[j][i] = " "
}
}
for _, p := range points {
res[(*p).y-y][(*p).x-x] = "*"
}
fmt.Println("Part 1: ")
for _, row := range res {
fmt.Println(row)
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func move(points []*point, x, X, y, Y *int) {
var tx, tX, ty, tY int = 2147483647, -2147483648, 2147483647, -2147483648
for _, val := range points {
(*val).x += (*val).vx
(*val).y += (*val).vy
tx = min(tx, (*val).x)
tX = max(tX, (*val).x)
ty = min(ty, (*val).y)
tY = max(tY, (*val).y)
}
*x, *X, *y, *Y = tx, tX, ty, tY
}
func main() {
file, err := os.Open("input")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var points []*point
var x, X, y, Y int = 2147483647, -2147483648, 2147483647, -2147483648
for scanner.Scan() {
p := new(point)
line := scanner.Text()
fmt.Sscanf(line, "position=<%d, %d> velocity=<%d, %d>", &p.x, &p.y, &p.vx, &p.vy)
points = append(points, p)
x = min(x, (*p).x)
X = max(X, (*p).x)
y = min(y, (*p).y)
Y = max(Y, (*p).y)
}
var minAt, minVal int
minVal = (X - x) + (Y - y)
for i := 1; i <= 100000; i++ {
move(points, &x, &X, &y, &Y)
current := (X - x) + (Y - y)
if current < minVal {
minVal = current
minAt = i
}
}
var tx, tX, ty, tY int = 2147483647, -2147483648, 2147483647, -2147483648
for _, p := range points {
(*p).x = (*p).x - (100000-minAt)*(*p).vx
(*p).y = (*p).y - (100000-minAt)*(*p).vy
tx = min(tx, (*p).x)
tX = max(tX, (*p).x)
ty = min(ty, (*p).y)
tY = max(tY, (*p).y)
}
print(points, tX-tx, tY-ty, tx, ty)
fmt.Printf("Part 2: %v\n", minAt)
}

98
2018-go/day_11/main.go Normal file
View File

@@ -0,0 +1,98 @@
package main
import (
"fmt"
"math"
)
const gridSize int = 300
const gridSerial int = 7347
func cellPower(x int, y int, serial int) int {
rackID := x + 1 + 10
powerLevel := rackID * (y + 1)
powerLevel += serial
powerLevel *= rackID
powerLevel %= int(math.Pow(10, 3))
powerLevel /= int(math.Pow(10, 2))
return powerLevel
}
func maxPowerSquare(powerGrid *[gridSize][gridSize]int, powerGridCheck *[gridSize][gridSize]bool, size int, res chan<- [4]int) {
maxSum := 0
maxSumCheck := false
maxX, maxY := 0, 0
for x := 0; x < gridSize-size+1; x++ {
for y := 0; y < gridSize-size+1; y++ {
powerSum := 0
for xi := x; xi < x+size; xi++ {
for yi := y; yi < y+size; yi++ {
if !powerGridCheck[xi][yi] {
powerGrid[xi][yi] = cellPower(xi, yi, gridSerial)
powerGridCheck[xi][yi] = true
}
powerSum += powerGrid[xi][yi]
}
}
if powerSum > maxSum || !maxSumCheck {
maxSum = powerSum
maxSumCheck = true
maxX = x
maxY = y
}
}
}
res <- [4]int{size, maxSum, maxX + 1, maxY + 1}
}
func main() {
var powerGrid [gridSize][gridSize]int
var powerGridCheck [gridSize][gridSize]bool
var res = make(chan [4]int)
go func() {
maxPowerSquare(&powerGrid, &powerGridCheck, 3, res)
close(res)
}()
p1 := <-res
fmt.Printf("Part One: %v,%v\n", p1[2], p1[3])
res = make(chan [4]int)
for i := 0; i < gridSize; i++ {
go func(i int) {
maxPowerSquare(&powerGrid, &powerGridCheck, i, res)
if i == gridSize-1 {
close(res)
}
}(i)
}
maxSum := 0
maxSumCheck := false
maxX, maxY := 0, 0
maxSize := 0
for r := range res {
fmt.Printf("Size: %v\t Sum: %v\t x: %v\t y: %v\n", r[0], r[1], r[2], r[3])
if r[1] > maxSum || !maxSumCheck {
maxSize = r[0]
maxSum = r[1]
maxSumCheck = true
maxX = r[2]
maxY = r[3]
}
}
fmt.Printf("Part Two: %v,%v,%v\n", maxX, maxY, maxSize)
res = make(chan [4]int)
go func() {
maxPowerSquare(&powerGrid, &powerGridCheck, 299, res)
close(res)
}()
p1 = <-res
fmt.Printf("Part Two 299: %v,%v,%v\n", p1[1], p1[2], p1[3])
}

77
2018-go/day_11/prova.go Normal file
View File

@@ -0,0 +1,77 @@
package main
import (
"fmt"
"strconv"
)
func calculate(i int, j int, memo *[][]int, ps *[][]int, input int) int {
t := 0
for x := i; x < i+3; x++ {
for y := j; y < j+3; y++ {
if (*memo)[y][x] != 0 {
t += (*memo)[y][x]
} else {
id := x + 10
serial := (id*y + input)
hundreds := strconv.Itoa(serial * id)
var result int
if len(hundreds) >= 3 {
result, _ = strconv.Atoi(string(hundreds[len(hundreds)-3]))
}
result -= 5
t += result
(*memo)[y][x] = result
(*ps)[y][x] = result + (*ps)[y-1][x] + (*ps)[y][x-1] - (*ps)[y-1][x-1]
}
}
}
return t
}
func main() {
input := 7347
var res, X, Y int
// technically memo isn't needed anymore but I added partial sums after
// part 2 and didn't wanna clean up my code after for part 1
memo := make([][]int, 301)
partialSums := make([][]int, 301)
for i := range memo {
memo[i] = make([]int, 301)
partialSums[i] = make([]int, 301)
}
for i := 1; i <= 298; i++ {
for j := 1; j <= 298; j++ {
if c := calculate(i, j, &memo, &partialSums, input); c > res {
res = c
X = i
Y = j
}
}
}
fmt.Printf("Part 1: %v, %v, %v\n", res, X, Y)
res = 0
var sz int
for size := 1; size < 300; size++ {
for i := size; i <= 300; i++ {
for j := size; j <= 300; j++ {
if c := partialSums[i][j] - partialSums[i-size][j] -
partialSums[i][j-size] + partialSums[i-size][j-size]; c > res {
res = c
Y = i - size + 1
X = j - size + 1
sz = size
}
}
}
}
fmt.Printf("Part 2: %v, %v, %v, %v\n", res, X, Y, sz)
}