-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/kfarnung/advent-of-code/2024/lib" | ||
) | ||
|
||
type constraint struct { | ||
first int64 | ||
second int64 | ||
} | ||
|
||
func part1(input string) int64 { | ||
constraints, err := parseConstraints(input) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
pageLists, err := parsePageList(input) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sum := int64(0) | ||
for _, pages := range pageLists { | ||
meetsAllConstraints := true | ||
for _, constraint := range constraints { | ||
firstIndex := slices.Index(pages, constraint.first) | ||
secondIndex := slices.Index(pages, constraint.second) | ||
|
||
if firstIndex == -1 || secondIndex == -1 { | ||
continue | ||
} | ||
|
||
if secondIndex < firstIndex { | ||
meetsAllConstraints = false | ||
break | ||
} | ||
} | ||
|
||
if meetsAllConstraints { | ||
sum += pages[len(pages)/2] | ||
} | ||
} | ||
|
||
return sum | ||
} | ||
|
||
func part2(input string) int64 { | ||
constraints, err := parseConstraints(input) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
pageLists, err := parsePageList(input) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sum := int64(0) | ||
for _, pages := range pageLists { | ||
meetsAllConstraints := true | ||
didSwap := true | ||
for didSwap { | ||
didSwap = false | ||
for _, constraint := range constraints { | ||
firstIndex := slices.Index(pages, constraint.first) | ||
secondIndex := slices.Index(pages, constraint.second) | ||
|
||
if firstIndex == -1 || secondIndex == -1 { | ||
continue | ||
} | ||
|
||
if secondIndex < firstIndex { | ||
meetsAllConstraints = false | ||
|
||
temp := pages[firstIndex] | ||
pages[firstIndex] = pages[secondIndex] | ||
pages[secondIndex] = temp | ||
|
||
didSwap = true | ||
} | ||
} | ||
} | ||
|
||
if !meetsAllConstraints { | ||
sum += pages[len(pages)/2] | ||
} | ||
} | ||
|
||
return sum | ||
} | ||
|
||
func parseConstraints(input string) ([]constraint, error) { | ||
var constraints []constraint | ||
for _, line := range lib.SplitLines(input) { | ||
if line == "" { | ||
break | ||
} | ||
|
||
split := strings.Split(line, "|") | ||
first, err := lib.ParseInt[int64](split[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
second, err := lib.ParseInt[int64](split[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
constraints = append(constraints, constraint{first, second}) | ||
} | ||
|
||
return constraints, nil | ||
} | ||
|
||
func parsePageList(input string) ([][]int64, error) { | ||
var pages [][]int64 | ||
foundBreak := false | ||
for _, line := range lib.SplitLines(input) { | ||
if line == "" { | ||
foundBreak = true | ||
continue | ||
} else if !foundBreak { | ||
continue | ||
} | ||
|
||
split := strings.Split(line, ",") | ||
var page []int64 | ||
for _, s := range split { | ||
val, err := lib.ParseInt[int64](s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
page = append(page, val) | ||
} | ||
|
||
pages = append(pages, page) | ||
} | ||
|
||
return pages, nil | ||
} | ||
|
||
func main() { | ||
name := os.Args[1] | ||
content, err := lib.LoadFileContent(name) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("Part 1: %d\n", part1(content)) | ||
fmt.Printf("Part 2: %d\n", part2(content)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/kfarnung/advent-of-code/2024/lib" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var input = strings.Join([]string{ | ||
"47|53", | ||
"97|13", | ||
"97|61", | ||
"97|47", | ||
"75|29", | ||
"61|13", | ||
"75|53", | ||
"29|13", | ||
"97|29", | ||
"53|29", | ||
"61|53", | ||
"97|53", | ||
"61|29", | ||
"47|13", | ||
"75|47", | ||
"97|75", | ||
"47|61", | ||
"75|61", | ||
"47|29", | ||
"75|13", | ||
"53|13", | ||
"", | ||
"75,47,61,53,29", | ||
"97,61,53,29,13", | ||
"75,29,13", | ||
"75,97,47,61,53", | ||
"61,13,29", | ||
"97,13,75,29,47", | ||
"", | ||
}, "\n") | ||
|
||
func TestPart1(t *testing.T) { | ||
assert.Equal(t, int64(143), part1(input)) | ||
|
||
inputContent, err := lib.GetInputContent() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, int64(6041), part1(inputContent)) | ||
} | ||
|
||
func TestPart2(t *testing.T) { | ||
assert.Equal(t, int64(123), part2(input)) | ||
|
||
inputContent, err := lib.GetInputContent() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
assert.Equal(t, int64(4884), part2(inputContent)) | ||
} |
Submodule private
updated
from 8bf14c to 46d333