Last active
December 3, 2017 23:51
-
-
Save slopp/158e970ed05fbe36e1f740876bf011a4 to your computer and use it in GitHub Desktop.
Advent Of Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "strconv" | |
| "math" | |
| ) | |
| func main() { | |
| input := "823936645345581272695677318513459491834641129844393742672553544439126314399846773234845535593355348931499496184839582118817689171948635864427852215325421433717458975771369522138766248225963242168658975326354785415252974294317138511141826226866364555761117178764543435899886711426319675443679829181257496966219435831621565519667989898725836639626681645821714861443141893427672384716732765884844772433374798185955741311116365899659833634237938878181367317218635539667357364295754744829595842962773524584225427969467467611641591834876769829719248136613147351298534885563144114336211961674392912181735773851634298227454157885241769156811787611897349965331474217223461176896643242975397227859696554492996937235423272549348349528559432214521551656971136859972232854126262349381254424597348874447736545722261957871275935756764184378994167427983811716675476257858556464755677478725146588747147857375293675711575747132471727933773512571368467386151966568598964631331428869762151853634362356935751298121849281442128796517663482391226174256395515166361514442624944181255952124524815268864131969151433888721213595267927325759562132732586252438456569556992685896517565257787464673718221817783929691626876446423134331749327322367571432532857235214364221471769481667118117729326429556357572421333798517168997863151927281418238491791975399357393494751913155219862399959646993428921878798119215675548847845477994836744929918954159722827194721564121532315459611433157384994543332773796862165243183378464731546787498174844781781139571984272235872866886275879944921329959736315296733981313643956576956851762149275521949177991988236529475373595217665112434727744235789852852765675189342753695377219374791548554786671473733124951946779531847479755363363288448281622183736545494372344785112312749694167483996738384351293899149136857728545977442763489799693492319549773328626918874718387697878235744154491677922317518952687439655962477734559232755624943644966227973617788182213621899579391324399386146423427262874437992579573858589183571854577861459758534348533553925167947139351819511798829977371215856637215221838924612644785498936263849489519896548811254628976642391428413984281758771868781714266261781359762798" | |
| inputNum := toInt(input) | |
| // first challenge | |
| matches := matchAdjacent(inputNum) | |
| fmt.Println(sum(matches)) | |
| // second challenge | |
| matches = matchHalfway(inputNum) | |
| fmt.Println(sum(matches)) | |
| // Generalize | |
| fmt.Println(sum(matchStep(inputNum, 1)) == sum(matchAdjacent(inputNum))) | |
| fmt.Println(sum(matchStep(inputNum, len(inputNum)/2)) == sum(matchHalfway(inputNum))) | |
| } | |
| func matchAdjacent(ints []int) []int { | |
| var matched []int | |
| prev := ints[len(ints)-1] | |
| for _, v := range ints { | |
| if v == prev { | |
| matched = append(matched, v) | |
| } | |
| prev = v | |
| } | |
| return matched | |
| } | |
| func matchHalfway(ints []int) []int { | |
| l := len(ints) | |
| steps := l/2 | |
| var matched []int | |
| for i, v := range ints { | |
| idx := i+steps | |
| if idx > l-1 { | |
| idx = int(math.Mod(float64(idx), float64(l))) | |
| } | |
| if v == ints[idx] { | |
| matched = append(matched, v) | |
| } | |
| } | |
| return matched | |
| } | |
| func matchStep(ints []int, steps int) []int { | |
| l := len(ints) | |
| var matched []int | |
| for i, v := range ints { | |
| idx := i+steps | |
| if idx > l-1 { | |
| idx = int(math.Mod(float64(idx), float64(l))) | |
| } | |
| if v == ints[idx] { | |
| matched = append(matched, v) | |
| } | |
| } | |
| return matched | |
| } | |
| func toInt(s string) []int { | |
| inputNum := make([]int, len(s)) | |
| i:= 0 | |
| for i < len(s) { | |
| inputNum[i], _ = strconv.Atoi(string([]rune(s)[i])) | |
| i+=1 | |
| } | |
| return inputNum | |
| } | |
| func sum(input []int) int { | |
| s := 0 | |
| for i, _ := range(input) { | |
| s += input[i] | |
| } | |
| return s | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "strconv" | |
| "strings" | |
| ) | |
| func main() { | |
| //input := "5x1x9x5q7x5x3q2x4x6x8q" | |
| input := "1364x461x1438x1456x818x999x105x1065x314x99x1353x148x837x590x404x123q204x99x235x2281x2848x3307x1447x3848x3681x963x3525x525x288x278x3059x821q280x311x100x287x265x383x204x380x90x377x398x99x194x297x399x87q7698x2334x7693x218x7344x3887x3423x7287x7700x2447x7412x6147x231x1066x248x208q3740x837x4144x123x155x2494x1706x4150x183x4198x1221x4061x95x148x3460x550q1376x1462x73x968x95x1721x544x982x829x1868x1683x618x82x1660x83x1778q197x2295x5475x2886x2646x186x5925x237x3034x5897x1477x196x1778x3496x5041x3314q179x2949x3197x2745x1341x3128x1580x184x1026x147x2692x212x2487x2947x3547x1120q460x73x52x373x41x133x671x61x634x62x715x644x182x524x648x320q169x207x5529x4820x248x6210x255x6342x4366x5775x5472x3954x3791x1311x7074x5729q5965x7445x2317x196x1886x3638x266x6068x6179x6333x229x230x1791x6900x3108x5827q212x249x226x129x196x245x187x332x111x126x184x99x276x93x222x56q51x592x426x66x594x406x577x25x265x578x522x57x547x65x564x622q215x2092x1603x1001x940x2054x245x2685x206x1043x2808x208x194x2339x2028x2580q378x171x155x1100x184x937x792x1436x1734x179x1611x1349x647x1778x1723x1709q4463x4757x201x186x3812x2413x2085x4685x5294x5755x2898x200x5536x5226x1028x180q" | |
| rows := strings.Split(input, "q") | |
| rang := make([]int, len(rows)) | |
| var min, max int | |
| for i, r := range rows { | |
| row := strings.Split(r, "x") | |
| for j, s := range(row) { | |
| n, _ := strconv.Atoi(s) | |
| if j == 0 { | |
| min = n | |
| max = n | |
| } else { | |
| if n < min { | |
| min = n | |
| } | |
| if n > max { | |
| max = n | |
| } | |
| } | |
| } | |
| rang[i] = max - min | |
| } | |
| fmt.Println(sum(rang)) | |
| } | |
| func sum(input []int) int { | |
| s := 0 | |
| for i, _ := range(input) { | |
| s += input[i] | |
| } | |
| return s | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment