Add day 3
This commit is contained in:
parent
39551c066c
commit
4dd6175bcf
@ -12,4 +12,8 @@ let main _ =
|
||||
printfn $"{day2Star1}"
|
||||
let day2Star2 = day_2.Star2.main
|
||||
printfn $"{day2Star2}"
|
||||
let day3Star1 = day_3.Star1.main
|
||||
printfn $"{day3Star1}"
|
||||
let day3Star2 = day_3.Star2.main
|
||||
printfn $"{day3Star2}"
|
||||
0
|
||||
|
||||
23
advent-of-code/tasks/day-3/Star1.fs
Normal file
23
advent-of-code/tasks/day-3/Star1.fs
Normal file
@ -0,0 +1,23 @@
|
||||
module advent_of_code.tasks.day_3.Star1
|
||||
|
||||
open System.IO
|
||||
|
||||
let stream = new StreamReader("tasks/day-3/input.txt")
|
||||
|
||||
let getJoltage (line: string) =
|
||||
let firstDigit = List.max(Seq.toList(line[0 .. line.Length - 2]))
|
||||
let firstDigitPos = line.IndexOf firstDigit
|
||||
let secondDigit = List.max(Seq.toList(line[firstDigitPos + 1 ..]))
|
||||
$"{firstDigit}{secondDigit}" |> int
|
||||
|
||||
let rec mainLoop (lines: string list) =
|
||||
let line, tail = lines.Head, lines.Tail
|
||||
let joltage = getJoltage line
|
||||
if (tail.IsEmpty) then
|
||||
joltage
|
||||
else
|
||||
joltage + mainLoop tail
|
||||
|
||||
let rec main =
|
||||
let lines = stream.ReadToEnd().Split("\n") |> Array.toList
|
||||
mainLoop lines
|
||||
25
advent-of-code/tasks/day-3/Star2.fs
Normal file
25
advent-of-code/tasks/day-3/Star2.fs
Normal file
@ -0,0 +1,25 @@
|
||||
module advent_of_code.tasks.day_3.Star2
|
||||
|
||||
open System.IO
|
||||
|
||||
let stream = new StreamReader("tasks/day-3/input.txt")
|
||||
|
||||
let rec getJoltage (line: string) (count: int) =
|
||||
let firstDigit = List.max(Seq.toList(line[0 .. line.Length - (count)]))
|
||||
let firstDigitPos = line.IndexOf firstDigit
|
||||
if count = 1 then
|
||||
$"{firstDigit}" |> int64
|
||||
else
|
||||
$"{firstDigit}{getJoltage line[firstDigitPos + 1 ..] (count - 1) }" |> int64
|
||||
|
||||
let rec mainLoop (lines: string list) =
|
||||
let line, tail = lines.Head, lines.Tail
|
||||
let joltage = getJoltage line 12
|
||||
if (tail.IsEmpty) then
|
||||
joltage
|
||||
else
|
||||
joltage + mainLoop tail
|
||||
|
||||
let rec main =
|
||||
let lines = stream.ReadToEnd().Split("\n") |> Array.toList
|
||||
mainLoop lines
|
||||
Loading…
x
Reference in New Issue
Block a user