Skip to content

Commit

Permalink
day17: start solving part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
fxnn committed Dec 18, 2024
1 parent 62babd9 commit 06b845d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
25 changes: 25 additions & 0 deletions day18/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
5,4
4,2
4,5
3,0
2,1
6,3
2,4
1,5
0,6
3,3
2,6
5,1
1,2
5,5
2,5
6,5
1,4
0,4
6,4
1,1
6,1
1,0
0,5
1,6
2,0
57 changes: 57 additions & 0 deletions day18/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
"regexp"

"github.com/fxnn/adventofcode2024/util"
)

const (
CORRUPTED = -1
)

func printSpace(space [][]int) {
for y := range space {
for x := range space[y] {
fmt.Printf("%2d ", space[y][x])
}
fmt.Println()
}
fmt.Println()
}

func main() {
var size = flag.Int("max", 70, "coordinates have range [0,max]")
var count = flag.Int("count", 1024, "number of bytes that have fallen")
flag.Parse()
var space = make([][]int, *size+1)
for y := range space {
space[y] = make([]int, *size+1)
}

var scanner = bufio.NewScanner(os.Stdin)
var pattern = regexp.MustCompile(`^(\d+),(\d+)$`)

var fallen int
for scanner.Scan() && fallen < *count {
var line = scanner.Text()
var matches = pattern.FindStringSubmatch(line)
var x, y int
if len(matches) > 0 {
x = util.Atoi(matches[1])
y = util.Atoi(matches[2])
space[y][x] = CORRUPTED
fallen++
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}

printSpace(space)
}

0 comments on commit 06b845d

Please sign in to comment.