Skip to content

Instantly share code, notes, and snippets.

@aloisdg
Last active December 2, 2022 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloisdg/7ba22a053d27e029e3e80be5c1bad446 to your computer and use it in GitHub Desktop.
Save aloisdg/7ba22a053d27e029e3e80be5c1bad446 to your computer and use it in GitHub Desktop.
Advent_2022_2
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var s = @"A Y
B X
C Z";
Console.WriteLine(s.Split("\n").Sum(Score));
}
static int Score(string input) => input switch
{
("A Y") => 2 + 6,
("B Y") => 2 + 3,
("C Y") => 2 + 0,
("A X") => 1 + 3,
("B X") => 1 + 0,
("C X") => 1 + 6,
("A Z") => 3 + 0,
("B Z") => 3 + 6,
("C Z") => 3 + 3,
};
}
let s = """A Y
B X
C Z"""
let score x =
match x with
| "A Y" -> 2 + 6
| "B Y" -> 2 + 3
| "C Y" -> 2 + 0
| "A X" -> 1 + 3
| "B X" -> 1 + 0
| "C X" -> 1 + 6
| "A Z" -> 3 + 0
| "B Z" -> 3 + 6
| "C Z" -> 3 + 3
| _ -> 0
s.Split "\n"
|> Array.sumBy score
|> printfn "%i"
using System;
using System.Linq;
public class Program
{
public static void Main()
{
var s = @"A Y
B X
C Z";
Console.WriteLine(s.Split("\n").Sum(Score));
}
static int Score(string input) => input switch
{
("A Y") => 1 + 3, // ok
("B Y") => 2 + 3,
("C Y") => 3 + 3,
("A X") => 3 + 0,
("B X") => 1 + 0, // ok
("C X") => 2 + 0,
("A Z") => 2 + 6,
("B Z") => 3 + 6,
("C Z") => 1 + 6, // ok
};
}
let s = """A Y
B X
C Z"""
let score x =
match x with
| "A Y" -> 1 + 3
| "B Y" -> 2 + 3
| "C Y" -> 3 + 3
| "A X" -> 3 + 0
| "B X" -> 1 + 0
| "C X" -> 2 + 0
| "A Z" -> 2 + 6
| "B Z" -> 3 + 6
| "C Z" -> 1 + 6
| _ -> 0
s.Split "\n"
|> Array.sumBy score
|> printfn "%i"
@aloisdg
Copy link
Author

aloisdg commented Dec 2, 2022

Alternative with an array:

let s = """A Y
B X
C Z"""

let score input =
  1 + List.findIndex (fun x -> x = input) [
    "B X";
    "C X";
    "A X";
    "A Y";
    "B Y";
    "C Y";
    "C Z";
    "A Z";
    "B Z" ]

s.Split "\n"
|> Array.sumBy score
|> printfn "%i"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment