10 次代码提交 dca93af4ed ... e6404f1428

作者 SHA1 备注 提交日期
  Piotr Czajkowski e6404f1428 Less is better 2 周之前
  Piotr Czajkowski e220686c46 Redundant 2 周之前
  Piotr Czajkowski 797ece23b7 DRY 2 周之前
  Piotr Czajkowski 323ce7086b Cleaner 2 周之前
  Piotr Czajkowski d3b2018860 Added description 2 周之前
  Piotr Czajkowski 2888b37dd5 Solved part2 2 周之前
  Piotr Czajkowski dab519929f Cleaner, but still wrong 2 周之前
  Piotr Czajkowski cb095db76d Solved for sample, clunky 2 周之前
  Piotr Czajkowski 575ddd3131 Solved part1 2 周之前
  Piotr Czajkowski bb3c2a8225 Able to read input 2 周之前
共有 3 个文件被更改,包括 152 次插入0 次删除
  1. 107 0
      03/code.go
  2. 45 0
      03/description.txt
  3. 0 0
      03/input

+ 107 - 0
03/code.go

@@ -0,0 +1,107 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+	"regexp"
+	"strings"
+)
+
+var mulRegex *regexp.Regexp = regexp.MustCompile(`mul\(\d+,\d+\)`)
+
+func getResults(line string) int {
+	var result int
+	matches := mulRegex.FindAllString(line, -1)
+	for _, match := range matches {
+		mul := make([]int, 2)
+		n, err := fmt.Sscanf(match, "mul(%d,%d)", &mul[0], &mul[1])
+		if n != 2 || err != nil {
+			log.Fatalf("Bad input: %s", err)
+		}
+
+		result += mul[0] * mul[1]
+	}
+
+	return result
+}
+
+func readInput(file *os.File) (int, []string) {
+	scanner := bufio.NewScanner(file)
+	var result int
+	var lines []string
+
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			break
+		}
+
+		lines = append(lines, line)
+		result += getResults(line)
+	}
+
+	return result, lines
+}
+
+func part1(muls [][]int) int {
+	var result int
+	for _, mul := range muls {
+		result += mul[0] * mul[1]
+	}
+
+	return result
+}
+
+func part2(lines []string) int {
+	var result int
+	multiply := true
+
+	for _, line := range lines {
+		var endIndex int
+		reading := true
+		for reading {
+			if multiply {
+				index := strings.Index(line, "don't()")
+				if index == -1 {
+					endIndex = len(line)
+					reading = false
+				} else {
+					multiply = false
+					endIndex = index
+				}
+
+				result += getResults(line[:endIndex])
+
+				line = line[endIndex:]
+			} else {
+				index := strings.Index(line, "do()")
+				if index == -1 {
+					reading = false
+				} else {
+					multiply = true
+					line = line[index:]
+				}
+			}
+		}
+	}
+
+	return result
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+	}
+
+	part1, lines := readInput(file)
+	fmt.Println("Part1:", part1)
+	fmt.Println("Part2:", part2(lines))
+}

+ 45 - 0
03/description.txt

@@ -0,0 +1,45 @@
+--- Day 3: Mull It Over ---
+
+"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The Historians head out to take a look.
+
+The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"
+
+The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the instructions have been jumbled up!
+
+It seems like the goal of the program is just to multiply some numbers. It does that with instructions like mul(X,Y), where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a result of 2024. Similarly, mul(123,4) would multiply 123 by 4.
+
+However, because the program's memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), or mul ( 2 , 4 ) do nothing.
+
+For example, consider the following section of corrupted memory:
+
+xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
+
+Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces 161 (2*4 + 5*5 + 11*8 + 8*5).
+
+Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
+
+Your puzzle answer was 161289189.
+--- Part Two ---
+
+As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result.
+
+There are two new instructions you'll need to handle:
+
+    The do() instruction enables future mul instructions.
+    The don't() instruction disables future mul instructions.
+
+Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled.
+
+For example:
+
+xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
+
+This corrupted memory is similar to the example from before, but this time the mul(5,5) and mul(11,8) instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction.
+
+This time, the sum of the results is 48 (2*4 + 8*5).
+
+Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications?
+
+Your puzzle answer was 83595109.
+
+Both parts of this puzzle are complete! They provide two gold stars: **

文件差异内容过多而无法显示
+ 0 - 0
03/input


部分文件因为文件数量过多而无法显示