this post was submitted on 01 Dec 2023
8 points (100.0% liked)

Advent of Code

292 readers
1 users here now

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

https://adventofcode.com

founded 2 years ago
MODERATORS
8
Day 1 solutions (adventofcode.com)
submitted 1 year ago* (last edited 1 year ago) by mykl to c/adventofcode
 

How was day one for everyone? I was surprised at how tricky it was to get the right answer for part two. Not the usual easy start I've seen in the past. I'd be interested to see any solutions here.

all 7 comments
sorted by: hot top controversial new old
[โ€“] mykl 4 points 1 year ago* (last edited 1 year ago)

Dart solution

Here's my solution to start the ball rolling. In the end I incorporated my part 1 answer into the part 2 logic, and golfed the code quite a lot.

import 'package:collection/collection.dart';

var ds = '0123456789'.split('');
var wds = 'one two three four five six seven eight nine'.split(' ');

int s2d(String s) => s.length == 1 ? int.parse(s) : wds.indexOf(s) + 1;

int value(String s, List digits) {
  var firsts = {for (var e in digits) s.indexOf(e): e}..remove(-1);
  var lasts = {for (var e in digits) s.lastIndexOf(e): e}..remove(-1);
  return s2d(firsts[firsts.keys.min]) * 10 + s2d(lasts[lasts.keys.max]);
}

part1(List lines) => lines.map((e) => value(e, ds)).sum;

part2(List lines) => lines.map((e) => value(e, ds + wds)).sum;
[โ€“] hal9001 3 points 1 year ago

My clojure solution

Part 2 was unexpected. Was not expecting that level of wrinkle on day 1 for sure.

I saw a few things that were guessing we might have a lot more of these sorts of twists to help throw off the AI solutions ๐Ÿคทโ€โ™‚๏ธ

[โ€“] [email protected] 2 points 1 year ago* (last edited 1 year ago)

I'm not really happy with the repetition I have, but here's my Factor solution:

Here it is on GitHub with comments and imports.

: part1 ( -- )
  "vocab:aoc-2023/day01/input.txt" utf8 file-lines
  [
    [ [ digit? ] find nip ]
    [ [ digit? ] find-last nip ] bi
    2array string>number
  ] map-sum .
;

MEMO: digit-words ( -- name-char-assoc )
  [ "123456789" [ dup char>name "-" split1 nip ,, ] each ] H{ } make
;

: first-digit-char ( str -- num-char/f i/f )
  [ digit? ] find swap
;

: last-digit-char ( str -- num-char/f i/f )
  [ digit? ] find-last swap
;

: first-digit-word ( str -- num-char/f )
  [
    digit-words keys [
      2dup subseq-index
      dup [
        [ digit-words at ] dip
        ,,
      ] [ 2drop ] if
    ] each drop                           !
  ] H{ } make
  [ f ] [
    sort-keys first last
  ] if-assoc-empty
;

: last-digit-word ( str -- num-char/f )
  reverse
  [
    digit-words keys [
      reverse
      2dup subseq-index
      dup [
        [ reverse digit-words at ] dip
        ,,
      ] [ 2drop ] if
    ] each drop                           !
  ] H{ } make
  [ f ] [
    sort-keys first last
  ] if-assoc-empty
;

: first-digit ( str -- num-char )
  dup first-digit-char dup [
    pick 2dup swap head nip
    first-digit-word dup [
      [ 2drop ] dip
    ] [ 2drop ] if
    nip
  ] [
    2drop first-digit-word
  ] if
;

: last-digit ( str -- num-char )
  dup last-digit-char dup [
    pick 2dup swap 1 + tail nip
    last-digit-word dup [
      [ 2drop ] dip
    ] [ 2drop ] if
    nip
  ] [
    2drop last-digit-word
  ] if
;

: part2 ( -- )
  "vocab:aoc-2023/day01/input.txt" utf8 file-lines
  [ [ first-digit ] [ last-digit ] bi 2array string>number ] map-sum .
;
[โ€“] Rooki 1 points 1 year ago (1 children)

Really cool idea! Just bad that it increases the submit time every failed attempt. I have to wait now 10 minutes on the 2nd question. + In what order?

For example: oneight CAN be 1ight or on8

Is it sorted by 1-9 or one - nine or first in word first served?

I think this is my currently issue, and i cant go through and sadly my patience of submitting every try every 10 mins has ended and i just lost it.

The delay is just so unnecessary...

[โ€“] mykl 4 points 1 year ago* (last edited 1 year ago)

Itโ€™s normally not so tricky on the first day, and the examples also normally do a much better job of guiding you away from wrong approaches so the increasing delay is intended to ensure you arenโ€™t just spamming poorly tested answers.

The task in part two could be better written as โ€œfind the first occurrence of a digit in the string (either as a digit or as a word), and then separately find the last occurrence of a digit (either as a digit or as a word again) then compose these two digitsโ€. Thinking of it like that should help you identify where your problem lies.

[โ€“] yukiakari 1 points 1 year ago

๐Ÿ•› ๐Ÿ•‘ ๐Ÿ•’ ๐Ÿ˜ฒ Keep your distance please.