Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Day 8 - Part 1 & 2 #4

wants to merge 2 commits into from

Conversation

doniacld
Copy link
Owner

No description provided.

@@ -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"
Copy link
Collaborator

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 ?

Copy link
Owner Author

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// store the initial ops before any modification by the Part one
// store the initial ops before any modification by Part one

return "", err
}
// store the initial ops before any modification by the Part one
tmp := ops.ResetOps()
Copy link
Collaborator

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

switch o.name {
case nopOp:
idx = ops.nop(idx)
break
Copy link
Collaborator

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 thx

func (ops Operations) jump(idx int) int {
ops[idx].seen = true
if ops[idx].value < 0 {
return idx - (abs(ops[idx].value) % len(ops))
Copy link
Collaborator

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).

return 0
}

func (ops Operations) ResetOps() Operations {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset or duplicate ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate !


func (ops Operations) ResetOps() Operations {
// store the tmp ops for modification
tmpOps := make(Operations, len(ops))
Copy link
Collaborator

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.

Copy link
Owner Author

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mahaha...

return false, accCounter
}

//idx not in the range anymore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//idx not in the range anymore
// idx not in the range anymore

}

// 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


// ComputeCorruptedAcc computes the accumulator in the corrupted game
// ends when an Operation is seen twice
func (ops Operations) ComputeCorruptedAcc() (int, int) {
Copy link
Collaborator

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

idx, accCounter := 0, 0
for true {
o := ops[idx]
// op already seen, game over
Copy link
Collaborator

@floppyzedolfin floppyzedolfin Jul 14, 2021

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] {

Copy link
Owner Author

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
Copy link
Collaborator

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)
Copy link
Collaborator

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 ?

Copy link
Collaborator

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

Copy link
Owner Author

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 ?

Copy link
Collaborator

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

func (ops Operations) computeToTheLastOp() (bool, int) {
i, accCounter := 0, 0

for true {
Copy link
Collaborator

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

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed it !


for true {
o := ops[i]
idx := 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nextIndex ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

// 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
Copy link
Collaborator

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++ {
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// should not happened
// should not happen

currentIdx, nextIdx, accCounter := 0, 0, 0

o := ops[currentIdx]
for ; nextIdx != len(ops)-1; o = ops[currentIdx] {
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants