Skip to content

Commit

Permalink
Day 7 Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunburdick committed Dec 11, 2024
1 parent 7216fc5 commit ed6cf86
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Each day will be setup as a separate folder.
- [Day 4](/day-4/) - Ceres Search
- [Day 5](/day-5/) - Print Queue
- [Day 6](/day-6/) - Guard Gallivant
- [Day 7](/day-7/) - Bridge Repair

## Environment Setup

Expand Down
53 changes: 53 additions & 0 deletions day-7/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Day 7 - Bridge Repair

## Part 1

The Historians take you to a familiar rope bridge over a river in the middle of a jungle. The Chief isn't on this side of the bridge, though; maybe he's on the other side?

When you go to cross the bridge, you notice a group of engineers trying to repair it. (Apparently, it breaks pretty frequently.) You won't be able to cross until it's fixed.

You ask how long it'll take; the engineers tell you that it only needs final calibrations, but some young elephants were playing nearby and stole all the operators from their calibration equations! They could finish the calibrations if only someone could determine which test values could possibly be produced by placing any combination of operators into their calibration equations (your puzzle input).

For example:

```
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
```

Each line represents a single equation. The test value appears before the colon on each line; it is your job to determine whether the remaining numbers can be combined with operators to produce the test value.

Operators are always evaluated left-to-right, not according to precedence rules. Furthermore, numbers in the equations cannot be rearranged. Glancing into the jungle, you can see elephants holding two different types of operators: add (+) and multiply (\*).

Only three of the above equations can be made true by inserting operators:

- 190: 10 19 has only one position that accepts an operator: between 10 and 19. Choosing + would give 29, but choosing _ would give the test value (10 _ 19 = 190).
- 3267: 81 40 27 has two positions for operators. Of the four possible configurations of the operators, two cause the right side to match the test value: 81 + 40 _ 27 and 81 _ 40 + 27 both equal 3267 (when evaluated left-to-right)!
- 292: 11 6 16 20 can be solved in exactly one way: 11 + 6 \* 16 + 20.

The engineers just need the total calibration result, which is the sum of the test values from just the equations that could possibly be true. In the above example, the sum of the test values for the three equations listed above is 3749.

Determine which equations could possibly be true. What is their total calibration result?

## Part 2

The engineers seem concerned; the total calibration result you gave them is nowhere close to being within safety tolerances. Just then, you spot your mistake: some well-hidden elephants are holding a third type of operator.

The concatenation operator (||) combines the digits from its left and right inputs into a single number. For example, 12 || 345 would become 12345. All operators are still evaluated left-to-right.

Now, apart from the three equations that could be made true using only addition and multiplication, the above example has three more equations that can be made true by inserting operators:

- 156: 15 6 can be made true through a single concatenation: 15 || 6 = 156.
- 7290: 6 8 6 15 can be made true using 6 _ 8 || 6 _ 15.
- 192: 17 8 14 can be made true using 17 || 8 + 14.

Adding up all six test values (the three that could be made before using only + and \* plus the new three that can now be made by also using ||) produces the new total calibration result of 11387.

Using your new knowledge of elephant hiding spots, determine which equations could possibly be true. What is their total calibration result?
136 changes: 136 additions & 0 deletions day-7/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
_ "embed"
"flag"
"fmt"
"log"
"slices"
"strconv"
"strings"

file "github.com/shaunburdick/advent-of-code-2024/lib"
)

var input string

func init() {
// do this in init (not main) so test file has same input
inputFile, err := file.LoadRelativeFile("input.txt")
if err != nil {
log.Println(err)
}

input = strings.TrimRight(inputFile, "\n")
}

func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)

if part == 1 {
ans := part1(input)
fmt.Println("Output:", ans)
} else {
ans := part2(input)
fmt.Println("Output:", ans)
}
}

func part1(input string) int64 {
parsed := parseInput(input)

var total int64
total = 0

for _, calibration := range parsed {
testValue, numbers := ParseCalibration(calibration)
iter := IterateOperators(numbers[0], numbers[1:])
if slices.Contains(iter, testValue) {
total += testValue
}
}

return total
}

func part2(input string) int64 {
parsed := parseInput(input)
_ = parsed

return 0
}

func IterateOperators(carry int64, numbers []int64) (results []int64) {
addResult := ApplyOperator(Add, carry, numbers[0])
mulResult := ApplyOperator(Multiply, carry, numbers[0])

// base case
if len(numbers) == 1 {
results = append(results, addResult, mulResult)
} else {
nextAdd := IterateOperators(addResult, numbers[1:])
nextMul := IterateOperators(mulResult, numbers[1:])

results = append(results, nextAdd...)
results = append(results, nextMul...)
}

return results
}

func ParseCalibration(c string) (testValue int64, numbers []int64) {
calSplit := strings.Split(c, ":")
tv, tvErr := strconv.Atoi(calSplit[0])
if tvErr != nil {
log.Fatalf("Unable to parse test value: %s", calSplit[0])
}
testValue = int64(tv)

for i, num := range strings.Fields(calSplit[1]) {
numInt, numErr := strconv.Atoi(num)
if numErr != nil {
log.Fatalf("Unable to parse number at position %d: %s", i, num)
}

numbers = append(numbers, int64(numInt))
}

return testValue, numbers
}

type Operator int

const (
Add Operator = iota
Multiply
)

func ApplyOperator(op Operator, a int64, b int64) int64 {
switch op {
case Add:
return a + b
case Multiply:
return a * b
default:
panic("Unknown Operator")
}
}

func ApplyOperators(op Operator, numbers []int64) int64 {
total := ApplyOperator(op, numbers[0], numbers[1])

if len(numbers) > 2 {
for _, number := range numbers[2:] {
total = ApplyOperator(op, total, number)
}
}

return total
}

func parseInput(input string) (ans []string) {
return strings.Split(input, "\n")
}
98 changes: 98 additions & 0 deletions day-7/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"testing"

file "github.com/shaunburdick/advent-of-code-2024/lib"
)

type TestDeclaration struct {
name string
input string
want int64
run bool
}

var example1 = `190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20`

func Test_day7_part1(t *testing.T) {
tests := []TestDeclaration{
{
name: "example",
input: example1,
want: 3749,
run: true,
},
{
name: "actual",
input: input,
want: 1289579105366,
run: file.ExistsRelativeFile("input.txt"),
},
}
for _, tt := range tests {
if tt.run {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}
}

func Benchmark_day7_part1(b *testing.B) {
for i := 0; i < b.N; i++ {
part1(example1)
}
}

var example2 = `190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20`

func Test_day7_part2(t *testing.T) {
tests := []TestDeclaration{
{
name: "example",
input: example2,
want: 11387,
run: true,
},
{
name: "actual",
input: input,
want: 0,
run: file.ExistsRelativeFile("input.txt"),
},
}
for _, tt := range tests {
if tt.run {
t.Run(tt.name, func(t *testing.T) {
if got := part2(tt.input); got != tt.want {
t.Errorf("part2() = %v, want %v", got, tt.want)
}
})
}
}
}

func Benchmark_day7_part2(b *testing.B) {
for i := 0; i < b.N; i++ {
part2(example2)
}
}

0 comments on commit ed6cf86

Please sign in to comment.