Compare commits

..

33 Commits

Author SHA1 Message Date
42aecb2071 Day 6 solution. 2024-02-19 17:55:08 +00:00
8bbc7bf653 A bit of cleanup. 2024-02-18 22:22:37 +00:00
e0cd4c3f98 Day 05 solution. 2024-02-18 22:13:10 +00:00
d229aa44d9 Add a readme. 2024-02-17 20:20:38 +00:00
e1cf0cf53f Template update. 2024-02-17 20:11:41 +00:00
96fab0b0a7 Day 04 solution. 2024-02-17 20:11:27 +00:00
0e22e82652 Day 3 solution, plus some mucking about with the template code. 2024-02-17 01:45:09 +00:00
355f5f6628 Add template and utils library. 2024-02-15 18:09:56 +00:00
f2caf1d0cb Fix day 1 to work with our harness. 2024-02-15 17:57:00 +00:00
64fd4e99c4 Fix module name. 2024-02-15 17:50:57 +00:00
1fd835c142 Remove unused stuff. 2024-02-15 17:50:28 +00:00
5bb3392691 Move code into a single cargo module with an execution harness. 2024-02-15 17:49:57 +00:00
5b3a5d829d Add day 2 solution. 2024-02-15 15:34:35 +00:00
e04df4b548 Implement step 2 for day 1. 2024-02-14 20:04:34 +00:00
577da4ab52 Refactor code to be slightly more idiomatic, maybe 2024-02-14 19:10:53 +00:00
4bda83476c 2022 day 1. Learning rust! 2024-02-14 18:44:02 +00:00
849fc74a15 Day 20 solved. Sometimes it produces no answer... some configurations seem to fail to render the correct image. ¯\_(ツ)_/¯ 2020-12-26 01:06:14 +00:00
c604a4074f Day 25 solved. 2020-12-25 21:33:52 +00:00
ed95f875ad Day 24 solved. 2020-12-25 21:18:34 +00:00
3f9bfdf512 Day 23 solved. 2020-12-25 09:58:23 +00:00
35eeb8744a Make template better. 2020-12-25 09:12:18 +00:00
581e2571de Day 22 solution. 2020-12-25 05:49:31 +00:00
8bc0ff81c3 Day 21 solved. 2020-12-21 06:07:42 +00:00
f09b1fc787 Day 19.2 solved. 2020-12-20 04:00:20 +00:00
817a08aba7 Day 19.1 solved. 2020-12-20 03:20:03 +00:00
034cae395f Day 18 solved. 2020-12-18 11:46:20 +00:00
66e48278c5 Day 17 solved 2020-12-17 05:36:05 +00:00
f6f1929382 Day 16 solution. 2020-12-16 07:55:39 +00:00
0ba29a4e75 Day 15 solution. 2020-12-15 05:25:21 +00:00
2614bb2def Refactor some of our tool code. 2020-12-14 23:27:46 +00:00
4cc37c6e39 Day 14 solution. 2020-12-14 23:20:53 +00:00
3983164a45 Add day 13 solution. This one is super ugly. 2020-12-13 10:08:45 +00:00
2804abb674 Day 12 solution. 2020-12-12 05:46:31 +00:00
47 changed files with 9186 additions and 27 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
**/target/

