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