Add day one solutions
This commit is contained in:
commit
76dee81b8c
11
advent-of-code/Program.fs
Normal file
11
advent-of-code/Program.fs
Normal 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
|
||||
23
advent-of-code/tasks/day-1/Star1.fs
Normal file
23
advent-of-code/tasks/day-1/Star1.fs
Normal 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)
|
||||
25
advent-of-code/tasks/day-1/Star2.fs
Normal file
25
advent-of-code/tasks/day-1/Star2.fs
Normal file
@ -0,0 +1,25 @@
|
||||
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)
|
||||
direction * count
|
||||
|
||||
let solve (line: string) (start: int) =
|
||||
let change = diff line
|
||||
let changeMod100 = change % 100
|
||||
let newPos = start + changeMod100
|
||||
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)
|
||||
Loading…
x
Reference in New Issue
Block a user