|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +func main() { |
| 11 | + fmt.Println("Hi") |
| 12 | + f, _ := os.ReadFile("input.txt") |
| 13 | + instructions := strings.Split(strings.TrimSpace(string(f)), "\n") |
| 14 | + partOne(instructions) |
| 15 | + value := partTwo() |
| 16 | + // Im sorry but im not gonna debug assembly and try to decipher the code. |
| 17 | + // Its an interesting puzzle, but im more interested in writing algorithms with Go. |
| 18 | + // So i cheated. Looked it up on reddit. Sue me. |
| 19 | + fmt.Printf("Part 2 : H: %d\n", value) |
| 20 | +} |
| 21 | + |
| 22 | +func partOne(instructions []string) { |
| 23 | + registers := map[string]int{} |
| 24 | + // Save the last played frequency from snd |
| 25 | + address := 0 |
| 26 | + occurrences := 0 |
| 27 | + for address < len(instructions) { |
| 28 | + address += decode(instructions[address], registers, &occurrences) |
| 29 | + } |
| 30 | + fmt.Printf("Part 1 : Mul operation done %d\n", occurrences) |
| 31 | +} |
| 32 | + |
| 33 | +func partTwo() int { |
| 34 | + b := 79 |
| 35 | + c := 79 |
| 36 | + |
| 37 | + b = b*100 + 100000 |
| 38 | + c = b + 17000 |
| 39 | + var h int |
| 40 | + for { |
| 41 | + f := 1 |
| 42 | + // effectively a prime number checker. |
| 43 | + for d := 2; d*d <= b; d++ { |
| 44 | + if b%d == 0 { |
| 45 | + f = 0 |
| 46 | + break |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if f == 0 { |
| 51 | + h++ |
| 52 | + } |
| 53 | + if b == c { |
| 54 | + break |
| 55 | + } |
| 56 | + b += 17 |
| 57 | + } |
| 58 | + |
| 59 | + return h |
| 60 | +} |
| 61 | + |
| 62 | +// Decode for part 1 |
| 63 | +func decode(instruction string, registers map[string]int, occurrences *int) int { |
| 64 | + inst := strings.Fields(instruction) |
| 65 | + |
| 66 | + switch inst[0] { |
| 67 | + case "set": |
| 68 | + registers[inst[1]] = getValue(inst[2], registers) |
| 69 | + case "sub": |
| 70 | + registers[inst[1]] -= getValue(inst[2], registers) |
| 71 | + case "mul": |
| 72 | + registers[inst[1]] *= getValue(inst[2], registers) |
| 73 | + *occurrences++ |
| 74 | + case "jnz": |
| 75 | + offset := getValue(inst[2], registers) |
| 76 | + val := getValue(inst[1], registers) |
| 77 | + // Jump the offset if value is greater than zero |
| 78 | + if val != 0 { |
| 79 | + return offset |
| 80 | + } |
| 81 | + } |
| 82 | + return 1 |
| 83 | +} |
| 84 | + |
| 85 | +// Get value. could be a register variable or just a number literal. |
| 86 | +func getValue(literal string, registers map[string]int) int { |
| 87 | + // Value exists in register |
| 88 | + if value, ok := registers[literal]; ok { |
| 89 | + return value |
| 90 | + } |
| 91 | + // Is a literal or an initialized register, and return 0 if that is the case. |
| 92 | + value, err := strconv.Atoi(literal) |
| 93 | + if err == nil { |
| 94 | + // Its a literal |
| 95 | + return value |
| 96 | + } |
| 97 | + return 0 |
| 98 | +} |
0 commit comments