Compare commits

...

2 Commits

Author SHA1 Message Date
7ecffd293b day6 makes me wanna do it in python lol 2024-12-20 01:07:14 -05:00
3065fc3966 day5 part2 completed 2024-12-20 00:04:04 -05:00
3 changed files with 184 additions and 2 deletions

View File

@ -14,7 +14,7 @@ func main() {
inputFile := os.Args[1]
part1, part2 := solution(inputFile)
fmt.Printf("(%s) Part 1: %d\n(%s) Part 2: %d\n:", inputFile, part1, inputFile, part2)
fmt.Printf("(%s) Part 1: %d\n(%s) Part 2: %d\n", inputFile, part1, inputFile, part2)
}
type Rule struct {
@ -86,10 +86,61 @@ func solution(inputFile string) (int, int) {
if validUpdate {
validSum += updatePages[len(updatePages)/2]
} else {
invalidSum += reorderedMid(updatePages, rules)
}
}
return validSum, invalidSum
}
func reorderedMid(pages []int, rules []Rule) int {
corrected := false
for !corrected {
for i := 0; i < len(pages); i++ {
for j := 1; j < len(pages); j++ {
if i == j {
continue
}
if i < j {
valid := isValid(rules, pages[i], pages[j])
if !valid {
pages[i], pages[j] = pages[j], pages[i]
}
} else {
valid := isValid(rules, pages[j], pages[i])
if !valid {
pages[i], pages[j] = pages[j], pages[i]
}
}
}
}
return validSum, invalidSum
corrected = wasCorrected(pages, rules)
}
fmt.Printf("Corrected %v\n", pages)
return pages[len(pages)/2]
}
func wasCorrected(pages []int, rules []Rule) bool {
for i := 0; i < len(pages); i++ {
for j := 1; j < len(pages); j++ {
if i == j {
continue
}
if i < j {
valid := isValid(rules, pages[i], pages[j])
if !valid {
return false
}
} else {
valid := isValid(rules, pages[j], pages[i])
if !valid {
return false
}
}
}
}
return true
}
func isValid(rules []Rule, left, right int) bool {

127
day6/main.go Normal file
View File

@ -0,0 +1,127 @@
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) < 2 {
panic("Provide input file")
}
inputFile := os.Args[1]
part1, part2 := solution(inputFile)
fmt.Printf("(%s) Part 1: %d\n(%s) Part 2: %d\n", inputFile, part1, inputFile, part2)
}
type Patroller struct {
x, y int
direction int
walkMap [][]bool
height int
width int
}
func solution(inputFile string) (int, int) {
var part2 int
// Read line by line
input, err := os.Open(inputFile)
if err != nil {
panic(err)
}
defer input.Close()
// patrolMap is a 2D bool array to keep track of the patrol
var pX, pY int
patrolMap := make([][]bool, 0)
for {
var line string
_, err := fmt.Fscanf(input, "%s\n", &line)
if err != nil {
break
}
// Add the line to the patrolMap
patrolMap = append(patrolMap, make([]bool, len(line)))
for i, c := range line {
if c == '#' {
patrolMap[len(patrolMap)-1][i] = false
}
if c == '.' {
patrolMap[len(patrolMap)-1][i] = true
}
if c == '^' {
pX = i
pY = len(patrolMap)
patrolMap[len(patrolMap)-1][i] = true
}
}
}
fmt.Println(pX+1, pY+1)
steps := 0
escaped := false
p := NewPatroller(pX, pY, 0, patrolMap)
for {
steps++
switch p.direction {
case 0: // NORTH
if p.y-1 < 0 {
escaped = true
break
} else if !p.walkMap[p.x][p.y-1] {
p.turnRight()
break
}
p.y--
case 1: // EAST
if p.x+1 >= len(p.walkMap[0]) {
escaped = true
break
}
if !p.walkMap[p.x+1][p.y] {
p.turnRight()
break
}
p.x++
case 2: // SOUTH
if p.y+1 >= len(p.walkMap) {
escaped = true
break
}
if !p.walkMap[p.x][p.y+1] {
p.turnRight()
break
}
p.y++
case 3: // WEST
if p.x-1 < 0 {
escaped = true
break
}
if !p.walkMap[p.x-1][p.y] {
p.turnRight()
break
}
p.x--
}
if escaped {
break
}
}
return steps, part2
}
func NewPatroller(x, y, direction int, walkMap [][]bool) *Patroller {
return &Patroller{
x: x,
y: y,
direction: direction,
walkMap: walkMap,
}
}
func (p *Patroller) turnRight() {
p.direction = (p.direction + 1) % 4
}

4
day6/run Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
go build -o ./bin/solution ./main.go
./bin/solution "$1"