Bootstrap and day 1 solution

This commit is contained in:
2022-06-06 15:14:31 -05:00
commit 662d76eb7c
13 changed files with 373 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.exe
__debug_bin
aoc2019
debug.test

8
.idea/.gitignore generated vendored Normal file
View File

@ -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

9
.idea/aoc2019.iml generated Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/aoc2019.iml" filepath="$PROJECT_DIR$/.idea/aoc2019.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

18
.vscode/launch.json vendored Normal file
View File

@ -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"]
}
]
}

47
days/01.go Normal file
View File

@ -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)
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module parnic.com/aoc2019
go 1.18

100
inputs/01p.txt Normal file
View File

@ -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

6
inputs/inputs.go Normal file
View File

@ -0,0 +1,6 @@
package inputs
import "embed"
//go:embed *
var Sets embed.FS

75
main.go Normal file
View File

@ -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)
}

44
utilities/colors.go Normal file
View File

@ -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"
)

45
utilities/parsing.go Normal file
View File

@ -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
}