updates
This commit is contained in:
@@ -20,46 +20,45 @@ type Instruction struct {
|
|||||||
Steps int
|
Steps int
|
||||||
}
|
}
|
||||||
|
|
||||||
func Part1(input string) int {
|
func parseInput(input string) []Instruction {
|
||||||
lines := strings.Split(input, "\n")
|
|
||||||
position := 50
|
|
||||||
code := 0
|
|
||||||
Instructions := make([]Instruction, 0)
|
Instructions := make([]Instruction, 0)
|
||||||
|
lines := strings.Split(input, "\n")
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
var steps int
|
var steps int
|
||||||
var dir Dir
|
var dir Dir
|
||||||
|
steps = utils.MustAtoi(line[1:])
|
||||||
switch line[0] {
|
switch line[0] {
|
||||||
case 'L':
|
case 'L':
|
||||||
steps = utils.MustAtoi(line[1:])
|
|
||||||
dir = Left
|
dir = Left
|
||||||
case 'R':
|
case 'R':
|
||||||
steps = utils.MustAtoi(line[1:])
|
|
||||||
dir = Right
|
dir = Right
|
||||||
default:
|
default:
|
||||||
fmt.Println("Invalid direction")
|
fmt.Println("Invalid direction")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if steps > 100 {
|
|
||||||
steps = steps % 100
|
|
||||||
}
|
|
||||||
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
|
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
|
||||||
}
|
}
|
||||||
fmt.Print(Instructions)
|
return Instructions
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part1(input string) int {
|
||||||
|
|
||||||
|
Instructions := parseInput(input)
|
||||||
|
position := 50
|
||||||
|
code := 0
|
||||||
|
|
||||||
for _, instr := range Instructions {
|
for _, instr := range Instructions {
|
||||||
if instr.Turn == Left {
|
switch instr.Turn {
|
||||||
if position-instr.Steps < 0 {
|
case Left:
|
||||||
position = 100 + (position - instr.Steps)
|
position -= instr.Steps % 100
|
||||||
} else {
|
case Right:
|
||||||
position -= instr.Steps
|
position += instr.Steps % 100
|
||||||
}
|
}
|
||||||
} else if instr.Turn == Right {
|
if position < 0 {
|
||||||
if position+instr.Steps >= 100 {
|
position += 100
|
||||||
position = (position + instr.Steps) - 100
|
} else if position > 99 {
|
||||||
} else {
|
position -= 100
|
||||||
position += instr.Steps
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fmt.Println("\n", instr.Turn, instr.Steps, "->", position)
|
|
||||||
if position == 0 {
|
if position == 0 {
|
||||||
code++
|
code++
|
||||||
}
|
}
|
||||||
@@ -68,61 +67,34 @@ func Part1(input string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Part2(input string) int {
|
func Part2(input string) int {
|
||||||
lines := strings.Split(input, "\n")
|
Instructions := parseInput(input)
|
||||||
position := 50
|
position := 50
|
||||||
code := 0
|
code := 0
|
||||||
Instructions := make([]Instruction, 0)
|
new_position := position
|
||||||
for _, line := range lines {
|
|
||||||
var steps int
|
|
||||||
var dir Dir
|
|
||||||
switch line[0] {
|
|
||||||
case 'L':
|
|
||||||
steps = utils.MustAtoi(line[1:])
|
|
||||||
dir = Left
|
|
||||||
case 'R':
|
|
||||||
steps = utils.MustAtoi(line[1:])
|
|
||||||
dir = Right
|
|
||||||
default:
|
|
||||||
fmt.Println("Invalid direction")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
Instructions = append(Instructions, Instruction{Turn: dir, Steps: steps})
|
|
||||||
}
|
|
||||||
fmt.Print(Instructions)
|
|
||||||
for _, instr := range Instructions {
|
for _, instr := range Instructions {
|
||||||
if instr.Steps > 100 {
|
switch instr.Turn {
|
||||||
code = code + (instr.Steps / 100)
|
case Left:
|
||||||
instr.Steps = instr.Steps % 100
|
new_position = position - instr.Steps%100
|
||||||
|
case Right:
|
||||||
|
new_position = position + instr.Steps%100
|
||||||
}
|
}
|
||||||
if instr.Turn == Left {
|
code += instr.Steps / 100
|
||||||
if position-instr.Steps < 0 {
|
if new_position == 0 {
|
||||||
if position != 0 {
|
code++
|
||||||
code++
|
}
|
||||||
}
|
if new_position < 0 {
|
||||||
position = 100 + (position - instr.Steps)
|
new_position += 100
|
||||||
|
if position != 0 {
|
||||||
} else {
|
code++
|
||||||
position -= instr.Steps
|
|
||||||
if position == 0 {
|
|
||||||
code++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if instr.Turn == Right {
|
} else if new_position > 99 {
|
||||||
if position+instr.Steps > 99 {
|
new_position -= 100
|
||||||
if position != 0 {
|
if position != 0 {
|
||||||
code++
|
code++
|
||||||
}
|
|
||||||
position = (position + instr.Steps) - 100
|
|
||||||
} else {
|
|
||||||
position += instr.Steps
|
|
||||||
if position == 0 {
|
|
||||||
code++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
position = new_position
|
||||||
fmt.Println("\n", instr.Turn, instr.Steps, "->", position, "code:", code)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return code
|
return code
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user