28 lines
1002 B
Forth
28 lines
1002 B
Forth
module advent_of_code.tasks.day_1.Star2
|
|
|
|
open System.IO
|
|
|
|
let stream = new StreamReader("tasks/day-1/input.txt")
|
|
|
|
let diff (line: string) =
|
|
let direction = if line.StartsWith("R") then 1 else -1
|
|
let count = (line.[1..] |> int)
|
|
// if (direction = -1) then
|
|
// printfn $"Turning left: {line} produces {count}"
|
|
direction * count
|
|
|
|
let solve (line: string) (start: int) =
|
|
let change = diff line
|
|
let changeMod100 = change % 100
|
|
let newPos = start + changeMod100
|
|
// printfn $"Line {line} produces change of {change} turns {changeMod100} moving from {start} to {newPos}"
|
|
let zeroesFound = (abs change / 100) + (if start <> 0 && (newPos <= 0 || newPos >= 100) then 1 else 0)
|
|
(newPos % 100 + (if newPos < 0 then 100 else 0), zeroesFound)
|
|
|
|
let rec main start zeroes =
|
|
let line = stream.ReadLine()
|
|
let newPos, newZeroes = solve line start
|
|
if stream.EndOfStream then
|
|
(newPos, zeroes + newZeroes)
|
|
else
|
|
main newPos (zeroes + newZeroes) |