mirror of
https://github.com/RussellChamp/advent-of-code-2021.git
synced 2026-10-27 21:52:32 +00:00
first commit
This commit is contained in:
1 parent
4c88777d7a
commit
1208efd0af
8 files changed
+342
No files matched your search
@@ -13,3 +13,6 @@
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Other stuff
|
||||
*.log
|
||||
@@ -1,2 +1,8 @@
|
||||
# advent-of-code-2021
|
||||
Advent of Code 2021
|
||||
|
||||
# Usage
|
||||
run `make #` to execute the solution for a given day
|
||||
|
||||
# Known Issues
|
||||
The VSCode integrated debugger isn't working right with WSL and GO :<
|
||||
@@ -0,0 +1,2 @@
|
||||
this is some data in a txt file
|
||||
in can have more than one line
|
||||
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func tryMe(e error) {
|
||||
if e != nil {
|
||||
fmt.Println("OH SNAP!")
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func files() {
|
||||
dat, err := os.ReadFile("./data.txt")
|
||||
tryMe(err)
|
||||
|
||||
fmt.Print(string(dat))
|
||||
fmt.Println("\n---")
|
||||
|
||||
f, err := os.Open("./data.txt")
|
||||
tryMe(err)
|
||||
coupleBytes := make([]byte, 10)
|
||||
num, err := f.Read(coupleBytes)
|
||||
tryMe(err)
|
||||
fmt.Println(num, string(coupleBytes))
|
||||
|
||||
reader := bufio.NewReader(f)
|
||||
rBuf, err := reader.Peek(5)
|
||||
tryMe(err)
|
||||
fmt.Println("buff reader", string(rBuf))
|
||||
|
||||
f.Close()
|
||||
}
|
||||
+274
@@ -0,0 +1,274 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"rsc.io/quote"
|
||||
)
|
||||
|
||||
const Answer = 42
|
||||
|
||||
func main() {
|
||||
files()
|
||||
// runStuff()
|
||||
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func runStuff() {
|
||||
fmt.Println("Hello world")
|
||||
fmt.Println(quote.Go())
|
||||
|
||||
mathFn()
|
||||
|
||||
fmt.Println("add stuff", sumStuff(2, 3))
|
||||
|
||||
a, b := doubleTrouble()
|
||||
fmt.Println(a, b)
|
||||
|
||||
declTypes()
|
||||
basicTypes()
|
||||
|
||||
fmt.Println("The answer is", Answer)
|
||||
|
||||
bitShifting()
|
||||
|
||||
loops()
|
||||
|
||||
conditions()
|
||||
|
||||
switching()
|
||||
|
||||
deferFn()
|
||||
|
||||
pointies()
|
||||
|
||||
buildingThings()
|
||||
|
||||
lists()
|
||||
|
||||
makingThings()
|
||||
|
||||
mapping()
|
||||
|
||||
funcFun()
|
||||
|
||||
methods()
|
||||
}
|
||||
|
||||
func mathFn() {
|
||||
fmt.Println("math")
|
||||
fmt.Println(math.Sin(2))
|
||||
|
||||
fmt.Println("random numbers")
|
||||
fmt.Println(rand.Intn(100), rand.ExpFloat64())
|
||||
}
|
||||
|
||||
func sumStuff(x int, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
func doubleTrouble() (string, string) {
|
||||
return "foo", "bar"
|
||||
}
|
||||
|
||||
func declTypes() {
|
||||
var i, j, k int = 1, 2, 3
|
||||
o := 42
|
||||
foo, bar := "zap", "zing"
|
||||
fmt.Println(i, j, k, o, foo, bar)
|
||||
}
|
||||
|
||||
func basicTypes() {
|
||||
var b bool = true
|
||||
var s string = "string"
|
||||
var i int = 99
|
||||
// int8,16,32,64
|
||||
// uint8,16,32,64,uintptr
|
||||
// byte, rune
|
||||
var f float32 = 1.23
|
||||
var c complex64 = complex64(123)
|
||||
|
||||
fmt.Println(b, s, i, f, c)
|
||||
}
|
||||
|
||||
func bitShifting() {
|
||||
val := 1 << 4
|
||||
val2 := val >> 2
|
||||
fmt.Println("bits shifting", val, val2)
|
||||
}
|
||||
|
||||
func loops() {
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Print(1<<i, ",")
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
var fibs = []int{1, 2, 3, 5, 8, 13, 21}
|
||||
for idx, val := range fibs {
|
||||
fmt.Print(idx, ":", val, ",")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
func conditions() {
|
||||
if Answer == 42 {
|
||||
fmt.Println("sorry for the inconvenience")
|
||||
}
|
||||
if v := Answer << 2; v > 100 {
|
||||
fmt.Println("that's a better answer", v)
|
||||
}
|
||||
if true == false {
|
||||
fmt.Println("i feel rather like a sofa")
|
||||
} else {
|
||||
fmt.Println("stability of the universe confirmed")
|
||||
}
|
||||
}
|
||||
|
||||
func switching() {
|
||||
val := "back"
|
||||
|
||||
switch val {
|
||||
case "blade":
|
||||
fmt.Println("sharp!")
|
||||
case "back":
|
||||
fmt.Println("snazzy!")
|
||||
default:
|
||||
fmt.Println("dunno!")
|
||||
}
|
||||
|
||||
today := time.Now().Weekday()
|
||||
switch today {
|
||||
case time.Saturday:
|
||||
fmt.Println("party time")
|
||||
case time.Saturday - 1:
|
||||
fmt.Println("TGIF")
|
||||
default:
|
||||
fmt.Println("another time")
|
||||
}
|
||||
|
||||
switch {
|
||||
case true == false:
|
||||
fmt.Println("never")
|
||||
case Answer > 1:
|
||||
fmt.Println("the cool kids way to write if/else blocks")
|
||||
default:
|
||||
fmt.Println("we fell out")
|
||||
}
|
||||
}
|
||||
|
||||
func deferFn() {
|
||||
defer fmt.Println("I came first!")
|
||||
fmt.Println("But I was executed first!")
|
||||
defer fmt.Println("And I was last!")
|
||||
}
|
||||
|
||||
func pointies() {
|
||||
var p *int
|
||||
badNum := 666
|
||||
p = &badNum
|
||||
fmt.Println("bad num", *p, p)
|
||||
}
|
||||
|
||||
func buildingThings() {
|
||||
type Aminal struct {
|
||||
color string
|
||||
age int
|
||||
flavor bool
|
||||
}
|
||||
c := Aminal{"blue", 2, false}
|
||||
fmt.Println(c)
|
||||
|
||||
p := &c
|
||||
p.color = "black"
|
||||
fmt.Println(*p)
|
||||
|
||||
d := &Aminal{age: 17}
|
||||
fmt.Println(d, *d)
|
||||
}
|
||||
|
||||
func lists() {
|
||||
var a [10]string
|
||||
a[0] = "Hello"
|
||||
a[6] = "World"
|
||||
bestPart := a[0:7]
|
||||
|
||||
evens := [5]int{2, 4, 6, 8, 10}
|
||||
|
||||
fmt.Println(bestPart, evens[1:3], evens[3:])
|
||||
fmt.Println("best", len(bestPart), cap(bestPart))
|
||||
|
||||
slice := []struct {
|
||||
label string
|
||||
value int
|
||||
}{
|
||||
{"foo", 1},
|
||||
{"bar", 2},
|
||||
{"baz", 3},
|
||||
}
|
||||
fmt.Println("slices", slice)
|
||||
|
||||
pie := evens[0:]
|
||||
pie2 := append(pie, 12)
|
||||
fmt.Println("pie?", pie, pie2)
|
||||
}
|
||||
|
||||
func makingThings() {
|
||||
a := make([]int, 5)
|
||||
b := make([]int, 0, 10)
|
||||
|
||||
fmt.Println("make", a, b)
|
||||
}
|
||||
|
||||
func mapping() {
|
||||
type RGB struct {
|
||||
r int
|
||||
g int
|
||||
b int
|
||||
}
|
||||
colorMap := make(map[string]RGB)
|
||||
colorMap["red"] = RGB{255, 0, 0}
|
||||
colorMap["puce"] = RGB{204, 136, 153}
|
||||
|
||||
fmt.Println(colorMap)
|
||||
|
||||
var favoriteShades = map[string]RGB{
|
||||
"sprange": {1, 2, 3},
|
||||
"guarve": {4, 5, 6},
|
||||
"light urple": {7, 8, 9},
|
||||
}
|
||||
fmt.Println(favoriteShades)
|
||||
|
||||
elem, found := favoriteShades["poop"]
|
||||
fmt.Println("found it?", found, elem)
|
||||
}
|
||||
|
||||
func funcFun() {
|
||||
workIt := func(s string, fn func(string) string) string {
|
||||
return fn(s)
|
||||
}
|
||||
|
||||
daft := workIt("harder", strings.ToUpper) //+ makeIt("better", strings.ToLower) + doIt("faster", makesUs("stronger"))))
|
||||
fmt.Println("punk", daft)
|
||||
}
|
||||
|
||||
func closures() {
|
||||
// wut?
|
||||
}
|
||||
|
||||
type Madness struct {
|
||||
peak, trough int
|
||||
}
|
||||
|
||||
func (m Madness) Avg() int {
|
||||
return (m.peak + m.trough) / 2
|
||||
}
|
||||
|
||||
func methods() {
|
||||
redHaze := Madness{98, 47}
|
||||
fmt.Println("yo", redHaze.Avg(), Madness.Avg(redHaze))
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
module AoC2021
|
||||
|
||||
go 1.17
|
||||
|
||||
require rsc.io/quote v1.5.2
|
||||
|
||||
require (
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
|
||||
rsc.io/sampler v1.3.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
|
||||
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
|
||||
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
|
||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
|
||||
Reference in new issue
Block a user