Compare commits
No commits in common. "7ecffd293ba031decaacf1b19dea50676b70f7a1" and "e51c8425e5c77abc40124a4711d4e8a2ab4cfc94" have entirely different histories.
7ecffd293b
...
e51c8425e5
55
day5/main.go
55
day5/main.go
@ -14,7 +14,7 @@ func main() {
|
|||||||
inputFile := os.Args[1]
|
inputFile := os.Args[1]
|
||||||
|
|
||||||
part1, part2 := solution(inputFile)
|
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 {
|
type Rule struct {
|
||||||
@ -86,63 +86,12 @@ func solution(inputFile string) (int, int) {
|
|||||||
|
|
||||||
if validUpdate {
|
if validUpdate {
|
||||||
validSum += updatePages[len(updatePages)/2]
|
validSum += updatePages[len(updatePages)/2]
|
||||||
} else {
|
|
||||||
invalidSum += reorderedMid(updatePages, rules)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return validSum, invalidSum
|
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]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
func isValid(rules []Rule, left, right int) bool {
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
if rule.Left == right && rule.Right == left {
|
if rule.Left == right && rule.Right == left {
|
||||||
|
|||||||
127
day6/main.go
127
day6/main.go
@ -1,127 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user