package main import "core:os" import "core:fmt" Pos :: [2]int; Slope :: [2]int; advancePos :: proc(pos: ^Pos, slope: Slope, maxPos: ^Pos) -> bool { if pos.y + slope.y >= maxPos.y do return true; pos^ = pos^ + slope; if pos.x > maxPos.x do pos.x = (pos.x % maxPos.x) - 1; //if pos.y > maxPos.y do pos.y = (pos.y % maxPos.y) - 1; return false; } Tile :: enum { Open, Tree, } Map :: struct { tiles: [dynamic][dynamic]Tile, } loadMap :: proc(s: string) -> (m: Map) { m.tiles = make([dynamic][dynamic]Tile, 0); lineIndex := 0; row := make([dynamic]Tile, 0); append(&m.tiles, row); rowPtr := &m.tiles[lineIndex]; for r in s { if r == '\n' { lineIndex += 1; row = make([dynamic]Tile, 0); append(&m.tiles, row); rowPtr = &m.tiles[lineIndex]; } else if r == '#' do append(rowPtr, Tile.Tree); else if r == '.' do append(rowPtr, Tile.Open); } return; } main :: proc() { input, err := os.read_entire_file("input.txt"); inputStr := string(input); fileMap := loadMap(inputStr); for row in fileMap.tiles { for col in row { if col == .Open do fmt.printf("."); else if col == .Tree do fmt.printf("#"); } fmt.println(""); } count1 := checkSlope(&fileMap, Slope { 1, 1 }); count2 := checkSlope(&fileMap, Slope { 3, 1 }); count3 := checkSlope(&fileMap, Slope { 5, 1 }); count4 := checkSlope(&fileMap, Slope { 7, 1 }); count5 := checkSlope(&fileMap, Slope { 1, 2 }); part1 := count2; part2 := count1 * count2 * count3 * count4 * count5; fmt.println(part1, part2); } checkSlope :: proc(fileMap: ^Map, slope: Slope) -> (count: int = 0) { currentPos := Pos { 0, 0 }; maxPos := Pos { len(fileMap.tiles[0]) - 1, len(fileMap.tiles) - 1 }; //fmt.println("Max: ", maxPos); for !advancePos(¤tPos, slope, &maxPos) { //fmt.println(currentPos); if fileMap.tiles[currentPos.y][currentPos.x] == .Tree do count += 1; } return count; }