Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoVIII committed Jul 29, 2024
1 parent 45e1bf1 commit e58e825
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 2023/solutions/fsharp/05/src/Day05.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Types.fs" />
<Compile Include="Functions.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
115 changes: 115 additions & 0 deletions 2023/solutions/fsharp/05/src/Functions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
namespace Day05

open System

[<RequireQualifiedAccess>]
module String =
let split (separator: string) (input: string) =
input.Split(separator, StringSplitOptions.RemoveEmptyEntries)

let trim (input: string) = input.Trim()

[<AutoOpen>]
module Functions =
let parseInput (input: string) =
let parseSeeds input =
let parts = String.split ":" input

parts[1]
|> String.trim
|> String.split " "
|> Array.map (uint >> Seed)
|> List.ofArray

let parseMap keyConstr valueConstr input =
let parseLine input =
input |> String.trim |> String.split " " |> Array.map uint

input
|> String.trim
|> String.split "\n"
|> List.ofArray
|> List.tail
|> List.map parseLine
|> List.collect (fun values ->
let valueStart = values[0]
let keyStart = values[1]
let range = values[2]

[
for i in 0u .. range - 1u do
keyConstr (keyStart + i), valueConstr (valueStart + i)
])
|> Map.ofList

let blocks = String.split "\n\n" input

parseSeeds blocks[0],
{
seedToSoil = parseMap Seed Soil blocks[1]
soilToFertilizer = parseMap Soil Fertilizer blocks[2]
fertilizerToWater = parseMap Fertilizer Water blocks[3]
waterToLight = parseMap Water Light blocks[4]
lightToTemperature = parseMap Light Temperature blocks[5]
temperatureToHumidity = parseMap Temperature Humidity blocks[6]
humidityToLocation = parseMap Humidity Location blocks[7]
}

let lookupSoil maps seed =
match Map.tryFind seed maps.seedToSoil with
| Some soil -> soil
| None ->
let (Seed value) = seed
Soil value

let lookupFertilizer maps soil =
match Map.tryFind soil maps.soilToFertilizer with
| Some fertilizer -> fertilizer
| None ->
let (Soil value) = soil
Fertilizer value

let lookupWater maps fertilizer =
match Map.tryFind fertilizer maps.fertilizerToWater with
| Some water -> water
| None ->
let (Fertilizer value) = fertilizer
Water value

let lookupLight maps water =
match Map.tryFind water maps.waterToLight with
| Some light -> light
| None ->
let (Water value) = water
Light value

let lookupTemperature maps light =
match Map.tryFind light maps.lightToTemperature with
| Some temperature -> temperature
| None ->
let (Light value) = light
Temperature value

let lookupHumidity maps temperature =
match Map.tryFind temperature maps.temperatureToHumidity with
| Some humidity -> humidity
| None ->
let (Temperature value) = temperature
Humidity value

let lookupLocation maps humidity =
match Map.tryFind humidity maps.humidityToLocation with
| Some location -> location
| None ->
let (Humidity value) = humidity
Location value

let lookupLocationBySeed maps seed =
seed
|> lookupSoil maps
|> lookupFertilizer maps
|> lookupWater maps
|> lookupLight maps
|> lookupTemperature maps
|> lookupHumidity maps
|> lookupLocation maps
11 changes: 11 additions & 0 deletions 2023/solutions/fsharp/05/src/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Day05.Program

open System.IO

// Part 1
let seeds, maps = File.ReadAllText("input.txt") |> parseInput

seeds
|> List.map (lookupLocationBySeed maps)
|> List.minBy (fun (Location location) -> location)
|> printfn "Part 1: %A"
21 changes: 21 additions & 0 deletions 2023/solutions/fsharp/05/src/Types.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Day05

type Base = uint
type Seed = Seed of Base
type Soil = Soil of Base
type Fertilizer = Fertilizer of Base
type Water = Water of Base
type Light = Light of Base
type Temperature = Temperature of Base
type Humidity = Humidity of Base
type Location = Location of Base

type Maps = {
seedToSoil: Map<Seed, Soil>
soilToFertilizer: Map<Soil, Fertilizer>
fertilizerToWater: Map<Fertilizer, Water>
waterToLight: Map<Water, Light>
lightToTemperature: Map<Light, Temperature>
temperatureToHumidity: Map<Temperature, Humidity>
humidityToLocation: Map<Humidity, Location>
}
1 change: 1 addition & 0 deletions 2023/solutions/fsharp/05/src/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FSharp.Core
16 changes: 16 additions & 0 deletions 2023/solutions/fsharp/05/test/Day05.Test.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\src\Day05.fsproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="Sample.fs" />
<Compile Include="Main.fs" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
7 changes: 7 additions & 0 deletions 2023/solutions/fsharp/05/test/Main.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Test

open Expecto

[<EntryPoint>]
let main argv =
Tests.runTestsInAssemblyWithCLIArgs [] argv
54 changes: 54 additions & 0 deletions 2023/solutions/fsharp/05/test/Sample.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Day05.Tests

open Expecto

let input =
[
"seeds: 79 14 55 13"
""
"seed-to-soil map:"
"50 98 2"
"52 50 48"
""
"soil-to-fertilizer map:"
"0 15 37"
"37 52 2"
"39 0 15"
""
"fertilizer-to-water map:"
"49 53 8"
"0 11 42"
"42 0 7"
"57 7 4"
""
"water-to-light map:"
"88 18 7"
"18 25 70"
""
"light-to-temperature map:"
"45 77 23"
"81 45 19"
"68 64 13"
""
"temperature-to-humidity map:"
"0 69 1"
"1 0 69"
""
"humidity-to-location map:"
"60 56 37"
"56 93 4"
]
|> String.concat "\n"

[<Tests>]
let tests =
testList "Functions" [
test "lookupLocationBySeed" {
let expected = [ 82u; 43u; 86u; 35u ] |> List.map Location

let seeds, maps = input |> parseInput
let actual = seeds |> List.map (lookupLocationBySeed maps)

Expect.equal actual expected "Expected a different result"
}
]
2 changes: 2 additions & 0 deletions 2023/solutions/fsharp/05/test/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Expecto
FSharp.Core

0 comments on commit e58e825

Please sign in to comment.