Day15
This commit is contained in:
parent
f2e83bc7d2
commit
1cccb7132d
@ -11,10 +11,9 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="400bdf61-8921-41d3-8f98-a0a1b0877f46" name="Default Changelist" comment="">
|
||||
<change afterPath="$PROJECT_DIR$/day14/day14.go" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/day14/day14Test.txt" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/day14/day14Test2.txt" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/day15/day15.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/helper/helper.go" beforeDir="false" afterPath="$PROJECT_DIR$/helper/helper.go" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -55,7 +54,7 @@
|
||||
<recent name="C:\Users\karls\go\src\AoC2020" />
|
||||
</key>
|
||||
</component>
|
||||
<component name="RunManager" selected="Go Build.go build day14.go">
|
||||
<component name="RunManager" selected="Go Build.go build day15.go">
|
||||
<configuration name="go build day10.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day10" />
|
||||
@ -106,6 +105,15 @@
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="go build day15.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$" />
|
||||
<kind value="FILE" />
|
||||
<filePath value="$PROJECT_DIR$/day15/day15.go" />
|
||||
<package value="AoC2020" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="go build day1.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day1" />
|
||||
@ -224,9 +232,11 @@
|
||||
<item itemvalue="Go Build.go build day12.go" />
|
||||
<item itemvalue="Go Build.go build day13.go" />
|
||||
<item itemvalue="Go Build.go build day14.go" />
|
||||
<item itemvalue="Go Build.go build day15.go" />
|
||||
</list>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Go Build.go build day15.go" />
|
||||
<item itemvalue="Go Build.go build day1.go" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
@ -261,26 +271,26 @@
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state x="739" y="287" key="FileChooserDialogImpl/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607278759015" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.bottom" timestamp="1607962571043">
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.bottom" timestamp="1608046616194">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629475" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607962571043" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.center" timestamp="1607962571042">
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608046616194" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.center" timestamp="1608046616194">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629474" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607962571042" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.left" timestamp="1607962571042">
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608046616194" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.left" timestamp="1608046616194">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629474" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607962571042" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.right" timestamp="1607962571042">
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608046616194" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.right" timestamp="1608046616194">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607708629475" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607962571042" />
|
||||
<state width="1877" height="386" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608046616194" />
|
||||
<state x="782" y="240" key="RollbackChangesDialog" timestamp="1607174263188">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
@ -301,9 +311,9 @@
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state x="593" y="141" width="585" height="801" key="find.popup/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607700424475" />
|
||||
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1607081728725">
|
||||
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1608045273455">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607081728725" />
|
||||
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1608045273455" />
|
||||
</component>
|
||||
</project>
|
59
day15/day15.go
Normal file
59
day15/day15.go
Normal file
@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println(run([]int{5,1,9,18,13,8,0}, 2020))
|
||||
fmt.Println(runPart2([]int{5,1,9,18,13,8,0}, 30000000))
|
||||
}
|
||||
|
||||
func run(startingNumber []int, length int) int{
|
||||
var band []int
|
||||
band = append(band, startingNumber...)
|
||||
for i := len(startingNumber); i < length; i++ {
|
||||
currentNumber := band[i-1]
|
||||
index := findIndex(currentNumber,band)
|
||||
if index == -1 {
|
||||
band = append(band, 0)
|
||||
}else {
|
||||
band = append(band, i-1-index)
|
||||
}
|
||||
}
|
||||
return band[len(band)-1]
|
||||
}
|
||||
|
||||
func runPart2(startingNumber []int, length int) int{
|
||||
var band = make (map[int][]int)
|
||||
for i, val := range startingNumber {
|
||||
band[val] = []int{i,i}
|
||||
}
|
||||
lastNumber := startingNumber[len(startingNumber)-1]
|
||||
for i := len(startingNumber); i < length; i++ {
|
||||
currentNumber := lastNumber
|
||||
indexes := band[currentNumber]
|
||||
diffIndexes := indexes[1]-indexes[0]
|
||||
if diffIndexes == 0 {
|
||||
lastNumber = 0
|
||||
band[0] = []int{band[0][1],i}
|
||||
}else {
|
||||
lastNumber = diffIndexes
|
||||
if len(band[diffIndexes]) != 0 {
|
||||
band[diffIndexes] = []int{band[diffIndexes][1],i}
|
||||
}else {
|
||||
band[diffIndexes] = []int{i,i}
|
||||
}
|
||||
}
|
||||
}
|
||||
return lastNumber
|
||||
}
|
||||
|
||||
func findIndex(number int, band []int) int{
|
||||
for i := len(band)-2; i >= 0; i-- {
|
||||
if band[i] == number {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
|
@ -56,3 +56,12 @@ func FindAddendsForSum (possibleAddends []int, sum int) (int, int, error) {
|
||||
}
|
||||
return 0,0,errors.New("No fitting Addends found")
|
||||
}
|
||||
|
||||
func SliceIndex(limit int, predicate func(i int) bool) int {
|
||||
for i := 0; i < limit; i++ {
|
||||
if predicate(i) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
Loading…
Reference in New Issue
Block a user