diff --git a/Makefile b/Makefile index 06526e9..a244b51 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,8 @@ day09: go run main.go day09 input day10: + go run main.go day10 input + day11: day12: day13: diff --git a/day10/example.txt b/day10/example.txt new file mode 100644 index 0000000..ec4a03f --- /dev/null +++ b/day10/example.txt @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 diff --git a/day10/example2.txt b/day10/example2.txt new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/day10/example2.txt @@ -0,0 +1,31 @@ +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3 diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..2145ae2 --- /dev/null +++ b/day10/input.txt @@ -0,0 +1,100 @@ +44 +41 +48 +17 +35 +146 +73 +3 +16 +159 +11 +29 +32 +63 +65 +62 +126 +151 +6 +124 +87 +115 +122 +43 +12 +85 +2 +98 +59 +156 +149 +66 +10 +82 +26 +79 +56 +22 +74 +49 +25 +69 +54 +19 +108 +18 +55 +131 +140 +15 +125 +37 +129 +91 +51 +158 +117 +136 +142 +109 +64 +36 +160 +150 +42 +118 +101 +78 +28 +105 +110 +40 +157 +70 +97 +139 +152 +47 +104 +81 +27 +116 +132 +143 +1 +80 +75 +141 +133 +9 +50 +153 +123 +111 +119 +130 +112 +94 +90 +86 diff --git a/day10/parse.go b/day10/parse.go new file mode 100644 index 0000000..687a69e --- /dev/null +++ b/day10/parse.go @@ -0,0 +1,28 @@ +package day10 + +import ( + "sort" + "strconv" + + "git.elis.nu/etu/aoc2020/utils" +) + +var rows []int + +func ParseFile(input string) { + // Add zero as start number + rows = append(rows, 0) + + // Parse file + for _, line := range utils.GetLinesFromFile("day10/" + input + ".txt") { + num, _ := strconv.Atoi(line) + + rows = append(rows, num) + } + + // Sort the numbers, will always be needed. And is required for calculation of consolues jolt usage below. + sort.Ints(rows) + + // Add the consoles jolt usage by taking the last number plus 3 + rows = append(rows, rows[len(rows)-1]+3) +} diff --git a/day10/solve1.go b/day10/solve1.go new file mode 100644 index 0000000..4fdf4de --- /dev/null +++ b/day10/solve1.go @@ -0,0 +1,26 @@ +package day10 + +import ( + "log" +) + +func Solve1() { + count1 := 0 + count3 := 0 + + for key, value := range rows { + if key+1 >= len(rows) { + break + } + + if value+1 == rows[key+1] { + count1++ + } + + if value+3 == rows[key+1] { + count3++ + } + } + + log.Printf("2020-12-10.01: Answer: %d\n", count1*count3) +} diff --git a/day10/solve2.go b/day10/solve2.go new file mode 100644 index 0000000..b27129b --- /dev/null +++ b/day10/solve2.go @@ -0,0 +1,41 @@ +package day10 + +import ( + "log" +) + +func Solve2() { + log.Printf("2020-12-10.02: Answer: %d\n", calc(rows)) +} + +func calc(data []int) int { + // Some cached answers, these works since we check if they + // increment by one when we split up the data below, so if the + // chain is a certain length, we know it works. + switch len(data) { + case 0, 1, 2: + return 1 + case 3: + return 2 + case 4: + return 4 + case 5: + return 7 + } + + // Locate a good place to break the chain in the middle + i := 0 + for i = 0; i < len(data)-1; i++ { + if data[i+1] == data[i]+1 { + continue + } + + break + } + + if len(data[:i]) >= 0 { + return calc(data[:i+1]) * calc(data[i+1:]) + } + + return 0 +} diff --git a/main.go b/main.go index d49ad84..aa8fba4 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( "git.elis.nu/etu/aoc2020/day07" "git.elis.nu/etu/aoc2020/day08" "git.elis.nu/etu/aoc2020/day09" + "git.elis.nu/etu/aoc2020/day10" "git.elis.nu/etu/aoc2020/utils" ) @@ -48,5 +49,8 @@ func main() { case "day09": utils.Perf("2020-12-09", day09.ParseFile, day09.Solve1, day09.Solve2) + + case "day10": + utils.Perf("2020-12-10", day10.ParseFile, day10.Solve1, day10.Solve2) } }