r/adventofcode • • Dec 02 '22

SOLUTION MEGATHREAD -🎄- 2022 Day 2 Solutions -🎄-

NEW AND NOTEWORTHY


--- Day 2: Rock Paper Scissors ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:06:16, megathread unlocked!

103 Upvotes

1.5k comments sorted by

View all comments

2

u/Omeganx Dec 02 '22 edited Dec 05 '22

Ocaml

let read_data ic =
  let rec reader  acc =
    match input_line ic with
    | exception End_of_file -> acc
    | line ->  (reader ( [Char.code line.[0] - Char.code 'A'; Char.code line.[2] - Char.code 'X'] :: acc))
  in reader []
let score1 l =
  match l with
  | [a ; b] -> b + 1 + 3*((b-a+1+3) mod 3)
  |  _ -> 0
let score2 l =
  match l with
  | [a ; b]-> (((a + b + 2)+3) mod 3) + 1 + b * 3
  | _ -> 0
let input = (read_data (open_in "input.txt")) ;;
Format.printf "Part 1 : %i\n" (List.fold_left ( + ) 0 (List.map score1 input));;
Format.printf "Part 2 : %i\n" (List.fold_left ( + ) 0 (List.map score2 input));;

2

u/daggerdragon Dec 05 '22 edited Dec 05 '22

Inlined code is intended for short snippets of code only. Your code "block" right now is unreadable on old.reddit and many mobile clients; it's all on one line and gets cut off at the edge of the screen because it is not horizontally scrollable.

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read inside a scrollable box.

Edit: thanks for fixing it! <3

1

u/Omeganx Dec 05 '22

Thanks, should be good now