-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Day 8 - Part 1 & 2 #4
base: main
Are you sure you want to change the base?
Conversation
@@ -9,6 +9,7 @@ import ( | |||
day04 "github.com/doniacld/adventofcode/puzzles/2020/04" | |||
day05 "github.com/doniacld/adventofcode/puzzles/2020/05" | |||
day06 "github.com/doniacld/adventofcode/puzzles/2020/06" | |||
day08 "github.com/doniacld/adventofcode/puzzles/2020/08" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened to day 7 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to assign you the code review...
if err != nil { | ||
return "", err | ||
} | ||
// store the initial ops before any modification by the Part one |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// store the initial ops before any modification by the Part one | |
// store the initial ops before any modification by Part one |
puzzles/2020/08/day08.go
Outdated
return "", err | ||
} | ||
// store the initial ops before any modification by the Part one | ||
tmp := ops.ResetOps() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe name it part2
rather than tmp
puzzles/2020/08/game/corrupted.go
Outdated
switch o.name { | ||
case nopOp: | ||
idx = ops.nop(idx) | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you silly Java girl... You don't need this in go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 thx
puzzles/2020/08/game/corrupted.go
Outdated
func (ops Operations) jump(idx int) int { | ||
ops[idx].seen = true | ||
if ops[idx].value < 0 { | ||
return idx - (abs(ops[idx].value) % len(ops)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if value is negative, you are computing the absolute of it, which is always - value
and substracting it, which means you are adding it. I therefore conslude that this line is exactly identical to line 50 and the entire if should be removed (with fire).
puzzles/2020/08/game/fixed.go
Outdated
return 0 | ||
} | ||
|
||
func (ops Operations) ResetOps() Operations { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reset or duplicate ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate !
puzzles/2020/08/game/fixed.go
Outdated
|
||
func (ops Operations) ResetOps() Operations { | ||
// store the tmp ops for modification | ||
tmpOps := make(Operations, len(ops)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not temporary if you are returning it. The use of newOps
or something would be clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
switch o.name { | ||
case nopOp: | ||
idx = ops.nops(i) | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mahaha...
puzzles/2020/08/game/fixed.go
Outdated
return false, accCounter | ||
} | ||
|
||
//idx not in the range anymore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
//idx not in the range anymore | |
// idx not in the range anymore |
puzzles/2020/08/game/fixed.go
Outdated
} | ||
|
||
// computeToTheLastOp calculates the accumulator counter depending on operations | ||
// exit false if the the operation is already seen or the index is not anymore in the range |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// exit false if the the operation is already seen or the index is not anymore in the range | |
// returns false if the operation is already seen or the index is not in the range anymore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
puzzles/2020/08/game/corrupted.go
Outdated
|
||
// ComputeCorruptedAcc computes the accumulator in the corrupted game | ||
// ends when an Operation is seen twice | ||
func (ops Operations) ComputeCorruptedAcc() (int, int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name output fields when they have the same type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
puzzles/2020/08/game/corrupted.go
Outdated
idx, accCounter := 0, 0 | ||
for true { | ||
o := ops[idx] | ||
// op already seen, game over |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather have this as the exit criterium of the infinite loop
for ; ! o.seen ; o = ops[idx] {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx !!
|
||
// nop for no Operation does nothing, go to the next instruction | ||
func (ops Operations) nop(idx int) int { | ||
ops[idx].seen = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a dangerous side effect that you're applying here that's based on how the language copies items.
Use
func (ops *Operations) nop(idx int) int {
to be more explicit about this func. Same goes for all the funcs that affect the contents of ops
.
// nop for no Operation does nothing, go to the next instruction | ||
func (ops Operations) nop(idx int) int { | ||
ops[idx].seen = true | ||
return (idx + 1) % len(ops) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you ever check that idx is > 0 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's kind of done in fixed.go:81
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@floppyzedolfin Should I merge the duplicated operations ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you can avoid having duplicated code, you should do what it takes
puzzles/2020/08/game/fixed.go
Outdated
func (ops Operations) computeToTheLastOp() (bool, int) { | ||
i, accCounter := 0, 0 | ||
|
||
for true { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
infinite loops deserve a comment on why we should expect them not to run forever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed it !
puzzles/2020/08/game/fixed.go
Outdated
|
||
for true { | ||
o := ops[i] | ||
idx := 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nextIndex
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
puzzles/2020/08/game/fixed.go
Outdated
// computeToTheLastOp calculates the accumulator counter depending on operations | ||
// exit false if the the operation is already seen or the index is not anymore in the range | ||
func (ops Operations) computeToTheLastOp() (bool, int) { | ||
i, accCounter := 0, 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currentIndex
?
// game ends at the last instruction | ||
// a jmp is replaced by a nop or the other way around | ||
func (ops Operations) ComputeFixedAcc() int { | ||
for i := 0; i < len(ops)-1; i++ { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or for _, op := range ops {
return accCounter | ||
} | ||
} | ||
// should not happened |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// should not happened | |
// should not happen |
currentIdx, nextIdx, accCounter := 0, 0, 0 | ||
|
||
o := ops[currentIdx] | ||
for ; nextIdx != len(ops)-1; o = ops[currentIdx] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
givin line 55, I think it makes a lot more sense to check currentIndex
No description provided.