Day9
This commit is contained in:
parent
bcacad9d15
commit
5e81d05091
@ -11,8 +11,11 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="400bdf61-8921-41d3-8f98-a0a1b0877f46" name="Default Changelist" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/day2/day2.go" beforeDir="false" afterPath="$PROJECT_DIR$/day2/day2.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/day3/day3.go" beforeDir="false" afterPath="$PROJECT_DIR$/day3/day3.go" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/day9/day9.go" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/day9/day9Test.txt" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/day1/day1.go" beforeDir="false" afterPath="$PROJECT_DIR$/day1/day1.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/helper/readFile.go" beforeDir="false" afterPath="$PROJECT_DIR$/helper/helper.go" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -44,7 +47,7 @@
|
||||
<property name="configurable.Project.GOPATH.is.expanded" value="true" />
|
||||
<property name="go.import.settings.migrated" value="true" />
|
||||
<property name="go.sdk.automatically.set" value="true" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$/day3" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$/day6" />
|
||||
<property name="settings.editor.selected.configurable" value="com.goide.configuration.GoLibrariesConfigurableProvider" />
|
||||
</component>
|
||||
<component name="RecentsManager">
|
||||
@ -53,7 +56,7 @@
|
||||
<recent name="C:\Users\karls\go\src\AoC2020" />
|
||||
</key>
|
||||
</component>
|
||||
<component name="RunManager" selected="Go Build.go build day5.go">
|
||||
<component name="RunManager" selected="Go Build.go build day9.go">
|
||||
<configuration name="go build day1.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day1" />
|
||||
@ -109,7 +112,7 @@
|
||||
<configuration name="go build day5.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day5" />
|
||||
<parameters value=""BFFFBBFRRR"" />
|
||||
<parameters value="day5Input.txt" />
|
||||
<kind value="FILE" />
|
||||
<filePath value="$PROJECT_DIR$/day5/day5.go" />
|
||||
<package value="AoC2020" />
|
||||
@ -117,12 +120,56 @@
|
||||
<output_directory value="$PROJECT_DIR$/day5" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="go build day6.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day6" />
|
||||
<parameters value="day6Input.txt" />
|
||||
<kind value="FILE" />
|
||||
<filePath value="$PROJECT_DIR$/day6/day6.go" />
|
||||
<package value="AoC2020" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="go build day7.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day7" />
|
||||
<parameters value="day7Input.txt" />
|
||||
<kind value="FILE" />
|
||||
<filePath value="$PROJECT_DIR$/day7/day7.go" />
|
||||
<package value="AoC2020" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="go build day8.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day8" />
|
||||
<parameters value="day8Input.txt" />
|
||||
<kind value="FILE" />
|
||||
<filePath value="$PROJECT_DIR$/day8/day8.go" />
|
||||
<package value="AoC2020" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="go build day9.go" type="GoApplicationRunConfiguration" factoryName="Go Application" nameIsGenerated="true">
|
||||
<module name="AoC2020" />
|
||||
<working_directory value="$PROJECT_DIR$/day9" />
|
||||
<parameters value="day9Input.txt" />
|
||||
<kind value="FILE" />
|
||||
<filePath value="$PROJECT_DIR$/day9/day9.go" />
|
||||
<package value="AoC2020" />
|
||||
<directory value="$PROJECT_DIR$" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<list>
|
||||
<item itemvalue="Go Build.go build day1.go" />
|
||||
<item itemvalue="Go Build.go build day2.go" />
|
||||
<item itemvalue="Go Build.go build day3.go" />
|
||||
<item itemvalue="Go Build.go build day4.go" />
|
||||
<item itemvalue="Go Build.go build day5.go" />
|
||||
<item itemvalue="Go Build.go build day7.go" />
|
||||
<item itemvalue="Go Build.go build day6.go" />
|
||||
<item itemvalue="Go Build.go build day8.go" />
|
||||
<item itemvalue="Go Build.go build day9.go" />
|
||||
</list>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
@ -147,39 +194,39 @@
|
||||
<option name="oldMeFiltersMigrated" value="true" />
|
||||
</component>
|
||||
<component name="WindowStateProjectService">
|
||||
<state x="448" y="103" width="1092" height="859" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1607175613035">
|
||||
<state x="351" y="0" width="1092" height="955" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1607534020287">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state x="-1472" y="103" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607019292652" />
|
||||
<state x="448" y="103" width="1092" height="859" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607175613035" />
|
||||
<state x="351" y="0" width="1092" height="955" key="#com.intellij.execution.impl.EditConfigurationsDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607534020287" />
|
||||
<state x="661" y="322" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1606836882379">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state x="661" y="322" key="#com.intellij.fileTypes.FileTypeChooser/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1606836882379" />
|
||||
<state x="739" y="287" key="FileChooserDialogImpl" timestamp="1607016918790">
|
||||
<state x="739" y="287" key="FileChooserDialogImpl" timestamp="1607278759015">
|
||||
<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="1607016918790" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.bottom" timestamp="1607175589852">
|
||||
<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="266" key="GridCell.Tab.0.bottom" timestamp="1607533993744">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607175230302" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607175589852" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.center" timestamp="1607175589852">
|
||||
<state width="1877" height="266" key="GridCell.Tab.0.bottom/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607533993744" />
|
||||
<state width="1877" height="266" key="GridCell.Tab.0.center" timestamp="1607533993744">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607175230302" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607175589852" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.left" timestamp="1607175589852">
|
||||
<state width="1877" height="266" key="GridCell.Tab.0.center/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607533993744" />
|
||||
<state width="1877" height="266" key="GridCell.Tab.0.left" timestamp="1607533993743">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607175230302" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607175589852" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.right" timestamp="1607175589852">
|
||||
<state width="1877" height="266" key="GridCell.Tab.0.left/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607533993743" />
|
||||
<state width="1877" height="266" key="GridCell.Tab.0.right" timestamp="1607533993744">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@-1920.0.1920.1040" timestamp="1607175230302" />
|
||||
<state width="1877" height="304" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607175589852" />
|
||||
<state width="1877" height="266" key="GridCell.Tab.0.right/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607533993744" />
|
||||
<state x="782" y="240" key="RollbackChangesDialog" timestamp="1607174263188">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
@ -188,10 +235,14 @@
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state x="461" y="165" key="SettingsEditor/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607081830252" />
|
||||
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog" timestamp="1607174299439">
|
||||
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog" timestamp="1607177137138">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607174299439" />
|
||||
<state x="92" y="92" width="1736" height="856" key="com.intellij.history.integration.ui.views.FileHistoryDialog/-1920.0.1920.1040/0.0.1920.1040@0.0.1920.1040" timestamp="1607177137138" />
|
||||
<state x="593" y="141" width="585" height="801" key="find.popup" timestamp="1607533841136">
|
||||
<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="1607533841136" />
|
||||
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1607081728725">
|
||||
<screen x="0" y="0" width="1920" height="1040" />
|
||||
</state>
|
||||
|
22
day1/day1.go
22
day1/day1.go
@ -19,26 +19,16 @@ func main() {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(day1(numbers))
|
||||
fmt.Println(day1(numbers, 2020))
|
||||
fmt.Println(day1_part2(numbers))
|
||||
}
|
||||
|
||||
func day1(numbers []int) int {
|
||||
goal := 2020
|
||||
sort.Ints(numbers)
|
||||
p1 := 0
|
||||
p2 := len(numbers) - 1
|
||||
for {
|
||||
if numbers[p1]+numbers[p2] < goal {
|
||||
p1++
|
||||
}
|
||||
if numbers[p1]+numbers[p2] > goal {
|
||||
p2--
|
||||
}
|
||||
if numbers[p1]+numbers[p2] == goal {
|
||||
return numbers[p1] * numbers[p2]
|
||||
}
|
||||
func day1(numbers []int, goal int) int {
|
||||
add1, add2, err := helper.FindAddendsForSum(numbers,goal)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return add1 * add2
|
||||
}
|
||||
|
||||
func day1_part2(numbers []int) (int, error) {
|
||||
|
74
day9/day9.go
Normal file
74
day9/day9.go
Normal file
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"AoC2020/helper"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var pointer int
|
||||
var band []int
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
input, err := helper.GetInput(args[0])
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
numbers, err := helper.MapToNumber(input)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Printf("%v \n",numbers)
|
||||
band = numbers
|
||||
breaker := part1(25)
|
||||
fmt.Println(breaker)
|
||||
fmt.Print(part2(breaker))
|
||||
}
|
||||
|
||||
func part1(preambleLen int) int{
|
||||
pointer = preambleLen
|
||||
isValid := true
|
||||
for isValid {
|
||||
_,_,err := helper.FindAddendsForSum(band[pointer-preambleLen:pointer],band[pointer])
|
||||
if err != nil {
|
||||
return band[pointer]
|
||||
isValid = false
|
||||
}
|
||||
pointer++
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func part2(breaker int) int{
|
||||
for p1 := 0; p1 < len(band); p1++ {
|
||||
for p2 := p1 + 1; p2 < len(band); p2++ {
|
||||
array := make([]int, len(band[p1:p2]))
|
||||
copy(array,band[p1:p2])
|
||||
sumArray := 0
|
||||
for _,value := range array {
|
||||
sumArray += value
|
||||
}
|
||||
if sumArray == breaker {
|
||||
fmt.Printf("%v \n" ,array)
|
||||
var max int
|
||||
var min int
|
||||
for i, val := range array {
|
||||
if i==0 || val < min {
|
||||
min = val
|
||||
}
|
||||
if i==0 || val > max {
|
||||
max = val
|
||||
}
|
||||
}
|
||||
fmt.Printf("%d , %d \n" ,min, max)
|
||||
return min + max
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func run(preambleLen int) {
|
||||
|
||||
}
|
1000
day9/day9Input.txt
Normal file
1000
day9/day9Input.txt
Normal file
File diff suppressed because it is too large
Load Diff
20
day9/day9Test.txt
Normal file
20
day9/day9Test.txt
Normal file
@ -0,0 +1,20 @@
|
||||
35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576
|
@ -1,7 +1,9 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -34,3 +36,23 @@ func MapToNumber(strings []string) ([]int, error) {
|
||||
}
|
||||
return numbers, nil
|
||||
}
|
||||
|
||||
func FindAddendsForSum (possibleAddends []int, sum int) (int, int, error) {
|
||||
var array = make([]int,len(possibleAddends))
|
||||
copy(array,possibleAddends)
|
||||
sort.Ints(array)
|
||||
p1 := 0
|
||||
p2 := len(array) - 1
|
||||
for p1 < p2 {
|
||||
if array[p1]+array[p2] < sum {
|
||||
p1++
|
||||
}
|
||||
if array[p1]+array[p2] > sum {
|
||||
p2--
|
||||
}
|
||||
if array[p1]+array[p2] == sum {
|
||||
return array[p1] , array[p2], nil
|
||||
}
|
||||
}
|
||||
return 0,0,errors.New("No fitting Addends found")
|
||||
}
|
Loading…
Reference in New Issue
Block a user