Add day one solutions

This commit is contained in:
Malachy Byrne 2025-12-01 16:44:44 +00:00
commit cb4706e896
Signed by: malmal200
GPG Key ID: 166F016E1182B99F
3 changed files with 62 additions and 0 deletions

11
advent-of-code/Program.fs Normal file
View File

@ -0,0 +1,11 @@
module advent_of_code.Program
open advent_of_code.tasks.day_1
[<EntryPoint>]
let main _ =
let _, day1Star1 = Star1.main 50 0
printfn $"{day1Star1}"
let _, day1Star2 = Star2.main 50 0
printfn $"{day1Star2}"
0

View File

@ -0,0 +1,23 @@
module advent_of_code.tasks.day_1.Star1
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)
direction * count
let solve (line: string) (start: int) =
let change = diff line
let newPos = (start + change) % 100
(newPos, if newPos = 0 then 1 else 0)
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)

View File

@ -0,0 +1,28 @@
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)