commit 662d76eb7c8d742071300bd13be08799ef5745da Author: Parnic Date: Mon Jun 6 15:14:31 2022 -0500 Bootstrap and day 1 solution diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e381e30 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.exe +__debug_bin +aoc2019 +debug.test diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/aoc2019.iml b/.idea/aoc2019.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/aoc2019.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..bee318a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3b14c9e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "buildFlags": "", + "mode": "auto", + "program": "${workspaceFolder}", + "env": {}, + "args": ["1"] + } + ] +} \ No newline at end of file diff --git a/days/01.go b/days/01.go new file mode 100644 index 0000000..3715eb2 --- /dev/null +++ b/days/01.go @@ -0,0 +1,47 @@ +package days + +import ( + "fmt" + "math" + "time" + + "parnic.com/aoc2019/utilities" +) + +type Day01 struct { + nums []int64 +} + +func (d *Day01) Parse() { + d.nums = utilities.GetIntLines("01p") + time.Sleep(time.Microsecond * 500) +} + +func (d *Day01) calcFuel(mass int64) int64 { + return int64(math.Floor(float64(mass)/3)) - 2 +} + +func (d *Day01) Part1() string { + var totalFuel int64 + for _, mass := range d.nums { + fuel := d.calcFuel(mass) + totalFuel += fuel + } + + return fmt.Sprintf("Fuel required: %s%d%s", utilities.TextBold, totalFuel, utilities.TextReset) +} + +func (d *Day01) Part2() string { + var totalFuel int64 + for _, mass := range d.nums { + for mass > 0 { + fuel := d.calcFuel(mass) + if fuel > 0 { + totalFuel += fuel + } + mass = fuel + } + } + + return fmt.Sprintf("Fuel required: %s%d%s", utilities.TextBold, totalFuel, utilities.TextReset) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b715a41 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module parnic.com/aoc2019 + +go 1.18 diff --git a/inputs/01p.txt b/inputs/01p.txt new file mode 100644 index 0000000..5658967 --- /dev/null +++ b/inputs/01p.txt @@ -0,0 +1,100 @@ +85644 +52584 +72349 +83834 +56593 +108492 +94585 +97733 +62732 +103113 +133259 +132647 +52460 +51299 +115749 +121047 +69451 +54737 +62738 +116686 +57293 +97273 +128287 +139440 +97583 +130263 +79307 +118198 +82514 +70679 +64485 +119346 +136281 +114724 +73580 +76314 +126198 +97635 +114655 +104195 +99469 +70251 +82815 +79531 +58135 +80625 +73106 +139806 +138478 +136605 +111472 +149915 +95928 +126905 +70496 +147999 +148501 +114025 +75716 +113473 +95390 +104466 +138715 +53053 +79502 +98601 +115139 +122315 +88402 +124332 +140107 +50912 +104885 +142005 +145938 +118556 +101858 +51142 +94100 +99421 +84544 +137234 +126374 +107333 +82439 +125373 +51212 +99358 +82821 +89913 +67513 +136907 +133707 +139988 +96914 +130672 +66474 +120729 +50131 +67475 \ No newline at end of file diff --git a/inputs/inputs.go b/inputs/inputs.go new file mode 100644 index 0000000..a00b29c --- /dev/null +++ b/inputs/inputs.go @@ -0,0 +1,6 @@ +package inputs + +import "embed" + +//go:embed * +var Sets embed.FS diff --git a/main.go b/main.go new file mode 100644 index 0000000..bdc2169 --- /dev/null +++ b/main.go @@ -0,0 +1,75 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" + "strings" + "time" + + "parnic.com/aoc2019/days" + "parnic.com/aoc2019/utilities" +) + +type day interface { + Parse() + Part1() string + Part2() string +} + +var dayMap = map[int]day{ + 1: &days.Day01{}, +} + +func main() { + arg := "1" + if len(os.Args) > 1 { + arg = os.Args[1] + } + if strings.ToLower(arg) == "all" { + for _, v := range dayMap { + solve(v) + } + } else { + iArg, err := strconv.Atoi(arg) + if err != nil { + log.Fatalf("Invalid day " + utilities.ColorCyan + arg + utilities.TextReset) + } + + p, ok := dayMap[iArg] + if !ok { + log.Fatalf("Unknown day " + utilities.ColorCyan + arg + utilities.TextReset) + } + + solve(p) + } + + os.Exit(0) +} + +func solve(d day) { + fmt.Println(utilities.ColorCyan + "Day 01" + utilities.TextReset) + + parseStart := time.Now() + d.Parse() + parseTime := time.Since(parseStart) + + part1Start := time.Now() + part1 := d.Part1() + part1Time := time.Since(part1Start) + + part2Start := time.Now() + part2 := d.Part2() + part2Time := time.Since(part2Start) + + fmt.Println(utilities.ColorGreen + "Part1:" + utilities.TextReset) + fmt.Println(part1) + fmt.Println(utilities.ColorGreen + "Part2:" + utilities.TextReset) + fmt.Println(part2) + fmt.Print(utilities.ColorBrightBlack) + fmt.Printf("Parsed in %s\n", parseTime) + fmt.Printf("Part01 in %s\n", part1Time) + fmt.Printf("Part02 in %s\n", part2Time) + fmt.Println(utilities.TextReset) +} diff --git a/utilities/colors.go b/utilities/colors.go new file mode 100644 index 0000000..1027211 --- /dev/null +++ b/utilities/colors.go @@ -0,0 +1,44 @@ +package utilities + +const ( + ColorBlack = "\u001b[30m" + ColorRed = "\u001b[31m" + ColorGreen = "\u001b[32m" + ColorYellow = "\u001b[33m" + ColorBlue = "\u001b[34m" + ColorMagenta = "\u001b[35m" + ColorCyan = "\u001b[36m" + ColorWhite = "\u001b[37m" + + ColorBrightBlack = "\u001b[30;1m" + ColorBrightRed = "\u001b[31;1m" + ColorBrightGreen = "\u001b[32;1m" + ColorBrightYellow = "\u001b[33;1m" + ColorBrightBlue = "\u001b[34;1m" + ColorBrightMagenta = "\u001b[35;1m" + ColorBrightCyan = "\u001b[36;1m" + ColorBrightWhite = "\u001b[37;1m" + + BackgroundBlack = "\u001b[40m" + BackgroundRed = "\u001b[41m" + BackgroundGreen = "\u001b[42m" + BackgroundYellow = "\u001b[43m" + BackgroundBlue = "\u001b[44m" + BackgroundMagenta = "\u001b[45m" + BackgroundCyan = "\u001b[46m" + BackgroundWhite = "\u001b[47m" + + BackgroundBrightBlack = "\u001b[40;1m" + BackgroundBrightRed = "\u001b[41;1m" + BackgroundBrightGreen = "\u001b[42;1m" + BackgroundBrightYellow = "\u001b[43;1m" + BackgroundBrightBlue = "\u001b[44;1m" + BackgroundBrightMagenta = "\u001b[45;1m" + BackgroundBrightCyan = "\u001b[46;1m" + BackgroundBrightWhite = "\u001b[47;1m" + + TextReset = "\u001b[0m" + TextBold = "\u001b[1m" + TextUnderline = "\u001b[4m" + TextReverse = "\u001b[7m" +) diff --git a/utilities/parsing.go b/utilities/parsing.go new file mode 100644 index 0000000..c7ae466 --- /dev/null +++ b/utilities/parsing.go @@ -0,0 +1,45 @@ +package utilities + +import ( + "bufio" + "fmt" + "strconv" + + "parnic.com/aoc2019/inputs" +) + +func getData(filename string, lineHandler func(line string)) { + file, err := inputs.Sets.Open(fmt.Sprintf("%s.txt", filename)) + // version that doesn't use embedded files: + // file, err := os.Open(fmt.Sprintf("inputs/%s.txt", filename)) + if err != nil { + panic(err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + lineHandler(scanner.Text()) + } +} + +func GetStringLines(filename string) []string { + retval := make([]string, 0) + getData(filename, func(line string) { + retval = append(retval, line) + }) + return retval +} + +func GetIntLines(filename string) []int64 { + retval := make([]int64, 0) + getData(filename, func(line string) { + val, err := strconv.ParseInt(line, 10, 64) + if err != nil { + panic(err) + } + retval = append(retval, val) + }) + return retval +}