generated from devries/aoc_template
-
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
6 changed files
with
230 additions
and
2 deletions.
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
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,50 @@ | ||
package day08p1 | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
// Get all LR moves | ||
moves := []rune(lines[0]) | ||
|
||
neighbors := make(map[string][]string) | ||
for i := 2; i < len(lines); i++ { | ||
parts := strings.Split(lines[i], " = ") | ||
|
||
destinations := strings.Trim(parts[1], "()") | ||
|
||
leftright := strings.Split(destinations, ", ") | ||
|
||
neighbors[parts[0]] = leftright | ||
} | ||
|
||
next := neighbors["AAA"] | ||
steps := 0 | ||
|
||
outer: | ||
for { | ||
for _, d := range moves { | ||
steps++ | ||
switch d { | ||
case 'L': | ||
if next[0] == "ZZZ" { | ||
break outer | ||
} | ||
next = neighbors[next[0]] | ||
case 'R': | ||
if next[1] == "ZZZ" { | ||
break outer | ||
} | ||
next = neighbors[next[1]] | ||
} | ||
} | ||
} | ||
|
||
return steps | ||
} |
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,48 @@ | ||
package day08p1 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `RL | ||
AAA = (BBB, CCC) | ||
BBB = (DDD, EEE) | ||
CCC = (ZZZ, GGG) | ||
DDD = (DDD, DDD) | ||
EEE = (EEE, EEE) | ||
GGG = (GGG, GGG) | ||
ZZZ = (ZZZ, ZZZ)` | ||
|
||
var testInput2 = `LLR | ||
AAA = (BBB, BBB) | ||
BBB = (AAA, ZZZ) | ||
ZZZ = (ZZZ, ZZZ)` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer int | ||
}{ | ||
{testInput, 2}, | ||
{testInput2, 6}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(int) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
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,75 @@ | ||
package day08p2 | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
// Get all LR moves | ||
moves := []rune(lines[0]) | ||
|
||
neighbors := make(map[string][]string) | ||
for i := 2; i < len(lines); i++ { | ||
parts := strings.Split(lines[i], " = ") | ||
|
||
destinations := strings.Trim(parts[1], "()") | ||
|
||
leftright := strings.Split(destinations, ", ") | ||
|
||
neighbors[parts[0]] = leftright | ||
} | ||
|
||
cyclesteps := int64(1) | ||
|
||
// Let's find the ones ending in A | ||
for pos := range neighbors { | ||
if !strings.HasSuffix(pos, "A") { | ||
continue | ||
} | ||
var steps int64 | ||
|
||
hits := []Hit{} | ||
|
||
outer: | ||
for { | ||
for i, d := range moves { | ||
next := neighbors[pos] | ||
steps++ | ||
switch d { | ||
case 'L': | ||
pos = next[0] | ||
case 'R': | ||
pos = next[1] | ||
} | ||
|
||
if strings.HasSuffix(pos, "Z") { | ||
h := Hit{pos, i, steps} | ||
for _, ph := range hits { | ||
if h.Candidate == ph.Candidate { | ||
// An examination shows that the interval between hits appears to be the same as the | ||
// time from start to first hit. This is slightly contrived, but makes the solution easier. | ||
// This is not a general solution, but a solution that works for this and I suspect all | ||
// advent of code inputs for this day. | ||
cyclesteps = utils.Lcm(cyclesteps, h.Steps-ph.Steps) | ||
break outer | ||
} | ||
} | ||
hits = append(hits, h) | ||
} | ||
} | ||
} | ||
} | ||
return cyclesteps | ||
} | ||
|
||
// State when destination candidate is hit | ||
type Hit struct { | ||
Candidate string // Ends with Z | ||
Cycle int // i value when hit | ||
Steps int64 // step at which it was hit | ||
} |
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,42 @@ | ||
package day08p2 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `LR | ||
11A = (11B, XXX) | ||
11B = (XXX, 11Z) | ||
11Z = (11B, XXX) | ||
22A = (22B, XXX) | ||
22B = (22C, 22C) | ||
22C = (22Z, 22Z) | ||
22Z = (22B, 22B) | ||
XXX = (XXX, XXX)` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer int64 | ||
}{ | ||
{testInput, 6}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(int64) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
Submodule inputs
updated
from c7f38c to e98b86