View File

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"os" "os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserIntMap(os.Args[2]) values := util.InputParserIntMap(os.Args[2])
switch step { switch step {
case "1": case "1":

View File

@ -7,7 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
func checkLine(line string, step string) int { func checkLine(line string, step string) int {
@ -50,7 +50,7 @@ func checkLine(line string, step string) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
total := 0 total := 0
for _, line := range values { for _, line := range values {

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
func countTrees(input []string, right, down int) int { func countTrees(input []string, right, down int) int {
@ -26,7 +26,7 @@ func countTrees(input []string, right, down int) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
switch step { switch step {
case "1": case "1":

View File

@ -8,7 +8,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
@ -130,7 +129,7 @@ func makeInt(input string) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
values = util.SplitOnBlankLine(values) values = util.SplitOnBlankLine(values)
fmt.Println("Valid Passports:", countValidPassports(values, step)) fmt.Println("Valid Passports:", countValidPassports(values, step))

View File

@ -7,7 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
func calculateSeatNumber(input string) int { func calculateSeatNumber(input string) int {
@ -28,7 +28,7 @@ func calculateSeatNumber(input string) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
switch step { switch step {
case "1": case "1":

View File

@ -5,7 +5,6 @@ import (
"os" "os"
"strings" "strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
@ -45,7 +44,7 @@ func countAnswerIntersection(input string) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
groups := util.SplitOnBlankLine(values) groups := util.SplitOnBlankLine(values)
total := 0 total := 0

View File

@ -7,7 +7,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
type Node struct { type Node struct {
@ -89,7 +89,7 @@ func countChildren(node *Node) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
nodeMap = make(map[string]*Node) nodeMap = make(map[string]*Node)

View File

@ -7,7 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
type Instruction struct { type Instruction struct {
@ -59,7 +59,7 @@ func executeProgram(instructions []Instruction) (int, bool) {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
instructions := make([]Instruction, 0) instructions := make([]Instruction, 0)

View File

@ -5,7 +5,7 @@ import (
"log" "log"
"os" "os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
type SumTracker map[int][]Tuple type SumTracker map[int][]Tuple
@ -85,7 +85,7 @@ func sumMinMax(values []int) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserInts(os.Args[2]) values := util.InputParserInts(os.Args[2])
var badValue int var badValue int
sumMap := make(SumTracker) sumMap := make(SumTracker)

View File

@ -5,7 +5,7 @@ import (
"log" "log"
"os" "os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
func getMax(values map[int]bool) int { func getMax(values map[int]bool) int {
@ -88,7 +88,7 @@ func countPaths(node *Node, target int) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserIntMap(os.Args[2]) values := util.InputParserIntMap(os.Args[2])
diffMap := make(map[int]int) diffMap := make(map[int]int)

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
func occupied1(board [][]byte, i, j int) int { func occupied1(board [][]byte, i, j int) int {
@ -95,7 +95,7 @@ func countOccupied(board [][]byte) int {
func main() { func main() {
step := os.Args[1] step := os.Args[1]
board := fileutils.InputParserBytes(os.Args[2]) board := util.InputParserBytes(os.Args[2])
changed := true changed := true
for changed { for changed {

148
2020/day12.go Normal file
View File

@ -0,0 +1,148 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
func distance(x1, y1, x2, y2 int) int {
dy := y2 - y1
dx := x2 - x1
if dy < 0 {
dy *= -1
}
if dx < 0 {
dx *= -1
}
return dx + dy
}
func rotate(x, y int, direction byte, degrees int) (int, int) {
turns := degrees / 90
newX := x
newY := y
for i := 0; i < turns; i++ {
hold := newX
newX = newY
newY = hold
switch direction {
case 'L':
newX *= -1
case 'R':
newY *= -1
}
}
return newX, newY
}
func step1(values []string) (int, int) {
facing := 90
x := 0
y := 0
for _, line := range values {
direction := line[0]
value, err := strconv.Atoi(line[1:])
if err != nil {
log.Panicf(err.Error())
}
switch direction {
case 'N':
y += value
case 'S':
y -= value
case 'E':
x += value
case 'W':
x -= value
case 'L':
facing -= value
case 'R':
facing += value
case 'F':
// the hard one
switch facing {
case 0:
y += value
case 90:
x += value
case 180:
y -= value
case 270:
x -= value
default:
log.Panicf("Oh no, ships can't face %d degrees!", facing)
}
}
if facing < 0 {
facing += 360
}
if facing >= 360 {
facing %= 360
}
}
return x, y
}
func step2(values []string) (int, int) {
x := 0
y := 0
wayX := 10
wayY := 1
for _, line := range values {
direction := line[0]
value, err := strconv.Atoi(line[1:])
if err != nil {
log.Panicf(err.Error())
}
switch direction {
case 'N':
wayY += value
case 'S':
wayY -= value
case 'E':
wayX += value
case 'W':
wayX -= value
case 'L':
wayX, wayY = rotate(wayX, wayY, direction, value)
case 'R':
wayX, wayY = rotate(wayX, wayY, direction, value)
case 'F':
for i := 0; i < value; i++ {
x += wayX
y += wayY
}
}
}
return x, y
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
x := 0
y := 0
switch step {
case "1":
x, y = step1(values)
case "2":
x, y = step2(values)
}
fmt.Println(distance(x, y, 0, 0))
}

122
2020/day13.go Normal file
View File

@ -0,0 +1,122 @@
package main
import (
"fmt"
"log"
"math"
"math/big"
"os"
"strconv"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
//
// Begin code borrowed from https://golang.hotexamples.com/examples/math.big/Int/GCD/golang-int-gcd-method-examples.html
//
func crt(a, n []*big.Int) (*big.Int, error) {
one := new(big.Int).SetInt64(1)
p := new(big.Int).Set(n[0])
for _, n1 := range n[1:] {
p.Mul(p, n1)
}
var x, q, s, z big.Int
for i, n1 := range n {
q.Div(p, n1)
z.GCD(nil, &s, n1, &q)
if z.Cmp(one) != 0 {
return nil, fmt.Errorf("%d not coprime", n1)
}
x.Add(&x, s.Mul(a[i], s.Mul(&s, &q)))
}
return x.Mod(&x, p), nil
}
//
// End borrowed code
//
func parseInput(input []string) (int, []int) {
earliest, err := strconv.Atoi(input[0])
if err != nil {
log.Panicf(err.Error())
}
busList := make([]int, 0)
for _, value := range strings.Split(input[1], ",") {
if value == "x" {
busList = append(busList, -1)
continue
}
x, err := strconv.Atoi(value)
if err != nil {
log.Panicf(err.Error())
}
busList = append(busList, x)
}
return earliest, busList
}
func findBus(busList []int, earliest int) (int, int) {
bestBus := -1
bestTime := -1
for _, bus := range busList {
if bus == -1 {
continue
}
time := int(math.Ceil(float64(earliest)/float64(bus))) * bus
if bestBus == -1 || time < bestTime {
bestBus = bus
bestTime = time
}
}
return bestBus, bestTime
}
// This uses the Chinese Remainder Theorem to calculate the answer.
// I don't actually understand the underlying logic, I just found this
// while looking around for modulus-related algorithms.
func findTimestampWithCRT(busList []int) *big.Int {
bigBusList := make([]*big.Int, 0)
offsetList := make([]*big.Int, 0)
for i, bus := range busList {
if bus == -1 {
continue
}
bigBus := big.NewInt(int64(bus))
bigBusList = append(bigBusList, bigBus)
offset := big.NewInt(int64(i))
offset.Sub(bigBus, offset)
offsetList = append(offsetList, offset)
}
fmt.Println(offsetList)
fmt.Println(bigBusList)
value, err := crt(offsetList, bigBusList)
if err != nil {
log.Panicf(err.Error())
}
return value
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
earliest, busList := parseInput(values)
switch step {
case "1":
busId, departureTime := findBus(busList, earliest)
fmt.Println(busId * (departureTime - earliest))
case "2":
fmt.Println(findTimestampWithCRT(busList))
}
}

160
2020/day14.go Normal file
View File

@ -0,0 +1,160 @@
package main
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
type Instruction struct {
Op string
Value0 int64 // address for mem, zeromask for mask
Value1 int64 // value for mem, onemask for mask
}
func makeMask(input string, maskType byte) int64 {
var mask int64
for i := 0; i < len(input); i++ {
index := len(input) - 1 - i
if input[index] == maskType {
mask = mask | (int64(1) << i)
}
}
return mask & 0xfffffffff
}
func applyFloatMask(address int64, mask int64) []int64 {
results := make([]int64, 0)
results = append(results, address)
for i := 0; i < 36; i++ {
if mask&(1<<i) != 0 {
results = fanoutAddresses(results, i)
}
}
return results
}
func fanoutAddresses(addresses []int64, bit int) []int64 {
newAddresses := make([]int64, 0)
for _, a := range addresses {
newAddresses = append(newAddresses, a|1<<bit)
newAddresses = append(newAddresses, a&(^(1 << bit)))
}
return newAddresses
}
var maskRe = regexp.MustCompile("^mask = ([01X]+)$")
var memRe = regexp.MustCompile("^mem\\[([0-9]+)\\] = ([0-9]+)")
func parseProgram(input []string, step string) []Instruction {
program := make([]Instruction, 0)
for _, line := range input {
maskData := maskRe.FindAllStringSubmatch(line, 10)
memData := memRe.FindAllStringSubmatch(line, 10)
if len(maskData) != 0 {
mask := maskData[0][1]
var mask0, mask1 int64
switch step {
case "1":
mask0 = makeMask(mask, '0')
mask1 = makeMask(mask, '1')
case "2":
mask0 = makeMask(mask, '1')
mask1 = makeMask(mask, 'X')
}
program = append(program, Instruction{
Op: "mask",
Value0: mask0,
Value1: mask1,
})
} else if len(memData) != 0 {
address, err := strconv.Atoi(memData[0][1])
if err != nil {
log.Panicf(err.Error())
}
value, err := strconv.Atoi(memData[0][2])
if err != nil {
log.Panicf(err.Error())
}
program = append(program, Instruction{
Op: "mem",
Value0: int64(address),
Value1: int64(value),
})
} else {
log.Panicf("Program parse error: %s", line)
}
}
return program
}
// returns the program's memory dump
func executeProgram1(program []Instruction) map[int64]int64 {
memory := make(map[int64]int64)
mask := make([]int64, 2)
for _, instruction := range program {
switch instruction.Op {
case "mask":
mask[0] = instruction.Value0
mask[1] = instruction.Value1
case "mem":
address := instruction.Value0
value := instruction.Value1
value = value & mask[0]
value = value | mask[1]
memory[address] = value & 0xfffffffff
}
}
return memory
}
func executeProgram2(program []Instruction) map[int64]int64 {
memory := make(map[int64]int64)
mask := make([]int64, 2)
for _, instruction := range program {
switch instruction.Op {
case "mask":
mask[0] = instruction.Value0
mask[1] = instruction.Value1
case "mem":
address := instruction.Value0
value := instruction.Value1
address = address | mask[0]
addresses := applyFloatMask(address, mask[1])
for _, a := range addresses {
memory[a] = value & 0xfffffffff
}
}
}
return memory
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
var memory map[int64]int64
program := parseProgram(values, step)
switch step {
case "1":
memory = executeProgram1(program)
case "2":
memory = executeProgram2(program)
}
var total int64
for _, value := range memory {
total += value
}
fmt.Println(total)
}

38
2020/day15.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"fmt"
"os"
)
func main() {
step := os.Args[1]
indexMap := make(map[int]int)
indexMap[0] = 0
indexMap[14] = 1
indexMap[1] = 2
indexMap[3] = 3
indexMap[7] = 4
last := 9
loops := 0
switch step {
case "1":
loops = 2020
case "2":
loops = 30000000
}
for i := 5; i < loops-1; i++ {
if x, ok := indexMap[last]; ok {
indexMap[last] = i
// fmt.Printf("%d was last seen %d ago\n", last, i-x)
last = i - x
} else {
indexMap[last] = i
// fmt.Printf("%d has not been seen before\n", last)
last = 0
}
}
fmt.Println(last)
}

214
2020/day16.go Normal file
View File

@ -0,0 +1,214 @@
package main
import (
"fmt"
"log"
"os"
"regexp"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
const (
FIELDS = iota
MY_TICKET
OTHER_TICKETS
)
type Field struct {
Name string
Min1 int
Min2 int
Max1 int
Max2 int
Index int
Indexes map[int]bool
}
func parseInput(input []string) ([]*Field, []int, [][]int) {
fields := make([]*Field, 0)
myTicket := make([]int, 0)
otherTickets := make([][]int, 0)
state := FIELDS
re := regexp.MustCompile("^(.*): ([0-9]+)-([0-9]+) or ([0-9]+)-([0-9]+)$")
for _, line := range input {
if line == "your ticket:" {
state = MY_TICKET
continue
} else if line == "nearby tickets:" {
state = OTHER_TICKETS
continue
} else if line == "" {
continue
}
switch state {
case FIELDS:
data := re.FindAllStringSubmatch(line, 32)
field := &Field{
Name: data[0][1],
Min1: util.MustAtoi(data[0][2]),
Max1: util.MustAtoi(data[0][3]),
Min2: util.MustAtoi(data[0][4]),
Max2: util.MustAtoi(data[0][5]),
Index: -1,
Indexes: make(map[int]bool),
}
fields = append(fields, field)
case MY_TICKET:
for _, num := range strings.Split(line, ",") {
myTicket = append(myTicket, util.MustAtoi(num))
}
case OTHER_TICKETS:
ticket := make([]int, 0)
for _, num := range strings.Split(line, ",") {
ticket = append(ticket, util.MustAtoi(num))
}
otherTickets = append(otherTickets, ticket)
}
}
return fields, myTicket, otherTickets
}
func makeValidValueMap(fields []*Field) map[int]bool {
valid := make(map[int]bool)
for _, field := range fields {
for i := field.Min1; i <= field.Max1; i++ {
valid[i] = true
}
for i := field.Min2; i <= field.Max2; i++ {
valid[i] = true
}
}
return valid
}
func findInvalidValues(fields []*Field, tickets [][]int) []int {
answers := make([]int, 0)
// make a map of all valid values
valid := makeValidValueMap(fields)
for _, ticket := range tickets {
for _, value := range ticket {
if _, ok := valid[value]; !ok {
answers = append(answers, value)
}
}
}
return answers
}
func removeInvalidTickets(fields []*Field, tickets [][]int) [][]int {
answers := make([][]int, 0)
valid := makeValidValueMap(fields)
for _, ticket := range tickets {
badTicket := false
for _, value := range ticket {
if _, ok := valid[value]; !ok {
badTicket = true
}
}
if !badTicket {
answers = append(answers, ticket)
}
}
return answers
}
// sets the indexes in place
func findIndexes(fields []*Field, tickets [][]int) {
for _, field := range fields {
for i := 0; i < len(fields); i++ {
field.Indexes[i] = true
}
}
for _, ticket := range tickets {
for i, num := range ticket {
for _, f := range fields {
if f.Indexes[i] && !((num >= f.Min1 && num <= f.Max1) ||
(num >= f.Min2 && num <= f.Max2)) {
// this field cannot be in this space
f.Indexes[i] = false
}
}
}
}
unique := false
for !unique {
unique = true
for _, field := range fields {
if field.Index != -1 {
continue
}
found := make([]int, 0)
for index, valid := range field.Indexes {
if valid {
found = append(found, index)
}
}
if len(found) == 0 {
log.Panicf("Failed to find any solution for field %s: %v", field.Name, field.Indexes)
}
if len(found) == 1 {
field.Index = found[0]
}
}
// now weed out duplicates
for _, field := range fields {
// any field we're certain of we can set to false on all fields
if field.Index != -1 {
for _, subField := range fields {
subField.Indexes[field.Index] = false
}
} else {
// any field we haven't set an Index for means it is still potentially ambiguous
unique = false
}
}
}
}
func mulDepartureFields(fields []*Field, ticket []int) int {
product := 1
for _, field := range fields {
if strings.Contains(field.Name, "departure") {
product *= ticket[field.Index]
}
}
return product
}
// 980 too low
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
fields, myTicket, otherTickets := parseInput(values)
switch step {
case "1":
invalidValues := findInvalidValues(fields, otherTickets)
total := 0
for _, value := range invalidValues {
total += value
}
fmt.Println(total)
case "2":
otherTickets = removeInvalidTickets(fields, otherTickets)
findIndexes(fields, otherTickets)
fmt.Println(mulDepartureFields(fields, myTicket))
}
}

135
2020/day17.go Normal file
View File

@ -0,0 +1,135 @@
package main
import (
"fmt"
"log"
"os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
func parseInputCubes(input []string) map[[3]int]bool {
state := make(map[[3]int]bool)
for x, line := range input {
for y, char := range line {
coords := [3]int{x, y, 0}
if char == '#' {
state[coords] = true
}
}
}
return state
}
func iterateCubes(prev map[[3]int]bool) map[[3]int]bool {
counts := make(map[[3]int]int)
for node, v := range prev {
if !v {
log.Panicf("Unexpected false value!")
}
x := node[0]
y := node[1]
z := node[2]
neighbors := make([][3]int, 26)
index := 0
for i := -1; i < 2; i++ {
for j := -1; j < 2; j++ {
for k := -1; k < 2; k++ {
if i == 0 && j == 0 && k == 0 {
continue
}
neighbors[index] = [3]int{x + i, y + j, z + k}
index++
}
}
}
for _, n := range neighbors {
counts[n]++
}
}
next := make(map[[3]int]bool)
for node, count := range counts {
if (prev[node] && count == 2) || count == 3 {
next[node] = true
}
}
return next
}
func parseInputHypercubes(input []string) map[[4]int]bool {
state := make(map[[4]int]bool)
for x, line := range input {
for y, char := range line {
coords := [4]int{x, y, 0, 0}
if char == '#' {
state[coords] = true
}
}
}
return state
}
func iterateHypercubes(prev map[[4]int]bool) map[[4]int]bool {
counts := make(map[[4]int]int)
for node, v := range prev {
if !v {
log.Panicf("Unexpected false value!")
}
x := node[0]
y := node[1]
z := node[2]
w := node[3]
neighbors := make([][4]int, 80)
index := 0
for i := -1; i < 2; i++ {
for j := -1; j < 2; j++ {
for k := -1; k < 2; k++ {
for l := -1; l < 2; l++ {
if i == 0 && j == 0 && k == 0 && l == 0 {
continue
}
neighbors[index] = [4]int{x + i, y + j, z + k, w + l}
index++
}
}
}
}
for _, n := range neighbors {
counts[n]++
}
}
next := make(map[[4]int]bool)
for node, count := range counts {
if (prev[node] && count == 2) || count == 3 {
next[node] = true
}
}
return next
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
switch step {
case "1":
state := parseInputCubes(values)
for i := 0; i < 6; i++ {
state = iterateCubes(state)
}
fmt.Println(len(state))
case "2":
state := parseInputHypercubes(values)
for i := 0; i < 6; i++ {
state = iterateHypercubes(state)
}
fmt.Println(len(state))
}
}

151
2020/day18.go Normal file
View File

@ -0,0 +1,151 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
// operators
const (
ADD = -1
MUL = -2
GRP = -3
)
type Stack struct {
data []int
}
func NewStack() *Stack {
return &Stack{
data: make([]int, 0),
}
}
func (s *Stack) Push(value int) {
s.data = append(s.data, value)
}
func (s *Stack) Pop() int {
if s.Empty() {
log.Panicf("Tried to pop from empty stack")
}
ret := s.Top()
s.data = s.data[:len(s.data)-1]
return ret
}
func (s *Stack) Top() int {
if s.Empty() {
return -3
}
return s.data[len(s.data)-1]
}
func (s *Stack) Empty() bool {
return len(s.data) == 0
}
// This only works when the starting expression is all positive, single-digit integers
// and where all operators are additive (i.e. we rely on being able to use negative numbers
// as sentinel values.
func convertRPN(exp string, precedence map[int]int) []int {
rpn := make([]int, 0)
ops := NewStack()
for _, i := range exp {
e := string(i)
x, err := strconv.Atoi(e)
if err == nil {
rpn = append(rpn, x)
continue
}
var opcode int
switch e {
case "+":
opcode = ADD
case "*":
opcode = MUL
case "(":
ops.Push(GRP)
continue
case ")":
for ops.Top() != GRP {
rpn = append(rpn, ops.Pop())
}
ops.Pop()
continue
default:
continue
}
if ops.Empty() {
ops.Push(opcode)
continue
}
if precedence[ops.Top()] >= precedence[opcode] {
rpn = append(rpn, ops.Pop())
}
ops.Push(opcode)
}
for !ops.Empty() {
rpn = append(rpn, ops.Pop())
}
return rpn
}
func evaluateExpression(rpn []int) int {
output := NewStack()
for _, value := range rpn {
if value >= 0 {
output.Push(value)
continue
}
switch value {
case ADD:
x := output.Pop()
y := output.Pop()
output.Push(x + y)
case MUL:
x := output.Pop()
y := output.Pop()
output.Push(x * y)
}
}
return output.Pop()
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
total := 0
precedence := make(map[int]int)
switch step {
case "1":
precedence[ADD] = 0
precedence[MUL] = 0
precedence[GRP] = -1
case "2":
precedence[ADD] = 1
precedence[MUL] = 0
precedence[GRP] = -1
}
exps := make([][]int, 0)
for _, line := range values {
// parse the expression into RPN
exps = append(exps, convertRPN(line, precedence))
}
for _, exp := range exps {
total += evaluateExpression(exp)
}
fmt.Println(total)
}

194
2020/day19.go Normal file
View File

@ -0,0 +1,194 @@
package main
import (
"fmt"
"os"
"regexp"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
const (
STATE_RULES = iota
STATE_DATA
)
type Node struct {
name string
children [][]*Node
}
func NewRule(name string) *Node {
return &Node{
name: name,
children: make([][]*Node, 0),
}
}
func parseRule(name string, rules map[string]string) *Node {
ruleString := rules[name]
rule := NewRule(name)
// and split into subrules
for _, text := range strings.Split(ruleString, " | ") {
// quoted strings are our terminals
if strings.HasPrefix(text, "\"") {
rule.name = strings.Trim(text, "\"") // change the rule to its "real" name
continue
}
// everything else are rule indexes, which we turn into child nodes
childSet := make([]*Node, 0)
for _, childName := range strings.Split(text, " ") {
childSet = append(childSet, parseRule(childName, rules))
}
rule.children = append(rule.children, childSet)
}
return rule
}
func parseInput(input []string) (map[string]string, []string) {
stateRe := regexp.MustCompile("^([0-9]+): (.*)$")
state := STATE_RULES
data := make([]string, 0)
rules := make(map[string]string) // unparsed rules
for _, line := range input {
switch state {
case STATE_RULES:
if line == "" {
state = STATE_DATA
continue
}
reData := stateRe.FindAllStringSubmatch(line, 16)
// rule name
rules[reData[0][1]] = reData[0][2]
case STATE_DATA:
data = append(data, line)
}
}
return rules, data
}
func createLanguage(rules map[string]string, rootName string) map[string]bool {
root := parseRule(rootName, rules)
// now we expand the grammar - generate all possible strings in the language
rawLanguage := expand(root)
language := make(map[string]bool)
for _, term := range rawLanguage {
language[term] = true
}
return language
}
// Expand the list of all possible substrings starting with node
func expand(node *Node) []string {
items := make([]string, 0)
if len(node.children) == 0 {
items = append(items, node.name)
return items
}
for _, childSet := range node.children {
newItems := make([][]string, 0)
for _, child := range childSet {
newItems = append(newItems, expand(child))
}
items = append(items, combine(newItems, "")...)
}
return items
}
// takes a list of strings and combines them in order, returning a list of
// strings of all possible combinations. Example:
// [["foo"] ["bar baz"] ["fnord"]] => ["foobarfnord" "foobazfnord"]
func combine(terms [][]string, acc string) []string {
remaining := len(terms)
results := make([]string, 0)
if remaining == 0 {
return results
}
for _, part := range terms[0] {
newAcc := acc + part
if remaining == 1 {
results = append(results, newAcc)
continue
}
results = append(results, combine(terms[1:], newAcc)...)
}
return results
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
rules, data := parseInput(values)
count := 0
switch step {
case "1":
language := createLanguage(rules, "0")
for _, item := range data {
if language[item] {
count++
}
}
case "2":
// stub
// for step 2, our base rule expands to 0: 42{N} 31{M}, for N > M and N > 2
// we exploit this, along with the fact that all valid strings for 42 and 31 are
// 8 characters long, to "cheat" a little
matchLength := 8
left := createLanguage(rules, "42")
right := createLanguage(rules, "31")
for _, item := range data {
leftCount := 0
rightCount := 0
onLeft := true
valid := true // valid until proven otherwise
for index := 0; index < len(item); index += matchLength {
if len(item[index:]) < matchLength {
fmt.Println("Bad Length")
valid = false // wrong length alignment
break
}
subString := item[index : index+matchLength]
if onLeft {
if left[subString] {
leftCount++
} else {
onLeft = false
}
}
if !onLeft {
if right[subString] {
rightCount++
} else {
valid = false
break
}
}
}
if valid && leftCount > 1 && rightCount > 0 && leftCount > rightCount {
count++
}
}
}
fmt.Println(count)
}

458
2020/day20.go Normal file
View File

@ -0,0 +1,458 @@
package main
import (
"fmt"
"log"
"os"
"regexp"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
const (
MATCH_NONE = iota
MATCH_TOP
MATCH_BOTTOM
MATCH_LEFT
MATCH_RIGHT
)
type Tile struct {
id int
data []string
rotation int
flippedX bool
flippedY bool
}
func NewTile() *Tile {
return &Tile{
data: make([]string, 0),
rotation: 0,
flippedX: false,
flippedY: false,
}
}
func (t *Tile) print() {
for _, line := range t.data {
fmt.Println(line)
}
fmt.Println()
}
func (t *Tile) rotate() {
newData := make([]string, len(t.data))
for i := len(t.data) - 1; i >= 0; i-- {
for j, char := range t.data[i] {
newData[j] += string(char)
}
}
t.data = newData
t.rotation++
if t.rotation > 3 {
t.rotation = 0
}
}
func (t *Tile) flipX() {
newData := make([]string, len(t.data))
for i := 0; i < len(newData); i++ {
newData[i] = t.data[len(t.data)-1-i]
}
t.data = newData
t.flippedX = !t.flippedX
}
func (t *Tile) flipY() {
newData := make([]string, len(t.data))
for i := 0; i < len(newData); i++ {
for j := 0; j < len(t.data[i]); j++ {
newData[i] += string(t.data[i][len(t.data)-1-j])
}
}
t.data = newData
t.flippedY = !t.flippedY
}
func (t *Tile) reset() {
for t.rotation != 0 {
t.rotate()
}
if t.flippedX {
t.flipX()
}
if t.flippedY {
t.flipY()
}
}
func parseInput(input []string) map[int]*Tile {
tileMap := make(map[int]*Tile)
re := regexp.MustCompile("^Tile ([0-9]+):$")
id := 0
tile := NewTile()
for _, line := range input {
if re.MatchString(line) {
id = util.MustAtoi(re.FindStringSubmatch(line)[1])
tile.id = id
continue
}
if line == "" {
tileMap[id] = tile
tile = NewTile()
id = 0
continue
}
tile.data = append(tile.data, line)
}
// if needed, add the last one (might be missing our final blank line)
if id != 0 {
tileMap[id] = tile
}
return tileMap
}
func matchTiles(oldTile, newTile *Tile) int {
for i := 0; i < 4; i++ {
check := subMatchTiles(oldTile, newTile)
if check != MATCH_NONE {
return check
}
newTile.rotate()
}
newTile.reset()
newTile.flipX()
for i := 0; i < 4; i++ {
check := subMatchTiles(oldTile, newTile)
if check != MATCH_NONE {
return check
}
newTile.rotate()
}
newTile.reset()
newTile.flipY()
for i := 0; i < 4; i++ {
check := subMatchTiles(oldTile, newTile)
if check != MATCH_NONE {
return check
}
newTile.rotate()
}
newTile.reset()
newTile.flipX()
newTile.flipY()
for i := 0; i < 4; i++ {
check := subMatchTiles(oldTile, newTile)
if check != MATCH_NONE {
return check
}
newTile.rotate()
}
return MATCH_NONE
}
func subMatchTiles(oldTile, newTile *Tile) int {
// check top
if oldTile.data[0] == newTile.data[0] {
return MATCH_TOP
}
// check bottom
if oldTile.data[len(oldTile.data)-1] == newTile.data[len(newTile.data)-1] {
return MATCH_BOTTOM
}
// check left
match := true
for i := 0; i < len(oldTile.data); i++ {
if oldTile.data[i][0] != newTile.data[i][0] {
match = false
break
}
}
if match {
return MATCH_LEFT
}
// check right
match = true
for i := 0; i < len(oldTile.data); i++ {
if oldTile.data[i][len(oldTile.data)-1] != newTile.data[i][len(newTile.data)-1] {
match = false
break
}
}
if match {
return MATCH_RIGHT
}
return MATCH_NONE
}
func arrangeTiles(tiles map[int]*Tile) map[[2]int]*Tile {
grid := make(map[[2]int]*Tile)
looseTiles := make([]*Tile, 0)
for _, v := range tiles {
looseTiles = append(looseTiles, v)
}
// arbitrarily place a first tile
grid[[2]int{0, 0}] = looseTiles[0]
looseTiles = looseTiles[1:]
for len(looseTiles) > 0 {
for coord, tile := range grid {
if _, ok := grid[[2]int{coord[0] + 1, coord[1]}]; ok {
if _, ok := grid[[2]int{coord[0] - 1, coord[1]}]; ok {
if _, ok := grid[[2]int{coord[0], coord[1] + 1}]; ok {
if _, ok := grid[[2]int{coord[0], coord[1] - 1}]; ok {
continue
}
}
}
}
for i, loose := range looseTiles {
matched := matchTiles(tile, loose)
// On a match, flip appropriately and set coords
// check for already present tiles as well - skip if that's the case
var newCoords [2]int
willFlipX := true
switch matched {
case MATCH_TOP:
newCoords = [2]int{coord[0], coord[1] + 1}
case MATCH_BOTTOM:
newCoords = [2]int{coord[0], coord[1] - 1}
case MATCH_LEFT:
newCoords = [2]int{coord[0] - 1, coord[1]}
willFlipX = false
case MATCH_RIGHT:
newCoords = [2]int{coord[0] + 1, coord[1]}
willFlipX = false
}
if matched != MATCH_NONE {
if _, ok := grid[newCoords]; ok {
continue
}
if willFlipX {
loose.flipX()
} else {
loose.flipY()
}
grid[newCoords] = loose
if i == len(looseTiles)-1 {
looseTiles = looseTiles[:i]
} else {
looseTiles = append(looseTiles[:i], looseTiles[i+1:]...)
}
break // to avoid sequencing issues
}
}
}
}
return grid
}
func findCorners(grid map[[2]int]*Tile) []*Tile {
corners := make([]*Tile, 0)
// min and max coord values
minX, minY, maxX, maxY := findLimits(grid)
corners = append(corners, grid[[2]int{minX, minY}])
corners = append(corners, grid[[2]int{maxX, minY}])
corners = append(corners, grid[[2]int{minX, maxY}])
corners = append(corners, grid[[2]int{maxX, maxY}])
return corners
}
func findLimits(grid map[[2]int]*Tile) (int, int, int, int) {
var minX, minY, maxX, maxY int
for coord, _ := range grid {
if coord[0] > maxX {
maxX = coord[0]
}
if coord[0] < minX {
minX = coord[0]
}
if coord[1] > maxY {
maxY = coord[1]
}
if coord[1] < minY {
minY = coord[1]
}
}
return minX, minY, maxX, maxY
}
func stripBorder(tile *Tile) []string {
ret := make([]string, 0)
for i := 1; i < len(tile.data)-1; i++ {
ret = append(ret, tile.data[i][1:len(tile.data)-1])
}
return ret
}
func combineTiles(grid map[[2]int]*Tile) *Tile {
bigTile := NewTile()
minX, minY, maxX, maxY := findLimits(grid)
for j := maxY; j >= minY; j-- {
row := make([]string, 8)
for i := minX; i <= maxX; i++ {
var tile *Tile
tile, ok := grid[[2]int{i, j}]
if !ok {
log.Panicf("Couldn't find tile: %d, %d", i, j)
}
data := stripBorder(tile)
for i, line := range data {
row[i] = row[i] + line
}
}
bigTile.data = append(bigTile.data, row...)
}
return bigTile
}
var monsterPattern = []string{
" # ",
"# ## ## ###",
" # # # # # # ",
}
func subFilterMonsters(image *Tile) int {
numMonsters := 0
for i := 0; i < len(image.data)-2; i++ {
for j := 0; j < len(image.data[0])-19; j++ {
if monsterCheck(image, i, j) {
removeMonster(image, i, j)
numMonsters++
}
}
}
return numMonsters
}
func removeMonster(image *Tile, i, j int) {
for y := 0; y < 3; y++ {
for x := 0; x < 20; x++ {
if monsterPattern[y][x] == '#' {
if j+x == len(image.data[i+y])-1 {
image.data[i+y] = image.data[i+y][:j+x] + "O"
} else {
image.data[i+y] = image.data[i+y][:j+x] + "O" + image.data[i+y][j+x+1:]
}
}
}
}
}
func monsterCheck(image *Tile, i, j int) bool {
data := image.data
for y := 0; y < 3; y++ {
for x := 0; x < 20; x++ {
if data[i+y][j+x] == '.' && monsterPattern[y][x] == '#' {
return false
}
}
}
return true
}
func filterMonsters(image *Tile) int {
numMonsters := 0
for i := 0; i < 4; i++ {
numMonsters = subFilterMonsters(image)
if numMonsters > 0 {
return numMonsters
}
image.rotate()
}
image.reset()
image.flipX()
for i := 0; i < 4; i++ {
numMonsters = subFilterMonsters(image)
if numMonsters > 0 {
return numMonsters
}
}
image.reset()
image.flipY()
for i := 0; i < 4; i++ {
numMonsters = subFilterMonsters(image)
if numMonsters > 0 {
return numMonsters
}
}
image.reset()
image.flipX()
image.flipY()
for i := 0; i < 4; i++ {
numMonsters = subFilterMonsters(image)
if numMonsters > 0 {
return numMonsters
}
}
return numMonsters
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
tileMap := parseInput(values)
grid := arrangeTiles(tileMap)
switch step {
case "1":
corners := findCorners(grid)
product := 1
for _, tile := range corners {
product *= tile.id
}
fmt.Println(product)
case "2":
image := combineTiles(grid)
monsters := filterMonsters(image)
if monsters == 0 {
log.Panicf("Found no monsters")
}
count := 0
for _, line := range image.data {
for _, char := range line {
if char == '#' {
count++
}
}
}
fmt.Println(count)
}
}

134
2020/day21.go Normal file
View File

@ -0,0 +1,134 @@
package main
import (
"fmt"
"os"
"regexp"
"sort"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
// could optimize this better with maps, but we'll just do this. Our data set should be small enough
func intersection(a, b []string) (result []string) {
result = make([]string, 0)
for _, av := range a {
for _, bv := range b {
if av == bv {
result = append(result, av)
break
}
}
}
return
}
func includes(list []string, item string) bool {
for _, cmp := range list {
if cmp == item {
return true
}
}
return false
}
func remove(list []string, item string) []string {
for i, cmp := range list {
if cmp == item {
return append(list[:i], list[i+1:]...)
}
}
return list
}
func parseInput(input []string) (ingredients map[string]int, allergens map[string][]string) {
re := regexp.MustCompile("^(.*) \\(contains (.*)\\)")
allergens = make(map[string][]string)
ingredients = make(map[string]int)
for _, line := range input {
data := re.FindStringSubmatch(line)
is := strings.Split(data[1], " ")
as := strings.Split(data[2], ", ")
for _, ingredient := range is {
ingredients[ingredient]++
}
for _, allergen := range as {
if ingredients, ok := allergens[allergen]; ok {
allergens[allergen] = intersection(is, ingredients)
} else {
allergens[allergen] = is
}
}
}
return
}
func findUniqueSolution(aMap map[string][]string) {
done := false
for !done {
done = true
for a1, is1 := range aMap {
if len(is1) == 1 {
ingredient := is1[0]
for a2, is2 := range aMap {
if a1 == a2 {
continue
}
aMap[a2] = remove(is2, ingredient)
}
}
}
for _, is := range aMap {
if len(is) > 1 {
done = false
break
}
}
}
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
ingredients, allergenMap := parseInput(values)
switch step {
case "1":
loose := make([]string, 0)
for ingredient, _ := range ingredients {
found := false
for _, is := range allergenMap {
if includes(is, ingredient) {
found = true
break
}
}
if !found {
loose = append(loose, ingredient)
}
}
total := 0
for _, i := range loose {
total += ingredients[i]
}
fmt.Println(total)
case "2":
findUniqueSolution(allergenMap)
keys := make([]string, 0)
for k, _ := range allergenMap {
keys = append(keys, k)
}
result := ""
sort.Strings(keys)
for _, k := range keys {
result = result + "," + allergenMap[k][0]
}
fmt.Println(strings.Trim(result, ","))
}
}

122
2020/day22.go Normal file
View File

@ -0,0 +1,122 @@
package main
import (
"fmt"
"os"
"strings"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
func parseInput(input []string) [][]int {
decks := make([][]int, 2)
decks[0] = make([]int, 0)
decks[1] = make([]int, 0)
player := 0
for _, line := range input {
if strings.HasPrefix(line, "Player") {
continue
}
if line == "" {
player++
continue
}
decks[player] = append(decks[player], util.MustAtoi(line))
}
return decks
}
func playGame(decks [][]int, step string) (int, [][]int) {
gameStates := make(map[string]struct{})
found := struct{}{}
for len(decks[0]) > 0 && len(decks[1]) > 0 {
state := serializeGame(decks)
if _, ok := gameStates[state]; ok && step == "2" {
return 0, decks
}
gameStates[state] = found
var winner int
// take the top cards
card0 := decks[0][0]
card1 := decks[1][0]
decks[0] = decks[0][1:]
decks[1] = decks[1][1:]
// determine the winner, put the cards in order
if card0 > card1 {
winner = 0
} else {
winner = 1
}
// if each card is >= the number of cards in that deck
if step == "2" && card0 <= len(decks[0]) && card1 <= len(decks[1]) {
newDecks := make([][]int, 2)
newDecks[0] = make([]int, card0)
newDecks[1] = make([]int, card1)
copy(newDecks[0], decks[0][:card0])
copy(newDecks[1], decks[1][:card1])
// Recursive Combat throws out the "normal" winner above
winner, _ = playGame(newDecks, step)
}
// stack the cards for adding to the winner's bottom
cards := make([]int, 2)
if winner == 0 {
cards[0] = card0
cards[1] = card1
} else {
cards[0] = card1
cards[1] = card0
}
// and add them to the winner's bottom
decks[winner] = append(decks[winner], cards...)
}
if len(decks[0]) > 0 {
return 0, decks
}
return 1, decks
}
func serializeGame(decks [][]int) string {
out := ""
for _, deck := range decks {
for _, card := range deck {
out = out + string(card) + ","
}
out = out + "|"
}
return out
}
func calculateScore(decks [][]int) int {
score := 0
var index int
if len(decks[0]) > 0 {
index = 0
} else {
index = 1
}
deckSize := len(decks[index])
for i := deckSize - 1; i >= 0; i-- {
score += decks[index][i] * (deckSize - i)
}
return score
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
input := parseInput(values)
_, decks := playGame(input, step)
fmt.Println(calculateScore(decks))
}

125
2020/day23.go Normal file
View File

@ -0,0 +1,125 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
type Cup struct {
label int
next *Cup
}
func parseInput(input string, step string) (*Cup, map[int]*Cup) {
labelMap := make(map[int]*Cup)
var firstCup *Cup
var prev *Cup
first := true
for _, char := range input {
label := util.MustAtoi(string(char))
newCup := &Cup{
label: label,
}
labelMap[label] = newCup
if first {
first = false
firstCup = newCup
} else {
prev.next = newCup
}
prev = newCup
}
if step == "2" {
for i := 10; i <= 1000000; i++ {
newCup := &Cup{
label: i,
}
prev.next = newCup
prev = newCup
labelMap[i] = newCup
}
}
prev.next = firstCup
return firstCup, labelMap
}
func move(current *Cup, labels map[int]*Cup, step string) *Cup {
// The crab picks up the three cups that are immediately clockwise of the current cup. They are removed from the circle; cup spacing is adjusted as necessary to maintain the circle.
pickedUp := current.next
current.next = current.next.next.next.next
cursor := pickedUp
holding := make(map[int]struct{})
for i := 0; i < 3; i++ {
holding[cursor.label] = struct{}{}
cursor = cursor.next
}
// The crab selects a destination cup: the cup with a label equal to the current cup's label minus one. If this would select one of the cups that was just picked up, the crab will keep subtracting one until it finds a cup that wasn't just picked up. If at any point in this process the value goes below the lowest value on any cup's label, it wraps around to the highest value on any cup's label instead.
dest := current.label - 1
_, nope := holding[dest]
for nope || dest == 0 {
dest--
if dest <= 0 {
switch step {
case "1":
dest = 9
case "2":
dest = 1000000
}
}
_, nope = holding[dest]
}
// The crab places the cups it just picked up so that they are immediately clockwise of the destination cup. They keep the same order as when they were picked up.
destCup, ok := labels[dest]
if !ok {
log.Panicf("No such cup %d", dest)
}
oldNext := destCup.next
destCup.next = pickedUp
pickedUp.next.next.next = oldNext
// The crab selects a new current cup: the cup which is immediately clockwise of the current cup.
return current.next
}
func formatCups(start *Cup) string {
answer := ""
startLabel := start.label
for cursor := start.next; cursor.label != startLabel; cursor = cursor.next {
answer = answer + strconv.Itoa(cursor.label)
}
return answer
}
// too high: 540428194020
func main() {
step := os.Args[1]
values := os.Args[2]
current, labels := parseInput(values, step)
switch step {
case "1":
for i := 0; i < 100; i++ {
current = move(current, labels, step)
}
fmt.Println(formatCups(labels[1]))
case "2":
for i := 0; i < 10000000; i++ {
current = move(current, labels, step)
}
cursor := labels[1]
fmt.Println(cursor.next.label)
fmt.Println(cursor.next.next.label)
fmt.Println(cursor.next.label * cursor.next.next.label)
}
}

98
2020/day24.go Normal file
View File

@ -0,0 +1,98 @@
package main
import (
"fmt"
"os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
func parseInput(input []string) map[[2]int]bool {
tiles := make(map[[2]int]bool)
for _, line := range input {
coords := [2]int{0, 0}
for i := 0; i < len(line); i++ {
switch line[i] {
case 'e':
coords[0]++
case 'w':
coords[0]--
case 'n':
next := line[i+1]
switch next {
case 'e':
coords[0]++
coords[1]--
case 'w':
coords[1]--
}
i++
case 's':
next := line[i+1]
switch next {
case 'e':
coords[1]++
case 'w':
coords[0]--
coords[1]++
}
i++
}
}
if tile, ok := tiles[coords]; ok && tile == true {
tiles[coords] = false
} else {
tiles[coords] = true
}
}
return tiles
}
func runGeneration(oldGen map[[2]int]bool) map[[2]int]bool {
counts := make(map[[2]int]int)
newGen := make(map[[2]int]bool)
for coords, live := range oldGen {
if live {
counts[[2]int{coords[0] + 1, coords[1]}]++
counts[[2]int{coords[0] - 1, coords[1]}]++
counts[[2]int{coords[0], coords[1] + 1}]++
counts[[2]int{coords[0], coords[1] - 1}]++
counts[[2]int{coords[0] + 1, coords[1] - 1}]++
counts[[2]int{coords[0] - 1, coords[1] + 1}]++
}
}
for coords, count := range counts {
if oldGen[coords] && count <= 2 {
newGen[coords] = true
}
if !oldGen[coords] && count == 2 {
newGen[coords] = true
}
}
return newGen
}
func main() {
step := os.Args[1]
values := util.InputParserStrings(os.Args[2])
tiles := parseInput(values)
if step == "2" {
for i := 0; i < 100; i++ {
tiles = runGeneration(tiles)
}
}
count := 0
for _, v := range tiles {
if v == true {
count++
}
}
fmt.Println(count)
}

51
2020/day25.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"fmt"
"os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/util"
)
func findLoopSize(pubKey, subject int) int {
value := 1
for i := 0; ; i++ {
value *= subject
value %= 20201227
if value == pubKey {
return i + 1
}
}
}
func transformLoop(pubKey, loopSize int) int {
value := 1
for i := 0; i < loopSize; i++ {
value *= pubKey
value %= 20201227
}
return value
}
func main() {
step := os.Args[1]
values := util.InputParserInts(os.Args[2])
keyPub := values[0]
doorPub := values[1]
keyLoop := findLoopSize(keyPub, 7)
doorLoop := findLoopSize(doorPub, 7)
encryptionKey1 := transformLoop(keyPub, doorLoop)
encryptionKey2 := transformLoop(doorPub, keyLoop)
switch step {
case "1":
fmt.Println(encryptionKey1)
fmt.Println(encryptionKey2)
case "2":
}
}

View File

@ -1,4 +1,4 @@
package fileutils package util
import ( import (
"bufio" "bufio"
@ -57,7 +57,8 @@ func InputParserStrings(filename string) []string {
if err != nil { if err != nil {
log.Panicf(err.Error()) log.Panicf(err.Error())
} }
return strings.Split(string(data), "\n") output := strings.Split(string(data), "\n")
return output[:len(output)-1]
} }
func InputParserBytes(filename string) [][]byte { func InputParserBytes(filename string) [][]byte {

View File

@ -1,5 +1,10 @@
package util package util
import (
"log"
"strconv"
)
// Takes a slice of strings as from reading each line in a file. // Takes a slice of strings as from reading each line in a file.
// Concatenates strings, creating new ones when blank lines are encountered // Concatenates strings, creating new ones when blank lines are encountered
// NB: adds a single space to the beginning of each concatenated line. // NB: adds a single space to the beginning of each concatenated line.
@ -19,3 +24,11 @@ func SplitOnBlankLine(input []string) []string {
return converted return converted
} }
func MustAtoi(input string) int {
ret, err := strconv.Atoi(input)
if err != nil {
log.Panicf(err.Error())
}
return ret
}

View File

@ -4,11 +4,15 @@ import (
"fmt" "fmt"
"os" "os"
"git.annabunch.es/annabunches/adventofcode/2020/lib/fileutils" "git.annabunch.es/annabunches/adventofcode/2020/lib/util"
) )
func main() { func main() {
// step := os.Args[1] step := os.Args[1]
values := fileutils.InputParserStrings(os.Args[2]) values := util.InputParserStrings(os.Args[2])
switch step {
case "1":
case "2":
}
} }

16
2022/Cargo.lock generated Normal file
View File

@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc2022"
version = "0.1.0"
dependencies = [
"sorted-vec",
]
[[package]]
name = "sorted-vec"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6734caf0b6f51addd5eeacca12fb39b2c6c14e8d4f3ac42f3a78955c0467458"

9
2022/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "aoc2022"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sorted-vec = "0.8.3"

2252
2022/input/day01.txt Normal file

File diff suppressed because it is too large Load Diff

2500
2022/input/day02.txt Normal file

File diff suppressed because it is too large Load Diff

300
2022/input/day03.txt Executable file
View File

@ -0,0 +1,300 @@
FzQrhQpJtJMFzlpplrTWjTnTTrjVsVvvTnTs
mScqSqqgcfPCqGPZcfGNSvTNsVVNSjNvWSNsNz
fPcPGqgCcHgFzQpJJtHtJH
DZDqqlrjplDHrNCmnBcHBMCRcJzb
RQFLStFvdcBbzdJbJM
PThQtwftTPFvtTPhvtFtfFtpZZllwjRNlsqNqqZjwpGlrZ
pPwtqgwJZPJLgQqSFlqhFFlqMd
DBmCWBBDWTRGvcVRTCCnnfQlFSdlzfhfdMWQfjhhQz
drmBVVCRgprPtrZp
HznjQjvmzDMVrQnMLJMMlfWgPSlJGWWJPl
BdcqqhcdBRpFhhZBthhctdJSJJWfgGFlJCSFgbWPCDJS
NdRTZdNqBwqtthpRBTTRqdtZrsLQVzrrzjzDwDsnmrQrnsrr
HZFZCFzZWszqsRTBZTNMhmthVTmhDppmMQVPpm
wjvSbJddvrvlrvnJSJJvlJmhPlhVPVtGVpQDBVMpphQP
frbrfrcvvnvjfwbcJgrrCBRsCFsNzRgRCHCqssRH
dDFNqNqZqPLNqvqTTvCLSPdZssGHClJQJcRHJGHHcHBcsMsQ
lrjmWgWWrhjgrppQHHMQrsQRJGcBJc
lVlmnwjmdTTSvVFN
FWNFHvQPmLGwwwSHtswwln
RfMJcDdfdcfdddfZjdchrtZmSmCZVtqVnZmrnrtC
JMmJcfjjphcghpgjhRGzGzBBGPFGNBvPTpFL
cVPVwStmmcQPBQPpSCppwhHZNNqHszNBhsNRNjqHzj
MfWdDgvdbnvgMTWgvgZfzmsZJHzNhqjqjRhJ
MDWMWGndMgFDnFLDwQrPPCSrCSVrlmGS
QLZmPdRdWmMsMD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pSLVLgWwtfshDNDqvWvGvlQZvDNHQHjqq
MPrzmdRrPPrCJFnMnMRRFRPdqqZQNQvjvZDGDlHhQvGNDG
BmBMBBJTMmPBJMMFCCFJRmrsTlVpVbpwLSVwLsgcwTVlVc
SSGzmFRzmRGLgSSmGMJFnvfvJnJVnJQnMl
cBpjHtjwNfcpNZtppHtCMlMPMlJBVlVQlvJPvJ
dNtNZwqWfqtqZWtHttsqHqrRrrdRTLbmmzSLmTGGmbrg
RrrddnrgnRbbgWdGrfnwgQwjDjDpvTpBQTwBPP
MHCStZJzSwvPjWQD
mcJWVHCCLcGLbdcn
PlMsdjPdGMjdPSrSjgddbLbmHHTszHZzpHmsTFvmpzZzmN
ntRJQVRfcQhcQWhnchBJWntTFTTTNTSpFtztmZFDTpDZ
hQfcfCBSwCccVJhSJnrPPGLqPlbPLCrqldgb
vgvWDMZvGpcqgqsP
tSdtjLHLQLHjdFdDddQSQhwlsGqwQlqqqhQsPhGc
tbRjtTLFRvTZDBrMrV

1000
2022/input/day04.txt Executable file

File diff suppressed because it is too large Load Diff

512
2022/input/day05.txt Executable file
View File

@ -0,0 +1,512 @@
[B] [L] [J]
[B] [Q] [R] [D] [T]
[G] [H] [H] [M] [N] [F]
[J] [N] [D] [F] [J] [H] [B]
[Q] [F] [W] [S] [V] [N] [F] [N]
[W] [N] [H] [M] [L] [B] [R] [T] [Q]
[L] [T] [C] [R] [R] [J] [W] [Z] [L]
[S] [J] [S] [T] [T] [M] [D] [B] [H]
1 2 3 4 5 6 7 8 9
move 5 from 4 to 5
move 2 from 5 to 8
move 2 from 9 to 1
move 2 from 9 to 1
move 1 from 5 to 3
move 10 from 5 to 8
move 1 from 4 to 7
move 1 from 1 to 2
move 5 from 3 to 7
move 1 from 2 to 8
move 21 from 8 to 5
move 13 from 5 to 7
move 2 from 9 to 4
move 1 from 7 to 4
move 5 from 1 to 4
move 1 from 5 to 7
move 2 from 2 to 7
move 1 from 3 to 2
move 1 from 1 to 6
move 7 from 5 to 9
move 16 from 7 to 4
move 7 from 9 to 3
move 1 from 7 to 5
move 1 from 3 to 8
move 3 from 2 to 7
move 1 from 8 to 9
move 3 from 3 to 6
move 21 from 4 to 9
move 1 from 5 to 7
move 4 from 4 to 9
move 8 from 6 to 3
move 6 from 7 to 1
move 12 from 9 to 8
move 6 from 7 to 2
move 3 from 6 to 5
move 1 from 6 to 9
move 4 from 8 to 6
move 3 from 8 to 5
move 4 from 1 to 8
move 4 from 6 to 1
move 2 from 1 to 3
move 1 from 5 to 8
move 2 from 2 to 8
move 5 from 8 to 3
move 4 from 2 to 7
move 5 from 8 to 1
move 2 from 1 to 7
move 1 from 8 to 2
move 2 from 1 to 7
move 11 from 9 to 2
move 1 from 8 to 5
move 2 from 9 to 4
move 3 from 9 to 5
move 2 from 5 to 1
move 6 from 5 to 8
move 2 from 4 to 2
move 1 from 5 to 6
move 7 from 1 to 8
move 2 from 2 to 7
move 13 from 8 to 1
move 16 from 3 to 1
move 3 from 2 to 1
move 12 from 7 to 6
move 15 from 1 to 8
move 2 from 3 to 8
move 16 from 1 to 2
move 24 from 2 to 8
move 1 from 1 to 5
move 1 from 5 to 8
move 3 from 6 to 7
move 26 from 8 to 3
move 20 from 3 to 9
move 1 from 2 to 9
move 16 from 9 to 3
move 14 from 3 to 1
move 13 from 1 to 6
move 3 from 3 to 4
move 3 from 9 to 4
move 1 from 7 to 8
move 5 from 8 to 2
move 8 from 8 to 5
move 18 from 6 to 1
move 4 from 8 to 5
move 6 from 4 to 1
move 2 from 2 to 5
move 5 from 3 to 8
move 5 from 8 to 7
move 2 from 5 to 8
move 5 from 5 to 4
move 3 from 2 to 8
move 22 from 1 to 2
move 1 from 1 to 2
move 5 from 8 to 2
move 2 from 5 to 2
move 1 from 1 to 6
move 5 from 5 to 2
move 1 from 9 to 8
move 5 from 4 to 1
move 6 from 6 to 9
move 3 from 1 to 9
move 1 from 1 to 7
move 8 from 9 to 6
move 6 from 7 to 1
move 5 from 6 to 5
move 27 from 2 to 1
move 4 from 5 to 7
move 9 from 1 to 5
move 1 from 9 to 1
move 3 from 6 to 2
move 9 from 2 to 1
move 2 from 7 to 2
move 1 from 8 to 7
move 10 from 5 to 9
move 1 from 9 to 7
move 25 from 1 to 8
move 6 from 7 to 4
move 11 from 1 to 7
move 3 from 8 to 1
move 3 from 2 to 6
move 3 from 8 to 9
move 11 from 8 to 6
move 1 from 2 to 6
move 12 from 6 to 4
move 13 from 4 to 5
move 1 from 6 to 1
move 3 from 7 to 5
move 5 from 8 to 7
move 1 from 7 to 1
move 5 from 1 to 6
move 3 from 6 to 4
move 3 from 8 to 6
move 2 from 5 to 2
move 12 from 5 to 9
move 5 from 6 to 2
move 2 from 5 to 9
move 6 from 4 to 9
move 11 from 7 to 3
move 1 from 2 to 5
move 1 from 7 to 8
move 1 from 5 to 7
move 1 from 7 to 1
move 1 from 8 to 1
move 2 from 4 to 7
move 2 from 6 to 8
move 5 from 3 to 6
move 2 from 7 to 2
move 2 from 2 to 9
move 1 from 2 to 9
move 1 from 1 to 6
move 35 from 9 to 7
move 2 from 8 to 7
move 3 from 3 to 8
move 5 from 2 to 4
move 3 from 3 to 7
move 2 from 4 to 7
move 4 from 6 to 5
move 4 from 5 to 9
move 3 from 4 to 5
move 1 from 8 to 3
move 4 from 9 to 8
move 1 from 9 to 6
move 38 from 7 to 2
move 1 from 3 to 5
move 1 from 1 to 7
move 4 from 7 to 3
move 3 from 6 to 1
move 22 from 2 to 7
move 1 from 5 to 8
move 7 from 8 to 4
move 8 from 2 to 8
move 3 from 5 to 1
move 4 from 3 to 9
move 1 from 8 to 3
move 1 from 3 to 7
move 2 from 2 to 3
move 5 from 8 to 9
move 3 from 9 to 1
move 2 from 1 to 7
move 6 from 2 to 3
move 6 from 3 to 1
move 2 from 3 to 6
move 1 from 6 to 1
move 14 from 7 to 2
move 4 from 1 to 6
move 8 from 1 to 3
move 4 from 3 to 6
move 3 from 9 to 5
move 1 from 8 to 6
move 1 from 8 to 4
move 9 from 7 to 1
move 8 from 2 to 4
move 4 from 2 to 9
move 2 from 2 to 1
move 3 from 5 to 8
move 1 from 8 to 6
move 1 from 7 to 8
move 1 from 6 to 5
move 3 from 9 to 5
move 2 from 9 to 5
move 4 from 3 to 9
move 3 from 6 to 3
move 3 from 6 to 9
move 9 from 4 to 1
move 1 from 9 to 8
move 3 from 3 to 6
move 2 from 7 to 4
move 4 from 8 to 5
move 7 from 5 to 6
move 19 from 1 to 9
move 5 from 9 to 3
move 2 from 1 to 6
move 1 from 4 to 6
move 4 from 3 to 2
move 21 from 9 to 7
move 1 from 1 to 2
move 1 from 9 to 1
move 1 from 1 to 8
move 16 from 7 to 6
move 24 from 6 to 5
move 7 from 4 to 5
move 1 from 8 to 3
move 2 from 2 to 8
move 31 from 5 to 8
move 1 from 4 to 6
move 2 from 6 to 9
move 1 from 7 to 4
move 3 from 7 to 9
move 1 from 4 to 8
move 2 from 3 to 5
move 1 from 2 to 3
move 1 from 3 to 7
move 1 from 7 to 9
move 24 from 8 to 6
move 1 from 8 to 1
move 30 from 6 to 1
move 2 from 5 to 2
move 1 from 6 to 9
move 6 from 9 to 7
move 1 from 6 to 4
move 1 from 4 to 6
move 23 from 1 to 3
move 21 from 3 to 4
move 4 from 2 to 6
move 3 from 6 to 1
move 1 from 5 to 1
move 4 from 1 to 9
move 3 from 9 to 6
move 8 from 1 to 6
move 4 from 8 to 5
move 2 from 7 to 5
move 7 from 4 to 3
move 3 from 4 to 9
move 9 from 3 to 9
move 1 from 7 to 6
move 6 from 5 to 8
move 14 from 6 to 2
move 4 from 8 to 4
move 7 from 4 to 5
move 1 from 7 to 9
move 6 from 4 to 3
move 13 from 2 to 6
move 5 from 3 to 7
move 1 from 3 to 8
move 1 from 8 to 2
move 4 from 8 to 3
move 6 from 6 to 4
move 2 from 2 to 8
move 5 from 4 to 7
move 3 from 7 to 5
move 1 from 7 to 9
move 2 from 3 to 9
move 3 from 7 to 3
move 1 from 7 to 9
move 1 from 7 to 9
move 3 from 4 to 1
move 6 from 6 to 1
move 2 from 7 to 5
move 1 from 3 to 5
move 11 from 9 to 4
move 9 from 4 to 5
move 3 from 3 to 4
move 1 from 3 to 9
move 2 from 8 to 1
move 9 from 1 to 8
move 22 from 5 to 8
move 2 from 1 to 3
move 3 from 4 to 6
move 14 from 8 to 9
move 1 from 3 to 9
move 19 from 9 to 3
move 3 from 9 to 4
move 2 from 7 to 2
move 1 from 4 to 6
move 1 from 3 to 8
move 8 from 3 to 1
move 2 from 9 to 6
move 1 from 2 to 5
move 3 from 4 to 9
move 1 from 2 to 3
move 20 from 8 to 3
move 4 from 9 to 5
move 1 from 4 to 2
move 26 from 3 to 5
move 1 from 8 to 3
move 8 from 1 to 4
move 1 from 3 to 7
move 1 from 2 to 1
move 1 from 1 to 6
move 1 from 6 to 7
move 4 from 5 to 3
move 3 from 4 to 2
move 5 from 5 to 3
move 2 from 2 to 6
move 3 from 3 to 5
move 2 from 4 to 8
move 5 from 3 to 9
move 5 from 9 to 8
move 19 from 5 to 9
move 1 from 5 to 2
move 2 from 7 to 1
move 1 from 1 to 7
move 1 from 7 to 4
move 13 from 9 to 3
move 8 from 6 to 2
move 10 from 3 to 5
move 14 from 5 to 4
move 7 from 8 to 4
move 1 from 6 to 2
move 6 from 3 to 8
move 4 from 9 to 7
move 2 from 9 to 8
move 1 from 7 to 1
move 3 from 2 to 7
move 1 from 5 to 3
move 7 from 8 to 6
move 5 from 6 to 2
move 8 from 4 to 5
move 3 from 5 to 8
move 3 from 8 to 6
move 5 from 7 to 9
move 5 from 3 to 6
move 1 from 9 to 4
move 17 from 4 to 7
move 1 from 8 to 1
move 12 from 7 to 8
move 3 from 1 to 4
move 2 from 4 to 6
move 8 from 6 to 1
move 4 from 6 to 3
move 1 from 7 to 8
move 5 from 5 to 8
move 4 from 7 to 1
move 3 from 2 to 6
move 2 from 5 to 1
move 6 from 1 to 6
move 4 from 3 to 5
move 4 from 5 to 3
move 1 from 4 to 8
move 3 from 3 to 2
move 17 from 8 to 4
move 6 from 6 to 3
move 14 from 4 to 9
move 1 from 3 to 8
move 1 from 7 to 4
move 3 from 8 to 3
move 5 from 2 to 5
move 6 from 1 to 7
move 2 from 6 to 4
move 4 from 5 to 7
move 1 from 1 to 5
move 1 from 6 to 3
move 10 from 7 to 4
move 1 from 5 to 4
move 1 from 2 to 3
move 15 from 4 to 5
move 3 from 3 to 1
move 6 from 2 to 6
move 1 from 2 to 3
move 2 from 4 to 7
move 2 from 7 to 8
move 1 from 4 to 2
move 2 from 1 to 7
move 1 from 7 to 2
move 12 from 9 to 1
move 4 from 9 to 5
move 4 from 6 to 2
move 1 from 7 to 3
move 6 from 2 to 4
move 1 from 8 to 5
move 2 from 4 to 2
move 11 from 1 to 7
move 3 from 1 to 4
move 17 from 5 to 6
move 15 from 6 to 4
move 1 from 8 to 9
move 10 from 4 to 1
move 1 from 3 to 9
move 2 from 6 to 5
move 1 from 2 to 6
move 4 from 5 to 6
move 4 from 1 to 2
move 6 from 6 to 7
move 2 from 2 to 6
move 9 from 4 to 9
move 6 from 1 to 2
move 3 from 4 to 1
move 10 from 9 to 8
move 4 from 2 to 1
move 1 from 1 to 2
move 5 from 8 to 6
move 1 from 2 to 7
move 1 from 9 to 4
move 2 from 6 to 9
move 13 from 7 to 2
move 5 from 7 to 5
move 2 from 5 to 2
move 1 from 4 to 5
move 4 from 8 to 4
move 17 from 2 to 6
move 3 from 4 to 6
move 2 from 9 to 1
move 7 from 6 to 8
move 1 from 5 to 2
move 1 from 4 to 1
move 2 from 9 to 4
move 1 from 3 to 9
move 4 from 3 to 7
move 2 from 8 to 5
move 3 from 7 to 5
move 10 from 5 to 8
move 2 from 2 to 4
move 6 from 1 to 2
move 4 from 6 to 3
move 8 from 2 to 6
move 1 from 7 to 4
move 5 from 4 to 5
move 7 from 6 to 7
move 5 from 3 to 5
move 5 from 5 to 2
move 4 from 8 to 1
move 6 from 1 to 6
move 3 from 3 to 2
move 22 from 6 to 2
move 1 from 9 to 7
move 8 from 8 to 6
move 1 from 7 to 6
move 2 from 5 to 7
move 4 from 8 to 5
move 7 from 6 to 7
move 2 from 6 to 4
move 14 from 2 to 1
move 7 from 1 to 3
move 12 from 7 to 3
move 1 from 4 to 3
move 2 from 5 to 8
move 2 from 8 to 1
move 1 from 4 to 3
move 6 from 2 to 9
move 6 from 9 to 2
move 2 from 2 to 7
move 6 from 7 to 5
move 13 from 3 to 5
move 5 from 2 to 6
move 5 from 6 to 1
move 2 from 3 to 6
move 1 from 6 to 5
move 1 from 6 to 1
move 3 from 1 to 9
move 6 from 2 to 7
move 1 from 2 to 3
move 24 from 5 to 2
move 7 from 3 to 7
move 13 from 7 to 9
move 4 from 1 to 9
move 4 from 1 to 6
move 1 from 5 to 6
move 16 from 9 to 5
move 1 from 6 to 4
move 1 from 5 to 2
move 5 from 1 to 3
move 11 from 2 to 1
move 4 from 9 to 6
move 1 from 4 to 7
move 2 from 3 to 4
move 6 from 6 to 9
move 1 from 1 to 3
move 2 from 9 to 4
move 1 from 7 to 9
move 4 from 2 to 9
move 8 from 9 to 2
move 3 from 3 to 2
move 1 from 9 to 4
move 5 from 1 to 7
move 1 from 4 to 8
move 2 from 1 to 9
move 1 from 8 to 7
move 6 from 5 to 3
move 1 from 5 to 1
move 5 from 2 to 3
move 4 from 1 to 5
move 4 from 7 to 1
move 8 from 5 to 8

1
2022/input/day06.txt Executable file
View File

@ -0,0 +1 @@
rnttlvtttmnmpmhpmmzvmmhpmmnrntnnsnrnndvnddmbbtptssjcczmmbwmbmwwmmflfggwzzhjjgppwrwdwqwbbbmwbwgggqccmlmdmgdgqgpqgpgzzndddgdbbsvvfsfppwjwzjjcnjccwrwrprgppbpddnccjggfrggqngqgdqgddvsshqhmqqfvqvlvwvnwwmccrpcczvcczzgsgwwlggqsggmdgglblpblltbbzrbzzcscgccwssbddsmddzzvvhjjwjwrjwrwfftmttplldnllqttdhddmvdmmhsmsmqsqwqpqbpqppqdppbnpnhhppjbbrzrbrggdbddnrdrzrdrpdppzzfzrzgzllwlwzlllwtltslttlmtmqqbbzmzhmmjjwrjrrnzrzhzmmtbbrppmmfddjhdjdsdllrbbpfphpzpwwjvwwdpwpzwwzrrfnfwwfbbpttzjtjptpvvsfvvljjrzzmdmsddpvpgvvdcdwcwhwghwggrjjhdjhddzhhwbhwbwwbrwwrfrmfrfvrfftzzqrzqrqtrqtrqtrqttgctclcwlwwvqqnlnqlnnndmnmggznzhzqzggzrzbbrwbrwrvvscvvmvhmvvbsbjsjfjpfpqpbbbbjtjptpprnndpnnrsswffmnnjhhpqhqwhhbwhbhgbbfrbrjjpmjmzzdzjdjffcvvtwttrrcscvcncqcfcwwpgpfptpcpspllcfcssgbglgdllcdcmcpcqclctcvtvvbhbbdhhczhzshzhbhchzchchlcllwswqqmpmbbqwwzgzhggczgzffsgggjjlcjjpnjjfqjfjvfvrfrlrvvpfpcfcbffnmfmbmbgggplldblddvcvjcvvmbmmmrvrvvnhhqttgdgvdgdqgddcsclljplpbbsvbsbfsffshsgslsllzztggttfccctwtswwrvwvnnfbnnvjnvvlgvvfppmvpmmvpmmcttjffgsffcllncntcncfclcgcppvdvpvrvbrrnnvrvhrvrzvrvnrvrdrnrqrnngsshqhcqqbfqqzmmmzjzqztznttjffqzfqfbqqsggclgclcddtqqdppbjppqvpqvvmqmqnqcnqcqcqvqjqzqbzzbrrgfgddgtgnnpjjbzjjbbtstzszwswggfffmnnpllfvvnwnpwpqwqwlllqlmmzcmcrcmrcmrcrffnrnssjjrdjjwwqgwqgqdggwzwjzzbfbpptctchthbbbqsqggrllldwdffvwwlrwwljwjnncmmjmvjmmqnnmrrgjgvvpqqbppqlpqllqnqdndwwlppcjcdjjcnnmddffgjjrwjrwjjhvhqvhhfssrtsrsgrrwjwhhbqqpzzrdrhrqhrqrprqqmllsszhzllcdllpmpbbmsmjjmwmssvlslwwmtwmmgvgcvggzcgzgddsjsjfsssftfcchhfmfcmcpcpvcppqggjddljjbttwrrhghqggpghgngzngznnzqnnssqlqmqbmmmqdqqhttnqqjhhphrhccbggmjjpwpbwpprdrvvjzvvtctzzmpphwpwdpdvvhbvvdvsdsjdjbdjdljjstswttnhhbfffmnngddlglqggvzvlzvzqqvbqbvbjbnjjsvjjwpjjzshrdtrjttvqnbltrfpvnztrwzrtgjpzgqdjfglqgjrgdzbhqpghdbfhlfjhbfjfjppfgljmgwljlsbmltgztthnzvdrgqlgddvqhzctdgcphfqvnpjjgzwqfvnhvzdrwtpgfdjpqnfshslqmplcprdntnhqqbqptwzvdddhcjcqrfhjqnjvpnhttblwgjwlfwntdchgfjmdbgtqtdgnzbqwzzcltwtmtqtdbvjtfvlzpcvgmrfqwfhwqmhvwhftzgmhshffnjwqbvztszsrrglqvhfpqmbnqjsfnwdwgdtmztbvqrmztfctmvptbwnfzfgdztjgnqsrsqqqnrpgzsqszzwwwgqnnnrdzhzdbqjgbvncprzcjqchzfgnclbrmphbsdwwpvwjwlbshhgjfbhjjtdqrmrcjfnrrhqrpsbglthzpvfglqspttdpwlljhnlrjpzchbrqcgtmcscjnwvpztfjdcwbnbgmbpgdthgnhbrtwftnscbsrndghbslflpcpjwbcjnhzcwdcslmzqbtrlnzmntlpjcsctnsqwtbffqlhfgcsflvfwnmczvsbflnnnzpfjfrcwhhcbtbjcbghtcwcgdbrwrgfgvpwtcwlwcmnmrtcrjbwtwlrfstztsghfvrfjzzpswpqfqpvqstvbhqfjlgmtdlhqrhwzqpnqpllnlgzwptbgftmblqcwfcllbwfzdhrndfrvdvwzqvhnghlzvhldnnvrgqvlpfdnpmcgddjmstzsqfvzwftflrwtzqwjbbqhjpfbdztdfsgsztvvrvslgspgpdcmwszdfsddqhpzpsjgqmgzqvhchlgrmcmzwzbtwfphvgcdmhfdczhffgmqpncdjszzgwfvwsqddvbcgngbjwhmphjsmjthvbthhfwdtqmjctcmdpqpsdrnrzdgzgzctbhwsgvtjgwjbsnnjmpmqgwrnqfqbpnrpddjsrsvmcshhthwfrwmqsrjhlsrgfzvwmdzhwrvchppqldghgzflrnwqnvntmtdwmrpgbdbzvcmnqstzntvllcgzsnvrhqzsfncznhgrggmvrmgsqmhbdsjbsqqhzppfcwdrgdvfjdscrvpwtsdmcnczwbbjhvddprwtzfwslcfdcrqfszcgmhtdfvlqzqtvwngzvmmqcrqpzwzhggjnphsrmnctnfhtppglspnvzrsqfgzdfrrwbzbqwvbvbnzgmdrqrnsvdpvlgcmnggsbmbtfwrvdjrtgtgcqscnfpgswgsngdqnnscffdcnlrcpdpcbpzvqcrtjhlwvgnfhhqmprthrtcvcjjwgprqqdwfbgmzlwttjpvcjzfwbdhvngsjpgtqsvbldbcvhjhzbjzblqtqhlnbzzqfcpnzdhbplztcgvzhbgshqbccgwzhftqvtwzbwmnfrsgphhhgtsmwlqhlcchtbtggqwmbdthhmqqjtfdvfpddfdrtfjbpmwtcbfnrhwcnpdqrdtsfdmjfzdwwgnftnwpssgqtlpdbwhnzcnfmppclsswbhcdghpnslwjznqszgdtrnpncsqsnbrplrfwpbnfnvttlzcjtvhzcpzmhfsfzfjlzqqnprpdvwbfthmrswqrcqqwrnwzmgjqqsnqdblssmhngjjvprmqbswtgzzvprwhrgjqshvmwzgrgfmzlgrtzbmdlzncwqdftfsndvdfmmplswdbjtbcbvcvtpjvrqpghczpqvvpqwfbfhllbpvrrfsmsjhqbldcrwvzvcvzfffqvplbfbdbwctbjsljlfwtbcnpsbtpmcqgdvltmztvrcfsprbnvwplwhncgsdnrdmqnmcvpvvrlmlwtgvrnwvzsfctdlcfvtgqmpnbwcbwvfpmqnbjvwffpjtvvgflhrnlngrzhhttdtdbvscftsqtvbrgzfgsjvwhzjcbsqcttlgwmhhvjhwhgmmdtflfdbvnhgcblqmwjfsqnngjqfbvdnsfbgwjfhpgdgvhpbvlrtpcpvhrbtqpbffzcqrvbqwvqmmrcwtnvcgwvsqzvrwdbcnjshbnbftmmvrmjvgfdwbsjvqfdwnnvqqhbmshcrclrwhfhbtnwqvmrrvdwgcwcsrhdbqndsthmrmbjhttjtzmlflbrmhlcgsbdjcjcvwcjffnqrntpflrgfcngpchtrzpnflwjvcgbwtsnjfqsggwmwhdvbzdpjmtwlmrslnjsndjtgjmmwmdgtnfrztppzqvqhbfzqpsdhvsshddlzwcmsndrpqhndsrjnngnmgmzrvchwlqgdnbssbhpbpgwpsrcnbphpslvqplhpgdhmrnwwjmhvnsfjmrfwtvjjrmgptvjffhbgpmfgmgrcjrwqhccssrqjpljbpwcvsfdtmbhzsmsjsgblgpcqszsttfclrjcnsslmngmbmwqfhddbvmbvwmrmvglsl

5
2022/readme.md Normal file
View File

@ -0,0 +1,5 @@
## Advent of Code 2022
These problems were actually solved in early 2024 as part of a Rust study group.
For the most part, each day's solution was left "as-is" and feedback from the study group was not incorporated until subsequent days, in an attempt to document my attempt to learn the language.

42
2022/src/day01.rs Normal file
View File

@ -0,0 +1,42 @@
use std::io;
use std::io::prelude::*;
use std::fs::File;
use sorted_vec::SortedVec;
fn read_lines(filename: &str) -> io::Result<io::Lines<io::BufReader<File>>> {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn update_max(max: &mut SortedVec<i32>, current: i32) {
if max.is_empty() {
max.insert(current);
return
}
if current <= max[0] { return }
max.insert(current);
if max.len() > 3 { max.remove_index(0); }
}
pub fn execute() -> Result<(), io::Error> {
let mut max: SortedVec<i32> = SortedVec::new();
let mut current = 0;
for line in read_lines("input/day01.txt")? {
let text = line?;
current = match text.parse::<i32>() {
Ok(value) => current + value,
Err(_) => {
update_max(&mut max, current);
0
},
}
}
if let Some(x) = max.iter().last() {
println!("Step 1 solution: {x}");
}
println!("Step 2 solution: {}", max.iter().fold(0i32, |sum, i| sum + (*i as i32)));
Ok(())
}

58
2022/src/day02.rs Normal file
View File

@ -0,0 +1,58 @@
use std::io;
use std::io::prelude::*;
use std::fs::File;
fn read_lines(filename: &str) -> io::Result<io::Lines<io::BufReader<File>>> {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
fn print_step(step: i8, value: u64) {
println!("Step {} solution: {}", step, value);
}
pub fn execute() -> Result<(), io::Error> {
let mut score1: u64 = 0;
let mut score2: u64 = 0;
for line in read_lines("input/day02.txt")? {
let text = line?;
let opp = parse_move(text.chars().nth(0).unwrap());
score1 += step1(opp, text.chars().nth(2).unwrap());
score2 += step2(opp, text.chars().nth(2).unwrap());
}
print_step(1, score1);
print_step(2, score2);
Ok(())
}
fn step1(opp: i8, text: char) -> u64 {
let me = parse_move(text);
return match me - opp {
1 | -2 => me + 6,
0 => me + 3,
-1 | 2 => me,
_ => panic!("Received impossible result in a match."),
} as u64;
}
fn step2(opp: i8, text: char) -> u64 {
return match text {
'X' => ((opp + 4) % 3) + 1,
'Y' => opp + 3,
'Z' => (opp % 3) + 7,
_ => panic!("Received impossible strategy code."),
} as u64;
}
// Parses the second command in the input incorrectly.
fn parse_move(x: char) -> i8 {
return match x {
'A' | 'X' => 1,
'B' | 'Y' => 2,
'C' | 'Z' => 3,
_ => 0,
};
}

53
2022/src/day03.rs Normal file
View File

@ -0,0 +1,53 @@
use std::io;
use std::vec::Vec;
use std::collections::HashSet;
use crate::utils;
pub fn execute() -> Result<(), io::Error> {
let mut score = 0;
let mut sacks: Vec<HashSet<u8>> = Vec::new();
for line in utils::read_lines("input/day03.txt")? {
let text = line?;
let (text1, text2) = text.split_at(text.len() / 2);
let mut pouch1 = HashSet::new();
let mut pouch2 = HashSet::new();
for i in 0..text1.len() {
pouch1.insert(text1.as_bytes()[i]);
pouch2.insert(text2.as_bytes()[i]);
}
let intersect: HashSet<_> = pouch1.intersection(&pouch2).cloned().collect();
score += get_score(&intersect);
sacks.push(pouch1.union(&pouch2).cloned().collect());
}
let mut score2 = 0;
for group in sacks.chunks(3) {
let mut intersect: HashSet<u8> = group[0].intersection(&group[1]).cloned().collect();
intersect = intersect.intersection(&group[2]).cloned().collect();
score2 += get_score(&intersect);
}
utils::print_step(1, &score);
utils::print_step(2, &score2);
Ok(())
}
fn get_score(intersect: &HashSet<u8>) -> u64 {
if intersect.len() > 1 {
panic!("More than one overlapping item.");
}
if let Some(item) = intersect.iter().next() {
if *item > 0x60 {
return (*item - 0x60) as u64;
}
return ((*item - 0x40) + 26) as u64;
}
panic!("No items in intersection.");
}

52
2022/src/day04.rs Normal file
View File

@ -0,0 +1,52 @@
use std::io;
use std::vec::Vec;
use crate::utils;
struct Assignment {
min: u32,
max: u32,
}
pub fn execute() -> Result<(), io::Error> {
let mut subsets = 0;
let mut overlaps = 0;
for line in utils::read_lines("input/day04.txt")? {
let text = line?;
let inputs: Vec<_> = text.split(',').collect();
let ass1 = parse_assignment(inputs[0]);
let ass2 = parse_assignment(inputs[1]);
if (ass2.max >= ass1.max && ass2.min <= ass1.min) ||
(ass1.max >= ass2.max && ass1.min <= ass2.min) {
subsets += 1;
}
if (ass2.max >= ass1.min && ass2.min <= ass1.max) ||
(ass1.max >= ass2.min && ass1.min <= ass2.max) {
overlaps += 1;
}
}
utils::print_step(1, &subsets);
utils::print_step(2, &overlaps);
Ok(())
}
fn parse_assignment(input: &str) -> Assignment {
let values: Vec<_> = input.split('-').collect();
let Ok(min) = values[0].parse::<u32>() else {
panic!("Min value {} is non-numeric.", values[0])
};
let Ok(max) = values[1].parse::<u32>() else {
panic!("Max value {} is non-numeric.", values[1])
};
Assignment {
min: min,
max: max,
}
}

63
2022/src/day05.rs Normal file
View File

@ -0,0 +1,63 @@
use std::io;
use std::vec::Vec;
use crate::utils;
pub fn execute() -> Result<(), io::Error> {
let mut stacks1: [Vec<char>; 9] = Default::default();
let mut stacks2: [Vec<char>; 9] = Default::default();
for line in utils::read_lines("input/day05.txt")? {
let text = line?;
match text {
x if x.contains("[") => {
initialize_stacks(&mut stacks1, &x);
initialize_stacks(&mut stacks2, &x);
},
x if x.contains("move") => {
move_stacks(&mut stacks1, &x, 1);
move_stacks(&mut stacks2, &x, 2);
},
_ => {},
};
}
utils::print_step(1, &stacks1.iter().map(|x| x.last().unwrap()).collect::<String>());
utils::print_step(1, &stacks2.iter().map(|x| x.last().unwrap()).collect::<String>());
Ok(())
}
fn initialize_stacks(stacks: &mut [Vec<char>], text: &str) {
let mut iter = text.chars();
iter.next(); // discard the first value
for (i, stack) in iter.step_by(4).zip(stacks) {
if i.is_alphabetic() { stack.insert(0, i); }
}
}
fn move_stacks(stacks: &mut [Vec<char>], text: &str, step: u8) {
let fields = text.split(' ').collect::<Vec<_>>();
let count = fields[1].parse::<usize>().unwrap();
let start = fields[3].parse::<usize>().unwrap();
let end = fields[5].parse::<usize>().unwrap();
let mut crane = vec![];
for _ in 0..count {
let item = stacks[start-1].pop().unwrap();
match step {
1 => stacks[end-1].push(item),
2 => crane.push(item),
_ => panic!("Bad step provided to function."),
}
}
// if we're doing step 1, we're done
if crane.is_empty() { return; }
// step 2 logic
while let Some(item) = crane.pop() {
stacks[end-1].push(item);
}
}

30
2022/src/day06.rs Normal file
View File

@ -0,0 +1,30 @@
use std::io;
use std::fs;
use std::collections::HashSet;
use crate::utils;
pub fn execute() -> Result<(), io::Error> {
let data = fs::read_to_string("input/day06.txt")?;
let packet_marker = find_uniq_index(&data, 4);
let message_marker = find_uniq_index(&data, 14);
utils::print_step(1, &packet_marker);
utils::print_step(2, &message_marker);
Ok(())
}
// Returns the (1-indexed) position where the first sequence of unique characters of `length`
// ends in `data`
fn find_uniq_index(data: &str, length: usize) -> usize {
for (i, substr) in data.as_bytes().windows(length).enumerate() {
let mut uniq = HashSet::new();
if substr.iter().all(|x| uniq.insert(x)) {
return i+length;
}
}
panic!("No unique string of length '{length}' found.")
}

74
2022/src/main.rs Normal file
View File

@ -0,0 +1,74 @@
use std::env;
use std::io;
mod utils;
const LATEST: u8 = 6;
mod day01;
mod day02;
mod day03;
mod day04;
mod day05;
mod day06;
// mod day07;
// mod day08;
// mod day09;
// mod day10;
// mod day11;
// mod day12;
// mod day13;
// mod day14;
// mod day15;
// mod day16;
// mod day17;
// mod day18;
// mod day19;
// mod day20;
// mod day21;
// mod day22;
// mod day23;
// mod day24;
// mod day25;
fn main() -> io::Result<()> {
let day = determine_day(LATEST);
match day {
1 => day01::execute(),
2 => day02::execute(),
3 => day03::execute(),
4 => day04::execute(),
5 => day05::execute(),
6 => day06::execute(),
// 7 => day07::execute(),
// 8 => day08::execute(),
// 9 => day09::execute(),
// 10 => day10::execute(),
// 11 => day11::execute(),
// 12 => day12::execute(),
// 13 => day13::execute(),
// 14 => day14::execute(),
// 15 => day15::execute(),
// 16 => day16::execute(),
// 17 => day17::execute(),
// 18 => day18::execute(),
// 19 => day19::execute(),
// 20 => day20::execute(),
// 21 => day21::execute(),
// 22 => day22::execute(),
// 23 => day23::execute(),
// 24 => day24::execute(),
// 25 => day25::execute(),
_ => panic!("Couldn't execute day {day}")
}
}
fn determine_day(latest: u8) -> u8 {
let mut day = latest;
for arg in env::args() {
if let Ok(x) = arg.parse::<u8>() {
day = x;
break;
}
}
return day;
}

11
2022/src/template.rs Normal file
View File

@ -0,0 +1,11 @@
use std::io;
use crate::utils;
pub fn execute() -> Result<(), io::Error> {
for line in utils::read_lines("input/dayXX.txt")? {
let text = line?;
}
Ok(())
}

14
2022/src/utils.rs Normal file
View File

@ -0,0 +1,14 @@
use std::io;
use std::io::prelude::*;
use std::fs::File;
use std::fmt::Display;
// Returns the contents of a file as an iterator over strings.
pub fn read_lines(filename: &str) -> io::Result<impl Iterator<Item = io::Result<String>>> {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
pub fn print_step<T: Display>(step: i8, value: &T) {
println!("Step {step} solution: {value}");
}