38 lines
1.1 KiB
Forth
38 lines
1.1 KiB
Forth
module advent_of_code.tasks.day_4.Star1
|
|
|
|
open System
|
|
open System.IO
|
|
|
|
let stream = new StreamReader("tasks/day-4/input.txt")
|
|
|
|
let getPos (grid: string list) (pos: int * int) =
|
|
let y, x = pos.Deconstruct()
|
|
if (y < 0 || y >= grid.Length || x < 0 || x >= grid[y].Length) then
|
|
0
|
|
else
|
|
if grid[y][x] = '@' then 1 else 0
|
|
|
|
let isMovable (grid: string list) (y: int) (x: int) =
|
|
if (grid[y][x] = '.') then
|
|
false
|
|
else
|
|
let posList = [
|
|
(y - 1, x - 1); (y - 1, x); (y - 1, x + 1)
|
|
(y, x - 1); (y, x + 1)
|
|
(y + 1, x - 1); (y + 1, x); (y + 1, x + 1)
|
|
]
|
|
let adjacentList = posList |> List.map (getPos grid)
|
|
List.sum adjacentList < 4
|
|
|
|
let rec countRow (grid: string list) (y: int) (x: int) =
|
|
if x = grid[y].Length then 0 else
|
|
(if (isMovable grid y x) then 1 else 0) + (countRow grid y (x + 1))
|
|
|
|
let rec countGrid (grid: string list) (y: int) =
|
|
if y = grid.Length then 0 else
|
|
(countRow grid y 0) + (countGrid grid (y + 1))
|
|
|
|
let main =
|
|
let grid = stream.ReadToEnd().Split('\n') |> Array.toList
|
|
countGrid grid 0
|